C Preprocessor Directives
C Preprocessor is a separate program invoked by the compiler as first part of the compilation. It includes the directives for source file inclusion (ie #include), macro definition (#define) and conditional inclusion (ie #if).
Preprocessor directives starts with a "#" sign, and that statement will be executed prior to the actual compilation of the code.
(1) Source code inclusion
#include DirectivePreprocessor will replace the #include directive by entire content of the specified file.
If the file name is enclosed in angle brackets, compiler will search the file in standard directories where it is configured to search.
#include < stdio.h>If the file name is enclosed in single quotes, compiler will search the file first in current directory and then if not found search in standard directories.
#include "Timer.h"Inorder to search in directories other than standard directories, use -I flag in the Makefile.
CFLAGS += -I$(TOSDIR)/lib/printf
(2) Macro definition
"#define" Directive
#define Directive Defines the processor to replace all subsequent occurrence of a macro with a specific token.
#define ADXL345_DEVID 0x00The above example causes the preprocessor to replace the subsequent occurrence of the identifier "ADXL345_DEVID" with constant hex value 0x00.
"#undef"Directive
Defines macros are not affected by the block structure of C. A macro once defined will last until a "#undef" directive for that particular token. After undefining you can again redefine that token.
#define ADXL345_DEVID 0x00 #undef ADXL345_DEVID #define ADXL345_DEVID 0x01
(3) Conditional Inclusions
Allows to include or discard certain code based on some conditions.
#ifdef
#ifdef NUM int amount [NUM]; #endifHere the middle line will be compiled only if the NUM variable is already defined with #define.
#ifndef
#ifndef NUM #define NUM 46 int amount [NUM]; #endifHere the NUM variable will be defined only if it is not previously defined.
#if, #else, #elif
#if NUM>100 #undef NUM #define NUM 100 #elif NUM<50 #undef NUM #define NUM 50 #else #undef NUM #define NUM 200 #endifHere depending on certain condition the code is compiled.
Race condition
Usually occurs in threaded programs. When two or more threads try to access same resource and change its value at same time, race condition occurs. Inorder to avoid this condition put a lock around the variable before using.Printing in C
To print the remainder
Use % for printing remainder of a division operation.printf("%d",564%10);
rvalue Vs lvalue
rvalue: rvalue of a variable is the value stored in that variable.
lvalue: lvalue of a variable is the value of its address (where it is stored).
j=6; k=j;
Here in first line 'j' is at the left side of the equation and it denotes the address to which value 6 is to be copied. In the second line 'j' is at the right side of the equation and it denotes the value stored in 'j' to be copied to the address denoted by 'k'.
Function Pointer
Declaring function pointer
"ptFunction" is the function pointer declared and initialized to Null value.
int (*pt2Function)(float, char, char) = NULL;
Defining function pointer
"ptFunction" is initialized to "DoIt" function whose definition is given below
ptFunction = DoIt; // short form ptFunction = &DoIt; // correct assignment using address operator void DoIt(float a, char b, char c) { }
Comparing function pointer
This checks whether the "ptFunction" is defined or not.
if(ptFunction >0) { }
Calling function using function pointer
Both the methods will work.
int result1 = ptFunction(12, 'a', 'b'); int result2 = (*ptFunction) (12, 'a', 'b');
Pass function pointer as argument
Both the function call and the function definition are given below.
PassPtr(&DoIt); //Call the function void PassPtr(int (*pt2Func)(float, char, char)) { } // Definition for the called function
Enumerations (enum)
It is a set of named integer constants. Is defined like structures.
Enum definition
enum coin {penny, dollar, paisa=15, dime, quater}
Each of the enum symbol stands for an integer value starting from 0.
Printing the values
printf("%d %d %d %d %d\n",penny,dollar,paisa,dime,quater);Here penny=0, dollar=1, paisa=15, dime=16 and quater=17.
Declare variable
enum coin money;"money" is an object of type coin.
Initialize the variable
money = paisa;"money" is initialized to value "paisa". Printing money will print value 15.
No comments:
Post a Comment