Pre-processor in C
The preprocessor in C here is not part of the compiler, but there are separate steps in the compilation process. In the most basic way, the preprocessor in C language is the text replacement tool and the compiler instruction does not require pre-processing before being compiled. We aim to preprocessor C like CPP.
All preprocessor commands start with # #. It is at least not white characters, for easy reading. Below is a list of important pre-processing cards.
Description # define Replace for preprocessor macro #include Insert a special header from another file #undef Do not define a preprocessor macro #ifdef Returns true if this macro is defined #ifndef Returns price true if this macro is not defined #if Check if the compilation condition is true #else Part replacement for #if #elif #else a #if in a #endif command End preprocessor condition #error Print error message on stderr #pragma Notice special commands to the compiler, using a standardized methodFor example the preprocessor set in C
Analyze the following examples to understand the various directives.
#define DO_DAI_MANG_TOI_DA 20
This preprocessor notifies the C compiler to replace DO_DAI_MANG_TOI_DA with 20. Using #define for constants increases the readability of the program.
#include #include "headercuatoi.h"
This preprocessor tells the compiler to retrieve the stdio.h library from the System Library and add the current source code. The next line tells the compiler to get the headercuatoi.h file from the computer directory and add the current content and source.
#undef KICH_CO_FILE #define KICH_CO_FILE 42
This preprocessor tells the compiler to modify the variable KICH_CO_FILE and the new definition has the value 42.
#ifndef THONGDIEP #define THONGDIEP "Chao mung chang dep trai nhat nha!" #endif
This informs the C language compiler which defines THONGDIEP if THONGDIEP is not defined.
#ifdef DEBUG /* tai day la phan lenh de debug cua ban */ #endif
This informs the preprocessor of the command operation if DEBUG is defined.
Macros are predefined in C
ANSI C defines a number of macros. Although each of these macros is available for you to use in the program, you should not directly edit these predefined macros.
MacroDescription__DATE__ Current date, as a character constant, in the format "MMM DD YYYY" __TIME__ Current time, as a character constant, in the format "HH: MM: SS" __FILE__ It contains the name Current file as a constant string __LINE__ It contains the current line number as a decimal constant __STDC__ Defined as 1 when the compiler compiles with ANSI standardTry the following example:
#include main () { printf ( "File :%sn" , __FILE__ ); printf ( "Date :%sn" , __DATE__ ); printf ( "Time :%sn" , __TIME__ ); printf ( "Line :%dn" , __LINE__ ); printf ( "ANSI :%dn" , __STDC__ ); printf ( "n===========================n" ); printf ( "QTM chuc cac ban hoc tot! n" ); }
When the above C program in the file Untitled4.cpp is compiled and executed to see the result:
Preprocessor operator in C
The C language provides the following operators to help you create macros:
The continuation operator () of the macro in C
A macro is usually included in a single line. The macro's continuation operator is often used to continue a macro if there is more than one line. For example:
#define thong_diep ( a , b ) printf (# a " va " #b ": nghia la Forever Alone!n")
The pound sign (#) in C
The stringize operator - the pound sign ('#') , when used in a macro definition, converts a macro parameter into a string constant. This operator can be used with macros to specify a specific parameter in the parameter list. For example:
#include #define thong_diep ( a , b ) printf (# a " va " #b ": nghia la Forever Alone!n") int main ( void ) { thong_diep ( F , A ); printf ( "n===========================n" ); printf ( "QTM chuc cac ban hoc tot! n" ); return 0 ; }
Compile and execute the above C program to see the results:
Token Pasting operator (##) in C
The token pasting operator (##) is used in a macro definition that connects two parameters. It allows two separate tokens in the macro definition to be combined into one token. Therefore it is also called a compound operator . For example:
#include #define vidutoken ( n ) printf ( "token" #n " = %d", token##n) int main ( void ) { int token1 = 123 ; vidutoken ( 1 ); printf ( "n===========================n" ); printf ( "QTM chuc cac ban hoc tot! n" ); return 0 ; }
Compile and execute the above C program to see the results:
How does it happen, because this example has a real output from the preprocessor:
printf ( "token1 = %d" , token1 );
This example shows the concatenation of the token ## n in token34 and here we used both stringize and token-pasting .
Operator defined () in C
Preprocessor operator defined is used with constant expression to determine if an identifier is defined by #define. If the specified identifier is defined, the value is true (other than 0). If not defined, the value is false (zero). The operator defined is defined as follows:
#include #if !defined (THONGDIEP) #define THONGDIEP "Chao mung chang dep trai nhat nha!" #endif int main ( void ) { printf ( "Dev-C++: %sn" , THONGDIEP ); printf ( "n===========================n" ); printf ( "QTM chuc cac ban hoc tot! n" ); return 0 ; }
Compile and execute the above C program to see the results:
Macro parameter in C
One of CPP's powerful features is the ability to mimic functions by using parameter macros. For example, we can have a code to square a number like this:
int binhphuong ( int x ) { return x * x ; }
We can rewrite the above code using a macro like this:
#define binhphuong ( x ) (( x ) * ( x ))
Macros with parameters must be defined using #define before they can be used. The list of parameters is enclosed in parentheses and must be immediately followed by the macro name. Spaces are not allowed between macro names and open parentheses. For example:
#include #define LONNHAT ( x , y ) (( x ) > ( y ) ? ( x ) : ( y )) int main ( void ) { printf ( "Gia tri lon nhat giua 123 va 321 la %dn" , LONNHAT ( 123 , 321 )); printf ( "n===========================n" ); printf ( "QTM chuc cac ban hoc tot! n" ); return 0 ; }
Compile and execute the above C program to see the results:
According to Tutorialspoint
Previous article: Read - Write File in C
Next article: Header File in C
You should read it
May be interested
- Toshiba applies PS3 processor chip to laptoptoshiba said it will apply the broadband processor chip technology of the popular sony playstation 3 gaming system (ps3) to future notebooks.
- How to Upgrade Your Computer's Processorthis article shows you how to install a new processor on a windows computer. the microprocessor (cpu) plays an important role in the speed of the computer.
- Intel revealed information about 8-core i9-9900KS CPUthis morning, may 28, the world's largest processor chip intel officially revealed information about the latest generation i9-9900ks processor, which received a lot of attention from the community in last time.
- What is the CPU?cpu (central processing unit) is the central processor of the computer. the cpu processes all the commands it receives from hardware and software running on the computer.
- Learn about Snapdragon microprocessors on smartphones and tabletsuse smartphones daily but have you ever heard of snapdragon processors on your device? if you haven't heard it yet and still don't understand it, update your knowledge right through the article below.
- XPS 13 Ultrabook runs Windows 8 at the end of this monthdell products running the latest ivy bridge processor and still keep the starting price of $ 999.99 (about vnd 20.8 million).
- Intel brought its strongest chip, a Core i9 processor, to a laptopthis is the best processor and game processor for laptops that intel has ever created.
- Apple's A14 chip is likely to be the first mobile processor to exceed 3GHzapple's a14 processor, the successor to the a13 chip found in the iphone 11 and iphone 11 pro, is expected to launch this fall, in new iphones 12.
- 5 things to know about Qualcomm Snapdragon 845 chipqualcomm announced the snapdragon 845 at the hawaii tech summit but it wasn't until yesterday that they released details about the new chip.
- Intel updates the CPU price list for PCs with X 12 series of chipsthe information update below on intel's pricing will give us a better idea of what the company will charge for the upcoming 12-core processor as well as its performance. let's see more details!