Skip to content

Instantly share code, notes, and snippets.

@qiaohaijun
Created June 14, 2017 08:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save qiaohaijun/08eab5fb12e827a4e67d52b1b758cea2 to your computer and use it in GitHub Desktop.
Save qiaohaijun/08eab5fb12e827a4e67d52b1b758cea2 to your computer and use it in GitHub Desktop.

ANSI C标准中有几个标准预定义宏(也是常用的):

  1. LINE:在源代码中插入当前源代码行号
  2. FILE:在源文件中插入当前源文件名
  3. DATE:在源文件中插入当前的编译日期
  4. TIME:在源文件中插入当前编译时间
  5. STDC:当要求程序严格遵循ANSI C标准时该标识被赋值为1;
  6. __cplusplus:当编写C++程序时该标识符被定义。
#include <stdio.h>

int main(void) {
    int answer;
    short x = 1;
    long y = 2;
    float u = 3.0;
    double v = 4.4;
    long double w = 5.54;
    char c = 'p';;

    // __DATE__, __TIME__, __FILE__, __LINE__ 为预定义宏
    printf("Date : %s\n", __DATE__);
    printf("Time : %s\n", __TIME__);
    printf("File : %s\n", __FILE__);
    printf("Line : %d\n", __LINE__);
    printf("Enter 1 or 0 : ");
    scanf("%d", &answer);

    // 这是一个条件表达式
    printf("%s\n", answer?"You sayd YES":"You said NO");

    // 各种数据类型的长度
    printf("The size of int %d\n", sizeof(answer));
    printf("The size of short %d\n", sizeof(x));
    printf("The size of long %d\n", sizeof(y));
    printf("The size of float %d\n", sizeof(u));
    printf("The size of double %d\n", sizeof(v));
    printf("The size of long double %d\n", sizeof(w));
    printf("The size of char %d\n", sizeof(c));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment