Different Parts of a C Program

Different parts of a C Program

In this article, I am going to discuss the different parts of a Sample C Program in detail. Please read our previous article, where we discussed the basic syntax of C Program.

Sample C Program
#include <stdio.h>
int main()
{
    printf("Welcome to C tutorials");
    return 0;
}

Header file inclusion:

stdio.h:

stdio.h refers to standard input, output in C language in which built-in functions like printf(), scanf() is defined. The header file links the content to read and display the output using programs. This is a header file which holds the definition of the functions that are responsible for inputting and outputting, like the scanf, printf, gets, puts. When you include the stdio.h header file is when you are actually able to take an input from the user, either from the console or through a file.

#include:

In the C Programming Language, the #include directive tells the preprocessor to insert the contents of another file into the source code at the point where the #include directive is found. It is used to include a library function or to define a macro or to import codings from other programs or sources. #include is used to import the libraries into the c programming. For getting input we need to use scanf() function.to print data we need printf().

main function:

In C, program execution starts from the main() function. Every C program must contain a main() function. The main function may contain any number of statements. These statements are executed sequentially in the order in which they are written. The main function can in-turn call other functions. When the main calls a function, it passes the execution control to that function. The function returns control to the main when a return statement is executed or when the end of the function is reached. In C, the function prototype of the ‘main’ is one of the following:

int main(); //main with no arguments
int main(int argc, char *argv[]); //main with arguments

Return Statement in C:

The return statement is used to terminate the execution of a function and transfer program control back to the calling function. In addition, it can specify a value to be returned by the function. A function may contain one or more return statements.

return expression ;

Evaluates the expression, terminates the current function, and returns the result of the expression to the caller (the value returned becomes the value of the function call expression). Only valid if the function return type is not void.

return;

Terminates the current function. Only valid if the function return type is void.

printf( )

printf( ) is a predefined C function for displaying or printing outputs. It usually needs to include the header file stdio.h in the program. It prints whatever is written within its two double quotes, either directly or by substituting the control sequence with the values from the corresponding variables.

Syntax:

printf(“Welcome to C tutorials”);
Or
printf(“Welcome to C tutorials”, variables);

The control sequence specifies the formats for different types of data, such as

%c for a character value
%d for an integer value
%f for floating-point values (i.e. decimal values)
%s for a sequence of characters or string

Example:

printf(“Welcome to C tutorials”, variables);
printf(“%d and %d”, ,x, y); //given x and y are integers

scanf( )

scanf( ) is a predefined C function like printf( ), used for accepting inputs. It usually needs to include the header file stdio.h in the program. It accepts values through the control sequences placed within its two double quotes and stores them in the corresponding variables.

Syntax:  scanf(“Control sequences”,&variables);

The control sequence specify the formats for different types of data, such as

%c for a character value
%d for a integer value
%f for a floating point values (i.e. decimal values)
%s for a sequence of characters or string without spaces (in the case of strings there is no need of adding & before its variable)

Example:

scanf(“%d”,&x); //accepts integer values, given x is an integer
scanf(“%d%d”,&a,&b); //accepts two integer values, given a and b is are integers

PROGRAM:

/*Input 2 integers and display it*/

#include <stdio.h>

void main()
{
    int x, y; //Declaration of x and y
    printf("Enter 2 integer numbers");	//Prints a message on a screen
    scanf("%d%d", &x, &y);	//Accepts 2 integer inputs, stores in x and y
    printf("x = %d and y = %d", x, y);	//Prints the value of x and y
}

Output:

different part of a sample C Program

In the next article, I am going to discuss C Constants in detail. Here, in this article, I try to explain the different parts of a C Program in detail and I hope you enjoy this Different Parts of a C Program article.

Leave a Reply

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