Elements of C language

Before going in-depth, we need to understand the basic elements of the C language. It is very necessary to know these elements because it helps you to write C Program. In the below list, I have listed some important elements of the C language.

  • Preprocessor directives.
  • Functions.
  • Variables.
  • Statements.
  • Comments.

Preprocessor directives:

A pre-processor directive begins with a special character # (pound ). When you compile the code, then before compilation, directives tell the preprocessor to perform specific actions. These actions can be like replacing tokens in the text (using the macro), insert the contents of other files into the source file (Using file inclusion like #include <stdio.h> ), or perform conditional Compilation (using #if, #else, #endif ..etc ).

Functions:

A function is a collection of statements that perform a task, such as the addition of two numbers. In other languages, a function is called procedure or subroutine. In C language there should be at least one function, this mandatory function is the main() function. The main() function is an entry point for c language which means it executes first.

Variables:

A variable defines a location name where we can put value and we can use these values whenever required in the program. In other words, we can say that variable is a name (or identifier) that indicates some physical address in the memory, where data will be stored. The value of a variable can be changed at different times of execution and it may be chosen by the programmer in a meaningful way.

Syntax of variable,

Data_Type  Variable_Name;

E.g.,
Data_Type V1, V2, V3; V1 V2, and V3 are three variables of the same data type.

In the above example, the Data type should be valid. It can be int, char, float, etc, or any user-defined data type like structure, union, enum, etc.

 

Note: Variable names should be unique in the same scope otherwise you will get a compiler error.

 

Statements:

A statement is a command given to the computer that instructs the computer to take a specific action, like displaying a message on the console, performing a mathematical operation, and so on. The C program is the collection of statements and each statement must be terminated with a semicolon(;).

There is a lot of type of statements available in C language these are,

  • Labeled Statements (switch and case)
  • Expression Statements ( optional statement like printf (“welcome”); )
  • Compound Statements (function)
  • Selection Statements (if, else, ..etc)
  • Iteration Statements (for,while, ..etc)
  • Jump Statements (goto, ..etc) .

 

Comment:

Good comments increase the readability of the code. Every module should have good commenting, it helps the developer who comes on the project after you and it also helps to maintain the codebase.

One thing you should remember is if you are commenting on the code which has multiple lines then you should use the preprocessors conditional compilation feature (for example, #if 0 … #endif), it increases the code clarity.

See the below code,

//First way: Don't do this


/*
//bit reversal function
unsigned int ReverseTheBits(register unsigned int x)
{
 x = (((x & 0xaaaaaaaa) >> 1) | ((x & 0x55555555) << 1));
 x = (((x & 0xcccccccc) >> 2) | ((x & 0x33333333) << 2));
 x = (((x & 0xf0f0f0f0) >> 4) | ((x & 0x0f0f0f0f) << 4));
 x = (((x & 0xff00ff00) >> 8) | ((x & 0x00ff00ff) << 8));

return((x >> 16) | (x << 16));
}

*/



//Second way: Do this

#if 0

//bit reversal function
unsigned int ReverseTheBits(register unsigned int x)
{
    x = (((x & 0xaaaaaaaa) >> 1) | ((x & 0x55555555) << 1));
    x = (((x & 0xcccccccc) >> 2) | ((x & 0x33333333) << 2));
    x = (((x & 0xf0f0f0f0) >> 4) | ((x & 0x0f0f0f0f) << 4));
    x = (((x & 0xff00ff00) >> 8) | ((x & 0x00ff00ff) << 8));

    return((x >> 16) | (x << 16));
}

#endif

 

 

Recommended Post: