基本定义
#include <stdio.h>
*
#include <windows.h>
int main() {
Sleep(5000);
puts("hello, 尚硅谷~");
getchar();
return 0;
}
*/
#if _WIN32
#include <windows.h>
#elif __linux__
#include <unistd.h>
#endif
int main() {
#if _WIN32
Sleep(5000);
#elif __linux__
sleep(5);
#endif
puts("hello, 尚硅谷~");
getchar();
return 0;
}
宏定义介绍
#include <stdio.h>
#define M n*n+3*n
int main(){
int sum, n;
printf("k=%d", n);
printf("Input a number: ");
scanf("%d", &n);
sum = 3*M+4*M+5*M;
printf("sum=%d\n", sum);
getchar();
getchar();
return 0;
}
宏定义注意事项
带参宏定义
#include <stdio.h>
#define MAX(a,b) (a>b) ? a : b
int main(){
int x , y, max;
printf("input two numbers: ");
scanf("%d %d", &x, &y);
max = MAX(x, y);
printf("max=%d\n", max);
getchar();
getchar();
return 0;
}
带参宏定义和函数的区别
#include <stdio.h>
#include <stdlib.h>
#define SQ(y) ((y)*(y))
int main(){
int i = 1;
while(i<=5){
printf("%d^2 = %d\n", i-2, SQ(i++));
}
system("pause");
return 0;
}
C 语言预处理命令总结
宏定义 案例
|