Elements of c language

Before going in depth, we need to understand the basic elements of c language. It is very necessary to know these elements because it helps you to write c program. In below list, I have listed some important elements of 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, like 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 main() function. The main() function is an entry point for c language that means it executes first.

Variables:

A variable defines a location name where we can put value and we can use these value whenever required in the program. In other words, we can say that variable is a name (or identifier) which indicate some physical address in the memory, where data will be stored in form of the bits of string. The value of a variable can be changed at different times of executions 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 variable of the same data type.

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

Note: Variable name should be unique 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 display message on the console, performing the mathematical operation and so on. In 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. Evey module should have the good commenting, it helps the developer who comes on the project after you and it also help to maintain the code base.

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

See the below code,