c program structure | 5 basic components of a c program

If you want to know the c program structure then read the complete article.


Components of c program structure

C progam contains 5 main components are listed below.

  • Pre-processor.
  • variables.
  • Functions.
  • C program statements and expressions.
  • Comment lines.

Pre-processor in c program structure

The preprocessor is a simple instruction to compiler. It instructs that pre-processors have to be processed before compiling the program.

Compiler substiutes pre-processor text with its value.

Pre-processors begins with # symbols.

Various pre-processors are as follows:

NameDescription
#defineIt defines a pre-processor-macro.
#undefIt undefines a pre-processor macro.
#includeIt uincludes a header file.
#pragmaIt is to give special commands to the compiler.
#ifdefIt returns true if defined.
#ifndefIt returns true if not defined.
#ifpre-processor if block.
#elsepre-processor else block.
#elif#else and #if in on estatement.
#endifIt ends the if pre-processor.
#errorIt prints error message to stderr.

No lets look at some pre-processor examples.

define MY_MACRO 100
#ifndef MY_MACRO
 printf("My macro is not defined");
#endif
#ifdef MY_MACRO
 printf("My macro is defined");
#endif

Try other macoros yourself.

Predefined macros in c

Macro-nameDescription
__FILE__It is the file name of the c program.
__DATE__It returns the date in MM DD YYYY format.
__LINE__It returns current line number as decimal constant.
__TIME__It returns Time in HH:MM:SS format.
__STDC__It is defined as 1 if the compiler compiles in ANSI Standard

preprocessor operators

Name OperatorDescriptionExample
Macro continuation operator\It is used to continue a macro if it doesnot fit in one line.#define my_error(g) \ printf(#g " Error occurred\n")
String size operator#It converts parameter of macro to string constant.#define my_error(a, b) \ printf(#a " and " #b ":Are the errors\n")
Token pasting operator##It combines two arguments#define token_x(n) printf ("token" #n " = %d", token##n)
Defined operatordefinedIt is used to determine a macro is previously defined or not.#if !defined (MY_MESSAGE) #define MY_MESSAGE "Hello world!" #endif

Parameterized macros: macros which allows to pass parameter like we pass to functions.

Example:

#define square_of_a_number(x) ((x) * (x))


Variable in c program structure

Variables are nothing but name given to storing area of different data.

There are two types of variables: 1) Global variable 2) Local variable.

Below is the example to declare a variable:

datatype variable name;

Example: int example;

You can directly initialize variable like below

int example=100;

Refer to explanation to variables in hello world program.


Functions in c program structure

Function is a set of code which is intended to perform a specific task.

Main function in c where the program begins to exeute.

Function prototype in c is as below:

return_type function name (  parameters )
{
 .
 .
 .
}

Please refer to function example in hello world c program.


C program statements and expressions

Statements are the commands given to the computer to perform specific task.

Types of statements

  • Expression Statements.
  • Labeled Statements.
  • Compound Statements.
  • Jump Statements.
  • Iteration Statements.
  • Selection Statements.

Expression consists one more operand and operator which finally results in a value.

Expressions are used in control statements.


Comment lines in c program structure

Comment lines are for programmers to understand whats going on in the code.

These comment lines are not compiled and has no effect on the output of the c program.

Comment lines in your code is a good programming practice.

Comment lines are essential things.

In c programming there are two types of comment lines:

  • Single line comments. It begins with //
  • Multiple line comments. It begins with /* and ends with */

Example of comment line in c

//This is simgle line comment in c programming.
/*This is multiple line comment in c programming.

Example program for c program structure

#include<stdio.h>//This is #include pre-processor macro
int global_variable=1;//This is a global variable.
void my_function()
{
	printf("My function is called.\n");
}
int main(int argc, char const *argv[])
{
	//this is a single line comment.
	/*
	This is a multiple line comments.
	Line1
	Line2
	.
	.
	.
	.
	.
	*/
	int local_variable=100;//This is a local variable.
	my_function();
	if( local_variable==100 ) //if is a statement and localvariable==100 is an expression
	{
		printf("Local variable value is 100");
	}
	else
	{
		printf("Local variable value is not 100");
	}
	return 0;
}
c program structure
c program structure Example

c programming tutorial


Disclaimer: A tweet screenshot is being used is not the property of scholarsoul.com.