Unit 5 C Prog
Unit 5 C Prog
Unit – V
The C Preprocessor is not a part of the compiler, but is a separate step in the compilation process. In simple
terms, a C Preprocessor is just a text substitution tool and it instructs the compiler to do required pre-
processing before the actual compilation. We'll refer to the C Preprocessor as CPP.
All preprocessor commands begin with a hash symbol (#). It must be the first nonblank character, and for
readability, a preprocessor directive should begin in the first column.
Types of preprocessor directives:
C language supports different preprocessor statements like
Macro substitution
File inclusions
Conditional inclusion/Compilation
Macro substitution
Macro substitution has a name and replacement text, defined with #define directive. The preprocessor simply
replaces the name of macro with replacement text from the place where the macro is defined in the source code.
Constant as macro
Example:
1 #define PI 3.14
2 #include<stdio.h>
3 int main()
4 {
5 int rad;
6 float area,cir;
7 printf("Enter the radios:");
8 scanf("%d",&rad);
9 printf("Area %f",PI*rad*rad);
10 printf("\nCircumference %f",2*PI*rad);
11 return 0;
12 }
Execution:
Enter the radios: 15
Area 706.500000
Circumference 94.200005
1. File inclusive Directories are used to include user define header file inside C Program.
2. File inclusive directory checks included header file inside same directory (if path is not mentioned).
4. If Path is mentioned then it will include that header file into current scope.
5. Instead of using triangular brackets we use “Double Quote” for inclusion of user defined header file.
Explanation :
1. In order to include user defined header file inside C Program , we must have to create one user defined
4. We can combine all our user defined functions inside header file and can include header file
whenever require.
void main()
{
// Define another macro if MACRO NUM is defined
#ifdef NUM
#define MAX 20
#endif
printf("MAX number is : %d",MAX);
}
Output :
MAX Number is 20
Live Example 2 :
#include<stdio.h>
void main()
{
#ifdef MAX
#define MIN 90
#else
#define MIN 100
#endif
Output :
MIN number : 100
Rules :
3. You can nest conditional groups inside other conditional groups, but they must be completely nested.
4. You cannot start a conditional group in one file and end it in another.
5. Live Example 3
6. #include<stdio.h>
7.
8. int main(){
9.
10. #ifdef __DATE__
11. printf("%s",__DATE__);
12. #else
13. printf("__DATE__ is not defined");
14. #endif
15.
16. return 0;
17. }
18. Output :
19. Current Date is Printed
Predefined Macros
ANSI C defines a number of macros. Although each one is available for use in programming, the
predefined macros should not be directly modified.
Sr.No. Macro & Description
1 __DATE__
The current date as a character literal in "MMM DD YYYY" format.
2 __TIME__
The current time as a character literal in "HH:MM:SS" format.
3 __FILE__
This contains the current filename as a string literal.
4 __LINE__
This contains the current line number as a decimal constant.
5 __STDC__
Defined as 1 when the compiler complies with the ANSI standard.
Let's try the following example −
#include <stdio.h>
int main() {
}
When the above code in a file test.c is compiled and executed, it produces the following result −
File :test.c
Date :Jun 2 2012
Time :03:36:24
Line :8
ANSI :1
| Bitwise OR
^ Bitwise XOR
~ Bitwise complement
#include <stdio.h>
int main()
{
int a = 12, b = 25;
printf("Output = %d", a&b);
return 0;
}
Output
Output = 8
Bitwise OR operator |
The output of bitwise OR is 1 if at least one corresponding bit of two operands is 1. In C Programming, bitwise
OR operator is denoted by |.
#include <stdio.h>
int main()
{
int a = 12, b = 25;
printf("Output = %d", a|b);
return 0;
}
Output
Output = 29
#include <stdio.h>
int main()
{
int a = 12, b = 25;
printf("Output = %d", a^b);
return 0;
}
Output
Output = 21
The bitwise complement of 35 is 220 (in decimal). The 2's complement of 220 is -36. Hence, the output is -36
instead of 220.
Bitwise complement of any number N is -(N+1). Here's how:
bitwise complement of N = ~N (represented in 2's complement form)
2'complement of ~N= -(~(~N)+1) = -(N+1)
#include <stdio.h>
int main()
{
printf("Output = %d\n",~35);
printf("Output = %d\n",~-12);
return 0;
}
Output
Output = -36
Output = 11
printf("\n");
return 0;
}