Create your own game
eng   рус

C Language Hello World

Next tutorial: C Language Basic Syntax
Programming tutorial: Hello world in C Language

In this tutorial we'll examine our first program in C language. Acquaintance with any programming language begins with the so called "Hello World" program. Such program should just outputs phrase "Hello World" on the screen.

We can't start with some fancy programs, first we must learn how to make text-based. So our first programs will work in terminal. However we'll start to examine examples related to games quite soon.

Let's look at the source code of "Hello World" in C language:

#include <stdio.h>

int main(void)
    printf("Hello, World!\n");
    return 0;

First string includes the file stdio.h. Files with extension .h in C are header files. #include is a preprocessor directive. It allows us to use already written (by us or other developers) code in current program. In this case it includes stdio.h (standard input/output) - this file contains features that are related to input/output. We need it to use printf construction in code below.

Let's look at main construction and then examine it:

int main(void)
  // other code

Here I put comment in the braces. Comment is the text that will not be executed i.e. it's not part of the program, it's remainder or explanation of the code to yourself or other programmers. Here I replaced the actual code with comment so we can concentrate on main construction.

main is a function that is defined by us. Any program on C language must have this construction. It consists of header - the first string, and the body - everything in braces. What is int and (void) we'll learn in later tutorials. For now just understand that this construction is mandatory for all programs on C. Copy/paste it in yours any future program and put your actual code into braces.

main is called an entry point of the program. When you run any program, operating system (Windows, Linux...) executes everything inside braces of main. We have this code there:

    printf("Hello, World!\n");
    return 0;

Second string is part of main construction and is mandatory too. The first string just prints "Hello, World!" on the screen (yours command line or terminal). To print something on the screen we put the needed text in double quotes and put it in the brackets after word printf. printf is very powerful function and we'll examine it's capabilities soon.

Pay attention that both strings ends with semicolon. Each statement in C should end with semicolon.

String literals

Let's talk about the thing inside parentheses after printf. Literally we see there "Hello, World!\n" - the string of characters inside double quotes. This is called a string literal. When we see in source code characters in double quotes, that means we see string literals. And what about \n?

Escape Sequences in C Language

There are special sequences of characters in C that are called escape sequences. They are used to 'print' new line, tab, unicode characters - these are characters that you can't input from the keyboard. This characters are started from \ symbol:
print("Beginning: \n");
// \n tells the processor to go to next line
printf("\thello"); // prints tab, then 'hello'
printf("\\"); // prints \
printf("\""); // prints "
printf("\'"); // prints '
printf("\?"); // prints ?
printf("\u2605"); // prints ★

We need to use escape sequences to print \, ', ", ?. \u is used to print unicode (we'll have large tutorial about it later) in console. Here is the result of all code above:



printf function in C language

printf is a function. There are many functions. main is a function too. But let's chew it with small bites, we'll have a large tutorial about functions in C programming language. For now think that function is a piece of code that solves specific task. Like printf outputs string. Moreover printf can output formatted string.

printf is a name of a function and in brackets we put arguments of function. Above we saw that printf accepted one argument - string literal. But printf can accept different number of arguments to format the output in specific way. First argument should always be string and in it we can put many format specifiers. After the first argument we place the values for format specifiers in order of appearance of specifiers in first argument. All arguments must be separated by comma.

Format specifiers of printf

Let's see at examples (here I used vertical bars to show what exactly is printed and there are the white spaces):

printf("|%s|\n", "Hello");
//prints: |Hello|
printf("|%7s|\n", "Hello");
// |  Hello|
printf("|%-7s|\n", "Hello");
// |Hello  |
printf("|%.3s|\n", "Hello");
// |Hel|
// |%|
printf("|%s, %s!|\n", "Hello", "World");
// |Hello, World!|

%s format specifier allows to format string of text. As you can see above it allows to set space for text and align it. Pay attention how you can output % sing itself. In the last example there is a printf with two format specifiers.

printf can format other data types: integer numbers and numbers with floating point. Also it can print separate characters:

// |128|
printf("|%f|\n", 3.14);
// |3.14000|
// |X|
// |X|

%i is used to output integer numbers. %f - numbers with floating point. Pay attention that for 3.14 printf output 3.140000. We'll discuss floating point numbers later. %c is used to print separate character. There is a difference between "X" and 'X'. We'll talk about it later too. For now: single quotes are used when you want to represent one character in C language. The interesting thing with the last example. It uses character format specifier, the argument for it is a number and the result is a character. It's one of examples that every being in computers is just a bunch of numbers. Later we'll know how the text is encoded with numbers, but here we can see that 88 representing character X. If you change the number you'll see different characters.

C Language Hello World - Summary

Now let's compile the initial Hello World program in C language and run it. You should see on the screen this phrase:

Hello, World!

That's it for today.


Oct. 11, 2016, 10:06 a.m.
1 Guest
printf("Equivalent octal value: %lo", onum);
Oct. 11, 2016, 3:56 p.m.
2 Guest
printf of course has much more capabilities than described in the tutorial. Including output of octal numbers. I've just choose the things that will be used in this series of tutorials.
Oct. 11, 2016, 3:59 p.m.
3 Guest
About comments. Now all comments are anonymous - even from author (me). Later there will be registration - firstly need to create some content.
Oct. 16, 2016, 7:07 a.m.
4 Guest
nice and good for make to other person how to know c language in easier guide way for learning.. :D
Oct. 17, 2016, 11:18 a.m.
5 Guest
#include <iostream> using namespace std; int main () { cout<< "123456789"<<endl; return 0; }
Oct. 17, 2016, 12:20 p.m.
6 Guest
Streams are from C++. They are not in C programming language.
Oct. 18, 2016, 3:05 a.m.
7 Guest
#include<stdio.h> void(main) { printf("i am gowri"); if(i=0;i<1;i!=0) { printf("my nagitive loceerla") return 0; } }
Oct. 19, 2016, 5:25 p.m.
8 Guest
Voidf (int, short); void main () { int I =100; short s=12; short*p=&s; f (s,*s);}
Oct. 20, 2016, 4:20 a.m.
9 Guest
token of c language
Oct. 20, 2016, 1:38 p.m.
10 Guest
usage of c progamming language
Nov. 2, 2016, 6:46 p.m.
11 Guest
#include <stdio.h> int main(void) { char sw[256]; printf("Enter symbol a..c\n"); scanf("%s", &sw); switch(sw[0]){ case 'a': printf("Your enter symbol a"); case 'b': printf("Your enter symbol b"); case 'c': printf("Your enter symbol c"); } }
Nov. 4, 2016, 10:25 p.m.
12 Guest
#include <stdio.h> #include <stdlib.h> int main() { int n,min,max,val1,val2,i; printf("donnez le nombre de valeur\n"); scanf(n); printf("donnez la premiere valeur"); scanf(val1); max=val1; min=val1; for(i=2;i=n;i++){ printf("donnez la valeur",i); scanf(val2); if(val2>max){ max=val2; } if(val2<min){ min=val2; } } printf("\n",max); return 0; }
Nov. 9, 2016, 4:45 p.m.
13 Guest
Nov. 10, 2016, 5:43 a.m.
14 Guest
1 1 2 1 2 3 1 2 3 4
Nov. 10, 2016, 7:39 a.m.
15 Guest
#include <studio.h> #include <stdlib.h> main() { int x = 10, y = 20; if(x==y); printf("\n%d %d",x,y) };
Nov. 10, 2016, 2:04 p.m.
16 Guest
The code words in a separate text And each word into an array fold And every word tell
Nov. 11, 2016, 8:09 a.m.
17 Guest
#include<stdio.h>#include<stdlib.h>main(){int x=10,y=20;if(x==y);printf("\n%d%d",x,y)}
Nov. 12, 2016, 8:36 a.m.
18 Guest
please,sir gave me very very simple steps how we can learn c programming contact no is -9506916983.please call/sms to me at only 8:00 pm to 10:00 pm only.everyday/daily.i am do much and much score in the c programming.i am not learn c programming easily.thankyou very much sir.
Nov. 13, 2016, 9:39 a.m.
19 Guest
please define loops while loop and do-loop
Nov. 13, 2016, 10:36 a.m.
20 Guest
The while loop is described four tutorials ahead. On the top there is link to next tutorial. Here is the link to for and while loop tutorial:
Nov. 16, 2016, 5:34 p.m.
21 Guest
dear sir/ madam please give me the short key of c language, using in coding. thank you.
Nov. 20, 2016, 10:03 a.m.
22 Guest
turbo c
Nov. 21, 2016, 1:10 a.m.
23 Guest
#include <stdio.h> #include<stdlib.h> int main(void) { int myAge; prinf(%d,myAge); myAge=25 printf(%d,"25"); return 0 ;