C TUTORIALS

LEARN C PROGRAMMING

Till now we have learned about how to declare variables or constants, datatypes and much more in C programming language.

Now it’s time to write some programs and learn more about the components of a C Program.

A C Program is a collection of functions and a function is a collection of statements, logically related to each other to perform a given task. The syntax or the general structure to write a C Program is as follows:

/*Comments*/
#include<header file>
main()
{
statement 1;
statement 2;
statement 3;
……………….. 4;


}

  • Comments: Comments are the extra statements in a program that the programmer writes to describe the basic objective of the program, function or statement. Comments can be written any number of times in a program at any place. /*……*/  is the comment marks inside which the comments are written. this is basically used for writing multi-line comments. // can be used for a single line comment. e.g., //for printing sum.

 

  • #include directive: This is used to include any header file available in the c library to the program. The header files are a collection of the functions that are frequently required by the programmers. Altogether these header files make the standard library of the C language. Another way of writing this statement is : #include”header file name” i.e., using the double quotes. Some commonly used header files are : stdio.h, conio.h, math.h, string.h etc.

 

  • main() Function: The main() function is the most important part of the C Program, it is the starting point for the compiler. The compiler starts the compilation of the C Program always from the main() function in a sequential way from the first statement to the last one. The main() function have a datatype either void, int or float etc. Every statement in a C Program ends with a semicolon(;) and every program must have only one main() function as more that one main() function can create confusion to the compiler about the starting point.

Let us write our first program that is to Print HELLO WORLD

Understanding the above code in detail

(//1) The first line of the above code i.e. #include<stdio.h> is used here in order to include the stdio.h header file. It is the standard input/output header file, predefined in the C library.

(//2) The second line void main() defines the declaration of the main() function from where the compiler has to start the execution. A program is a set of functions and the program can have only one main function. The ‘void’ data type shows that the function main does not return any value.

(//3) The { (curly parenthesis) or the opening brace indicates the start of the function main.

printf(“HELLO WORLD”); Here printf() is a predefined function which is used for printing output. Double quotes are used inside the printf function to print a string. It is compulsory to terminate a statement using the semicolon(;).

(//5) The } (curly parenthesis) or the closing brace indicates the end of the function main.

The statements between the braces { } is known as the body of the function.

%d bloggers like this: