note_practical_C_programming chapter 10
来源:互联网 发布:android没有网络展示 编辑:程序博客网 时间:2024/05/20 06:52
Practical C Programming Chapter 10
C need a facility for handling named constants, marcos, and include files.
==> C pre-processor
1. #define statement
All pre-processor commands begin with a hash mark (#) in column one.
the pre-processor knows nothing about C
A pre-processor directive terminates at the end-of-line
#define name substitute-text
name can be any valid C identifier and substitute-text can be anything
Hint: C allows you to run your program through the pre-processor and view the output. In UNIX, the command:
2. #define vs. const
the #define directive can only define simple constants
the const statement can define almost any type of C constant, including things like structure classes
the #define directive is, however, essential for things like conditional compilation and other specialized uses
3. conditional compilation
In theory, C code is portable; in actual practice, different operating systems have little quirks that must be accounted for.
An other portability problem is caused by the fact that the standard leaves some of the features of the language up to the implementers.
The pre-processor, through the use of conditional compilation, allows the programmer great flexibility in changing the code generated.
NOTE: it's not necessary to put the /* DEBUG */ after the #endif; however, the entry is very useful as a comment
The general form of the option is -Dsymbol or -Dsymbol=value
Most C compilers automatically define some system-dependent symbols
4. include Files
The #include directive allows the program to use source code from another file
Files that are included in other programs are called header files. (Most #include directives come at the head of the program.)
The angle brackets (<>) indicate that the file is a standard file. On UNIX, these files are located in /usr/include
Standard include files define data structures and macros used by library routines.
Local include files may be specified by using double quotes ("") around the file name
The directive #ifndef symbol is true if the symbol is not defined. The directive is the reverse of #ifdef
5. Parameterized Marcos
#define SQR(x) ((x) * (x)) /* Square a number */
Note: No spaces must exist between the marco (SQR) and the parenthesis
6. Advanced Features
#if directive for conditional compilation
#pragma directive for inserting compiler-dependent commands into a file
7. Summary
The C preprocessor is very useful part of the C language. It has a completely different look and feel, though, and it must be treated apart from the main C complier
Problems in marco definitions often do not show up where the marco is defined, but result in errors much future down in the program. By following a few simple rules, you can decrease the chances of having problems:
Put parentheses () around everything. In particular, they should enclose #define constants and marco parameters
When defining a marco with more than one statement, enclose the code in curly braces ({})
The preprocessor is not C. Don't use = and ;. Finally, if you got this far, be glad that the worst is over.
C need a facility for handling named constants, marcos, and include files.
==> C pre-processor
1. #define statement
All pre-processor commands begin with a hash mark (#) in column one.
the pre-processor knows nothing about C
A pre-processor directive terminates at the end-of-line
#define name substitute-text
name can be any valid C identifier and substitute-text can be anything
Hint: C allows you to run your program through the pre-processor and view the output. In UNIX, the command:
%cc -E prog.c
2. #define vs. const
the #define directive can only define simple constants
the const statement can define almost any type of C constant, including things like structure classes
the #define directive is, however, essential for things like conditional compilation and other specialized uses
3. conditional compilation
In theory, C code is portable; in actual practice, different operating systems have little quirks that must be accounted for.
An other portability problem is caused by the fact that the standard leaves some of the features of the language up to the implementers.
The pre-processor, through the use of conditional compilation, allows the programmer great flexibility in changing the code generated.
#ifdef DEBUG printf("In compute-hash, value %d hash %d\n, value, hash); #endif /* DEBUG */
NOTE: it's not necessary to put the /* DEBUG */ after the #endif; however, the entry is very useful as a comment
#define DEBUG /* Turn debugging on */ #undef DEBUG /* Turn debugging off */ #ifdef DEBUG printf("Test version. Debugging is on\n"); #else DEBUG printf("Production verison\n"); #endif /* DEBUG */The compiler switch -Dsymbol allows symbols to be defined on the command line
The general form of the option is -Dsymbol or -Dsymbol=value
#undef DEBUGwill result in DEBUG being undefined whether or not you use -DDEBUG
Most C compilers automatically define some system-dependent symbols
4. include Files
The #include directive allows the program to use source code from another file
Files that are included in other programs are called header files. (Most #include directives come at the head of the program.)
The angle brackets (<>) indicate that the file is a standard file. On UNIX, these files are located in /usr/include
Standard include files define data structures and macros used by library routines.
Local include files may be specified by using double quotes ("") around the file name
The directive #ifndef symbol is true if the symbol is not defined. The directive is the reverse of #ifdef
5. Parameterized Marcos
#define SQR(x) ((x) * (x)) /* Square a number */
Note: No spaces must exist between the marco (SQR) and the parenthesis
6. Advanced Features
#if directive for conditional compilation
#pragma directive for inserting compiler-dependent commands into a file
7. Summary
The C preprocessor is very useful part of the C language. It has a completely different look and feel, though, and it must be treated apart from the main C complier
Problems in marco definitions often do not show up where the marco is defined, but result in errors much future down in the program. By following a few simple rules, you can decrease the chances of having problems:
Put parentheses () around everything. In particular, they should enclose #define constants and marco parameters
When defining a marco with more than one statement, enclose the code in curly braces ({})
The preprocessor is not C. Don't use = and ;. Finally, if you got this far, be glad that the worst is over.
0 0
- note_practical_C_programming chapter 10
- note_practical_C_programming chapter 8
- note_practical_c_programming chapter 9
- note_practical_c_programming chapter 11
- Chapter 10 Inheritance
- CHAPTER 10:EX 02
- CHAPTER 10:EX 33
- CHAPTER 10:EX 34
- CHAPTER 10:EX 24
- CHAPTER 10:EX 19
- Chapter 10 - Programmable Objects
- !!!Chapter 10 Associative Containers
- Chapter 10 信号
- Chapter 10 – Test
- Chapter 10 tuplesn&Assignment
- chapter 10:Approximate Inference
- Chapter 10 Virtual Memory
- Chapter 10 策略模式
- WorkSpace使用(与Library结合)
- 写点什么
- uva 11520 暴力
- HDU 2131
- Oracle11gr2新增APPEND_VALUES提示
- note_practical_C_programming chapter 10
- php按首字母排序
- 一步一步学习JavaScript
- 中国码农硅谷生存现状调查
- Codeforces Round #247 (Div. 2)
- Eclipse编写和调试Applet程序,调试的时候Eclipse会自动调用Java "小程序查看程序",而不用嵌入到网页中查看运行效果。
- 锤子手机T1发布:售价3000元起 7月初上市
- js的Array类型的常用方法
- ExtJs入门测试用例