C Tutorial C References

C - Syntax



First C Program

Although the "Hello World!" program looks simple, it has all the fundamental concepts of C which is necessary to learn to proceed further. Lets break down "Hello World!" program to learn all these concepts.

//Hello World! Example
#include <stdio.h>
 
int main ()
{
  printf("Hello, World!");
  return 0;
}

The output of the above code will be:

Hello World!.

Line 1: This is a single line comment which starts with //. Generally, comments are added in programming code with the purpose of making the code easier to understand. It is ignored by compiler while running the code. Please visit comment page for more information.

Line 2: #include <stdio.h> is header file library. It allows program to perform input and output operations.

Line 3: Blank line has no effect. It is used to improve readability of the code.

Line 4: int main() - It declares of function main. A function is a block of code with a name. Please visit function page for more information.

Line 5 and 8: It consists of open curly bracket { and close curly bracket }. It is essential to keep all block of codes of main function within curly bracket.

Line 6: printf("Hello, World!"); - printf() is standard character output device defined in <stdio.h> header file. It facilitates output from a program. Finally, ("Hello world!") is inserted into the printf() to get the output printed on the output device. Please note that, in C, every statement ends with semicolon ;.

Line 7: reurn 0; indicates ends of the main function.

Semicolons

In a C program, the semicolon is a statement terminator. Each individual statement in C must be ended with a semicolon. It indicates that the current statement has been terminated and other statements following are new statements. For example - Given below are two different statements:

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

line change ("\n")

"\n" facilitates line change while performing output operation. "\n" tells the program to start a new line.

#include <stdio.h>

int main () { 
  printf("Hello World!.\n");
  printf("Learning C is fun.");
  return 0;
}

The output of the above code will be:

Hello World!.
Learning C is fun.