Programming Structure in C Programming

Like any other programming language, C is also written in a text file in Windows operating system. One can use vi editor in UNIX / LINUX. Let us first understand the basic structure of C program. It will help in understanding latter topics.

Any C program will mainly have 5 sections in its program.

Preprocessor Directives

This is the first line in any C program. It is mainly used to include other pre-compiled programs into the current program. These directives will also be called as header files. This will help to re-use the already existing code. Since C is a program with functions, these header files will also have different functions within it. When they are included in the current program and when we call the functions from those files, they act as if they are written in the current program itself. This is because, when compiler compiles the current C program and sees these header files, it replaces the compiled version of those files into the current program. Thus it reduces re-writing time and code length.

These preprocessor directives are usually written using ‘#’, followed by ‘include’ or ‘define’. The directive ‘include’ is used for inserting other compiled C programs/ functions. It is followed by file names. The directive ‘define’ is used for initializing a global constant variable and for defining some symbols too. These header files are saved with ‘.h’ extensions.

One of the main preprocessor directives is stdio.h. This is the header mainly used for input and output functions. It contains functions like printf (), getchar () etc.

#include 
#include “stdio.h”
#include 
#define const_var 100 // initializes value for a constant variable const_var as 100
#define EQ = // defines an equivalent notation for ‘=’ as ‘EQ’.

Functions

C is a function oriented program. Hence the body of the program itself is a function. This is called as main () function. This in turn calls other functions. When the compiler starts compiling, it first replaces the header files with its respective files/ values, then it starts evaluating from the main () function. Like any other functions, it has also return type. When it is mentioned as void, then no need to return any values to the calling operating system, else we need to return the respective return values. All functions in C is has curly braces after its name to indicate beginning of the function. Once function is complete, braces are closed to indicate the end.

void main () {
	…
}

 

int main () {
	…
	return 0;
}

 

float main () {
	…
	return 0.0;
}

Comments

Comments are used for the developer or the user to understand the program or the code. They are not part of compliable code. Comments are started with ‘//’ for single line comment and are started with ‘/*’ and ended with ‘*/’ for multiline comments. These are followed by plain text to define the code. When the compiler sees the comments, it ignores them and does not compile it.

/* defines an equivalent notation for ‘=’ as ‘EQ’.
Whenever the compiler sees EQ in the code, it replaces it by ‘=’*/

// defines an equivalent notation for ‘=’ as ‘EQ’

Variables

Variables are the temporary place holders in the program, used to hold values in the program. For example, when we want to find the sum of two numbers, then we need 3 variables – two for storing the numbers to be added and one for holding their sum. In C variables are case sensitive. For example, var1 is different from Var1.

int var1;    int var2;    int sum;     float avg;    char key;

Statements and Expressions

These are the actual lines of codes in the program. These may be simple statements for getting the input from the user or displaying result or any messages, or any expressions like arithmetic operations, logical operations or any other calculations. Any line of code in C is terminated by semicolon (;) to indicate end of the code line.

printf (“This is a C Program!”);
getchar ();
sum = var1 + var2;
strSize = strlen (strName);

Below is the simple example of a C program which displays “This is a C Program!” on the screen. In some of the compilers, while executing the program it clears the screen or screen disappears as soon as it displays the results. In order to avoid screen from disappearing, we can give getchar (), which will wait for the user to enter any input (especially enter any key) and press enter. Thus user can read the output before screen disappears.

#include  // Preprocessor directive for input and output

int main () // beginning of the code
{
	printf ("This is a C Program!"); // This is a statement in C
	//getchar (); 

	/* Returns the value to the calling operating system*/
	return 0;
}

C programs can be saved with any names with extension ’.c’ in any preferred locations. But when compiling the code, we need to go that location and type gcc filename.c. this will check for any compilation errors and list them, if any. While compiling, if it gets any error, it lists like below with error, file name and line number.

If there are no errors, then it will generate an executable code named a.out. Any C program will have executable code name as a.out irrespective its file name. this executable file is then executed to get the output. This is how we compile in command prompt (windows) or any UNIX/LINUX systems.

Above example program is saved as firstCProg.c. Now when we compile and execute this program we see below result.

C:\Users\CPrograms> gcc firstCProg.c
C:\Users\CPrograms>./a.out

If you are using any tools like visual studio, then debug the code in the editor which will generate the executable code and then open the console for executing this code like shown below. Here we will not use a.out file to execute the code, rather we will use the project name in which C file exists. In this case CPrograms is the project name and firstCProg.c is C program name


Next > < Prev
Scroll to Top