当前位置:网站首页>[C and pointer Chapter 14] preprocessor
[C and pointer Chapter 14] preprocessor
2022-07-24 11:49:00 【Xiaoyuan Xiaoyuan eats dumplings】
The first 14 Chapter The preprocessor
14.1 Predefined symbols
#include<stdio.h>
int main()
{
printf(" The name of the source file to compile :%s \n",__FILE__);
printf(" The line number of the current line of the file :%d \n",__LINE__);
printf(" The date the file was compiled :%s\n",__DATE__);
printf(" When the file was compiled :%s\n",__TIME__);
printf(" Whether the compiler is ANSI C:%d\n",__STDC__);
return 0;
}
==========================================
PS E:\project_C> cd "e:\project_C\test\" ; if ($?) {
gcc test.c -o test } ; if ($?) {
.\test }
The name of the source file to compile :test.c
The line number of the current line of the file :7
The date the file was compiled :Jul 21 2022
When the file was compiled :15:04:21
Whether the compiler is ANSI C:1
14.2 #define
#include<stdio.h>
#define DEBUG_PRINT printf("File %s line %d:"\ "x=%d,y=%d,z=%d",\ __FILE__,__LINE__,\ x,y,z)
int main()
{
int x=1,y=1,z=1;
x *= 2;
y += x;
z = x*y;
DEBUG_PRINT;
return 0;
}
=====================================================
PS E:\project_C> cd "e:\project_C\test\" ; if ($?) {
gcc test.c -o test } ; if ($?) {
.\test }
File test.c line 13:x=2,y=3,z=6
#include<stdio.h>
#define PROCESS_LOOP for(int i=0;i<10;i++) \ {
\ printf("value=%d\n",i); \ }
int main()
{
PROCESS_LOOP;
return 0;
}
===================================================
PS E:\project_C\test> cd "e:\project_C\test\" ; if ($?) {
gcc test.c -o test } ; if ($?) {
.\test }
value=0
value=1
value=2
value=3
value=4
value=5
value=6
value=7
value=8
value=9
14.2.1 macro
#include<stdio.h>
#define SQUARE(x) x*x
int main()
{
printf("result = %d",SQUARE(6));
return 0;
}
=========================================
PS E:\project_C> cd "e:\project_C\test\" ; if ($?) {
gcc test.c -o test } ; if ($?) {
.\test }
result = 36
#include<stdio.h>
#define SQUARE(x) x*x
int main()
{
int a=5;
printf("result = %d",SQUARE(a+1));
return 0;
}
=========================================
PS E:\project_C\test> cd "e:\project_C\test\" ; if ($?) {
gcc test.c -o test } ; if ($?) {
.\test }
result = 11
#include<stdio.h>
#define SQUARE(x) (x)*(x)
int main()
{
int a=5;
printf("result = %d",SQUARE(a+1));
return 0;
}
=======================================
PS E:\project_C> cd "e:\project_C\test\" ; if ($?) {
gcc test.c -o test } ; if ($?) {
.\test }
result = 36
#include<stdio.h>
#define DOUBLE(x) (x)+(x)
int main()
{
int a=5;
printf("result = %d",10*DOUBLE(a));
return 0;
}
=====================================================
PS E:\project_C> cd "e:\project_C\test\" ; if ($?) {
gcc test.c -o test } ; if ($?) {
.\test }
result = 55
#include<stdio.h>
#define DOUBLE(x) ((x)+(x))
int main()
{
int a=5;
printf("result = %d",10*DOUBLE(a));
return 0;
}
===========================================
PS E:\project_C\test> cd "e:\project_C\test\" ; if ($?) {
gcc test.c -o test } ; if ($?) {
.\test }
result = 100
14.2.2 #define Replace
#include<stdio.h>
#define PRINT(FORMAT,VALUE) \ printf("The value is " FORMAT "\n",VALUE)
int main()
{
PRINT("%d",24);
return 0;
}
===============================================
PS E:\project_C> cd "e:\project_C\test\" ; if ($?) {
gcc test.c -o test } ; if ($?) {
.\test }
The value is 24
#include<stdio.h>
#define PRINT(FORMAT,VALUE) \ printf("The value of " #VALUE \ " is " FORMAT "\n",VALUE)
int main()
{
int x = 20;
PRINT("%d",x+3);
return 0;
}
========================================
PS E:\project_C\test> cd "e:\project_C\test\" ; if ($?) {
gcc test.c -o test } ; if ($?) {
.\test }
The value of x+3 is 23
#include<stdio.h>
#define ADD_TO_SUM(sum_number,value) \ sum ## sum_number += value
int main()
{
int sum1 = 20;
ADD_TO_SUM(1,10);
printf("sum1 The value of is :%d",sum1);
return 0;
}
============================================
PS E:\project_C> cd "e:\project_C\test\" ; if ($?) {
gcc test.c -o test } ; if ($?) {
.\test }
sum1 The value of is :30
14.2.3 Macros and functions
#include<stdio.h>
#define MAX(a,b) ((a)>(b)?(a):(b))
int main()
{
int a = 10,b = 15;
printf("MAX = %d",MAX(a,b));
return 0;
}
===================================================
PS E:\project_C> cd "e:\project_C\test\" ; if ($?) {
gcc test.c -o test } ; if ($?) {
.\test }
MAX = 15
#include<stdio.h>
#include<stdlib.h>
#define MALLOC(n,type) \ ((type *)malloc((n) * sizeof(type)))
int main()
{
int *p = MALLOC(25,int);
if(p!=NULL)
printf("OK");
return 0;
}
===========================================
PS E:\project_C> cd "e:\project_C\test\" ; if ($?) {
gcc test.c -o test } ; if ($?) {
.\test }
OK
14.2.4 Macro parameters with side effects
#include<stdio.h>
#define MAX(a,b) ((a)>(b)?(a):(b))
int main()
{
int x=5,y=8;
int z = MAX(x++,y++);
printf("x = %d,y = %d,z = %d",x,y,z);
return 0;
}
===============================================
PS E:\project_C> cd "e:\project_C\test\" ; if ($?) {
gcc test.c -o test } ; if ($?) {
.\test }
x = 6,y = 10,z = 9
14.2.5 Naming conventions
14.2.6 #undef

14.2.7 Command line definition
14.3 Conditional compilation
#include<stdio.h>
#define DEBUG 1
int main()
{
#if DEBUG
printf("DEBUG is 1 \n");
#endif
return 0;
}
===========================================
PS E:\project_C\test> cd "e:\project_C\test\" ; if ($?) {
gcc test.c -o test } ; if ($?) {
.\test }
DEBUG is 1
#include<stdio.h>
#define DEBUG1 0
#define DEBUG2 0
#define DEBUG3 1
int main()
{
#if DEBUG1
printf("DEBUG1 \n");
#elif DEBUG2
printf("DEBUG2 \n");
#elif DEBUG3
printf("DEBUG3 \n");
#endif
return 0;
}
=================================================
PS E:\project_C\test> cd "e:\project_C\test\" ; if ($?) {
gcc test.c -o test } ; if ($?) {
.\test }
DEBUG3
14.3.1 Is it defined
#include<stdio.h>
// #define symbol 0
#define X 0
#define ABC 0
#define BCD 0
int main()
{
// #if defined(symbol)
// printf("symbol has been defined");
// #endif
// #ifdef symbol
// printf("symbol has been defined");
// #endif
// #if !defined(symbol)
// printf("symbol has not been defined");
// #endif
// #ifndef symbol
// printf("symbol has been defined");
// #endif
#if X>0||(defined(ABC) &&defined(BCD))
printf("X>0||(defined(ABC) &&defined(BCD))");
#endif
return 0;
}
14.3.2 Nested instruction
#include<stdio.h>
#define OS_UNIX 0
#define OPTION2 0
int main()
{
#if defined(OS_UNIX)
#ifdef OPTION1
printf("OPTION1");
#endif
#ifdef OPTION2
printf("OPTION2");
#endif
#elif defined(OS_MSDOS)
#ifdef OPTION2
printf("OPTION2");
#endif
#endif
return 0;
}
=============================================
PS E:\project_C\test> cd "e:\project_C\test\" ; if ($?) {
gcc tempCodeRunnerFile.c -o tempCodeRunnerFile } ; if ($?) {
.\tempCodeRunnerFile }
OPTION2
14.4 File contains
14.4.1 The function library file contains
14.4.2 The local file contains
14.4.3 Nested files contain
//test.h
#ifndef _test_H
#define _test_H 1
#endif
========================================================
//test.c
#include<stdio.h>
#include "test.h"
int main()
{
printf("%d",_test_H);
return 0;
}
============================================
PS E:\project_C> cd "e:\project_C\test\" ; if ($?) {
gcc test.c -o test } ; if ($?) {
.\test }
1
14.5 Other instructions
#include<stdio.h>
// #define OPTION_A 1
int main()
{
#if defined(OPTION_A)
printf("123");
#else
#error no defined
#endif
return 0;
}
==============================================
PS E:\project_C\test> cd "e:\project_C\test\" ; if ($?) {
gcc test.c -o test } ; if ($?) {
.\test }
test.c: In function 'main':
test.c:9:10: error: #error no defined
#error no defined
#include<stdio.h>
#line 10 "string"
// The first 10 That's ok
int main() // The first 11 That's ok
{
// The first 12 That's ok
printf("%d %s",__LINE__,__FILE__); // The first 13 That's ok
return 0;
}
===========================================
PS E:\project_C\test> cd "e:\project_C\test\" ; if ($?) {
gcc test.c -o test } ; if ($?) {
.\test }
13 string
14.6 summary
14.7 Summary of warnings
14.8 Summary of programming tips
边栏推荐
- CCF 201803_ 1 jump jump
- [deserialization vulnerability-02] principle test and magic method summary of PHP deserialization vulnerability
- Differences between JS map and foreach
- Experience of redis deepwater area -- Interview reference
- JMeter runtime controller
- 运算放大器 —— 快速复苏笔记[壹](参数篇)
- Tensor and numpy convert "suggested collection" to each other
- Cgo+gsoap+onvif learning summary: 9. Go and C conduct socket communication and onvif protocol processing
- 使用Prometheus+Grafana实时监控服务器性能
- Semaphore详解
猜你喜欢

JPS has no namenode and datanode reasons

动态内存管理

字符串——344.反转字符串

Chapter 1 Introduction

Ctfshow ThinkPHP topic 1

JMeter while controller
What is cloud native? Why is cloud native technology so popular?

Shengxin weekly issue 37

The art of management - driving software R & D efficiency through leadership

【网络空间安全数学基础第9章】有限域
随机推荐
Top and bottom of stack
Collision, removal and cleaning
Database operation through shell script
哈希——15. 三数之和
栈顶与栈底
In kuborad graphical interface, operate kubernetes cluster to realize master-slave replication in MySQL
Introduction to Devops and common Devops tools
Stack Title: basic calculator II
【Markdown语法高级】让你的博客更精彩(四:设置字体样式以及颜色对照表)
2 万字详解,吃透 ES!
Literature record (part109) -- self representation based unsupervised exemplar selection in a union of subspaces
JMeter while controller
JMeter if controller
Source code analysis sentry user behavior record implementation process
String - Sword finger offer 05. replace spaces
Oracle 11.2.0.4 ASM single instance does not start automatically with system startup
运算放大器 —— 快速复苏笔记[贰](应用篇)
Import the data in MariaDB into columnstore
Shell script
Shell Scripting tips









































