Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
360 views
in Technique[技术] by (71.8m points)

c++ - Preprocessor examples in C language

I want some examples of C preprocessor directives, such as:

#define pi 3.14
#define MAX 100

I know only this. I want to know more than this, more about preprocessor directives.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

The biggest example would be

 #include<stdio.h>

But there are a fair amount. You can also define macros:

 #define MAX(X,Y) (((X) > (Y)) ? (X) : (Y))

And use header guards

#ifndef A_H
#define A_H

// code

#endif

There are proprietary extensions that compilers define to let you give processing directives:

#ifdef WIN32 // WIN32 is defined by all Windows 32 compilers, but not by others.
#include <windows.h>
#else
#include <unistd.h>
#endif

And the if statement cause also be used for commenting:

#if 0

int notrealcode = 0;

#endif

I often use the preprocessor to make debug builds:

#ifdef EBUG
printf("Debug Info");
#endif

$ gcc -DEBUG file.c //debug build
$ gcc file.c //normal build

And as everyone else has pointed out there are a lot of places to get more information:


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...