Preprocessor statements, constants and comments in C programming
Preprocessor statements
The first statement in most C programs is usually a preprocessor directive. For example, in the code below, #include<stdio.h>
is a preprocessor directive.
#include<stdio.h>
int main(){
return 0;
}
The C compiler can be thought of having two parts; the preprocessor, followed by the real compiler. The preprocessor part is responsible for performing simple textual manipulation of the program before the modified text is compiled.
A preprocessor directive is any line of code whose first visible character is a #
. For example, the preprocessor directive #include
causes the preprocessor to replace the line containing it with the contents of another file whose filename is found between the <
and >
brackets.
Constants in C programming
Constants are like variables in the sense that there are used to hold values. But unlike variables, the value held by a constant cant be changed later. Constants are useful for holding values that you don’t want to accidentally change later as the program is executed.
Defining constants with the define
statement
Another useful preprocessor directive is the #define
statement.
Syntax:
#define IDENTIFIER replacement
The preprocessor replaces the name represented by IDENTIFIER
with the text of replacement
whenever IDENTIFIER
occurs in the program. By convention, the identifier is written in uppercase for the readability of the program. The most common use of the #define
statement is to declare names for constants. For example:
#define MOLAR_GAS_VOLUME 24
The piece of code above means that whenever the preprocessor comes across the identifier MOLAR_GAS_VOLUME
it will replace it with 24. The usage of the statements is as follows:
#include <stdio.h>
#define MOLAR_GAS_VOLUME 24
#define AVOGADRO_CONSTANT 6e+23
int main(){
double volumeOfGas;
printf("Enter the volume of the Oxygen gas in dm cubed: \n");
scanf("%lf", &volumeOfGas);
double numberOfMoles = volumeOfGas/MOLAR_GAS_VOLUME;
double numberOfAtoms = numberOfMoles * AVOGADRO_CONSTANT;
printf("There is %.3lf mol or %lf atoms of Oxygen available", numberOfMoles, numberOfAtoms);
return 0;
}
Declaring constants with the const
keyword
You can also declare constants using the const
keyword.
const type identifier = value;
For example:
#include <stdio.h>
int main(){
const double ACCELERATION = 9.81;
const double MASS = 5.2;
int time = 2;
double force;
double impulse;
force = MASS * ACCELERATION;
impulse = force * time;
printf("The force causing the acceleration is %lf and its impulse is %lf", force, impulse);
return 0;
}
Identifiers for constants are uppercase by convention.
Comments in C programming
Comments are used to pass information to the person reading the code. The compiler treats a comment as a whitespace, which means that it does not change the execution of the program.
There are 3 ways of adding comments in C:
/* */
for multi-line comments//
for single line comments- using preprocessor
Multi-line comments
Multi-line comments are opened by the pair of characters /*
(with no space between them). From that point onwards, everything found there is taken to be a comment until the closing pair of characters */
is reached. The whole comment is taken in and is
replaced by a single space by the compiler. For example:
#include <stdio.h>
int main(){
/*this is a multiline comment
and it is allowed to span
many lines as you can
see. It doesn't affect the running
of the program */
printf("You can see me, I am outside the comment block. \n");
/* printf("I am inside the comment block and you will never know I m here"); */
/* can be used also for single line comments */
int /*integer variable*/ a = 5;
/*the compiler converts every comment into a single whitespace*/
int b = 6;
printf("%d and %d", a, b);
return 0;
}
And the output will be:
You can see me, I am outside the comment block.
5 and 6
Comments cannot be nested because any subsequent /*
will be ignored as part of the comment and the first */
will be treated as ending the comment.
Single line comments
Single line commenting by two forward slashes was introduced in C99. This syntax was borrowed from C++. This type of comment is also called an inline comment because it spans a single line only. For example:
#include <stdio.h>
int main(){
// this is a comment
// it spans only a single line
return 0;
}
Commenting using the preprocessor
A common way of commenting out large chunks of code is by the use of the preprocessor directives #if 0
and #endif
. Remember, earlier on I said you can’t nest comments. This means that you can’t use multi-line comments to comment out a chuck of code that also contains multi-line comments. This type of commenting solves this issue.
For example:
#include <stdio.h>
int main(){
#if 0
everything in here is a comment
even multiline comments can be nested in here
For example I m going to put in here a piece of code that contains
multiline comments.
/*int variable*/
int a = 23;
/* print variable */
printf("%d", a);
#endif
return 0;
}
Here is how it works. #if 0
evaluates to false which means that everything between it and the #endif
are removed by the preprocessor before compilation.