C Notes _C language environment, compiler, pre-treatment

1, environment

gcc -v                 //查看环境变量
gcc 同 g++
gcc main.c -o main.exe
gcc main.c             //默认生成a.exe

2, compiled

Pretreatment: gcc -E main.c -o main.i
compiler: gcc -S main.i -o main.s // while doing grammar checker
compilation: gcc -c main.s -o main.o
link: gcc main .o -o main.exe

3,4996 error

// 主要存在于scanf,strcpy,sprintf等
#pragma warning(disable:4996)          //防止4996错误
#define _CRT_SECURE_NO_WARNINGS        //防止4996错误

4, pretreatment

4.1 Macro

  • Macro definition

Description:
1) general macro name in uppercase in order to distinguish variables;
2) macro definition can be a constant, expression or the like;
3) macro definition syntax checking is not only the source code after compiling macros will be expanded error;
4) is not a C language macro definition, is not the end row semicolon;
5) macro name valid range is defined from the origin to the end of the file;
6) may be terminated by the scope defined macro commands #undef;
7) in a macro definition, can reference macro name defined;
8) with parentheses each parameter, defined and enclosed living whole macro.

  • Macro Constants
    • #define PI 3.14
    • Special macro definition
      file containing macros __FILE__ source file name
      __LINE__ line number of the macro row of
      __DATE__ code compilation date
      __TIME__ code compilation time
  • Macro Functions
#define MYADD(x,y)  ((x)+(y))
/*
1)宏的名字中不能有空格,但是在替换的字符串中可以有空格。ANSI C允许在参数列表中使用空格;
2)用括号括住每一个参数,并括住宏的整体定义。
3)用大写字母表示宏的函数名。
4)如果打算宏代替函数来加快程序运行速度。假如在程序中只使用一次宏对程序的运行时间没有太大提高。
*/

4.2 Conditional Compilation

  • In general, source code compilation to participate in all rows. But sometimes hope when only part of the source lines in certain conditions compilation, that this part of the compiler source code line specifies the conditions.
  • Tests for the presence
#define 标识符
...
#ifdef 标识符
    程序段1
#else
    程序段2
#endif

#define DEBUG
#ifdef DEBUG

void func()
{
    printf("debug版本调用\n");
}

#else
void func()
{
    printf("release版本调用\n");
}
#endif
  • Testing does not exist
#define 标识符
...
#ifndef 标识符
    程序段1
#else
    程序段2
#endif
  • According to the definition of the expression
#if 表达式
    程序段1
#else
    程序段2
#endif

#if 1
void func()
{
    printf("debug1版本调用\n");
}
#else
void func()
{
    printf("debug2版本调用\n");
}
#endif

Guess you like

Origin www.cnblogs.com/chungeyuan/p/11410243.html