Basic Syntax of C

Basic Syntax of C

The basic syntax of a simple C program contains header files, main() function, and then program code.

Here is the basic syntax of a C program

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

Output

Hello World!

 

1. Header Files

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

2. Main Function

int main() It is declared of function main. A function is a block of code with a name. It consists of an open curly bracket { and close curly bracket }. It is essential to keep all blocks of codes of the main function within the curly bracket.

printf() is standard character output device defined in <stdio.h> header file. It facilitates output from a program. 

(“Hello world!”) is inserted into the printf() to get the output printed on the output device.

3. Semicolons

The semicolon is used to terminate a statement. It is also called a statement terminator because each individual statement must be ended with a statement terminator or semicolon And it indicates that the current statement has been terminated and the end of one logical entity or statements. If a semicolon is not placed at the end of any statements, then the program will not compile, generate an error message on the compilation. Each individual statement in C must be ended with a semicolon.

Following are two different statements terminated by the semicolon each:

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

4 Line Change(“\n”)

“\n” is line change while performing output operation. “\n” tells the program to start a new line.

Program

#include <stdio.h>

int main () { 
  printf("This is first line.\n");
  printf("This is new line.");
  return 0;
}

Output

This is first line
This is new line

 

Also read, C Programming

Share this post

Leave a Reply

Your email address will not be published. Required fields are marked *