Learning C language third day

It's not a document or a tutorial. It's just supervising myself to learn C language by clocking in and recording only some small notes. If there is any mistake, thank you very much for pointing it out! ! !

One, the compilation process

(Source code) -> [preprocessing] -> (source code after macro replacement) -> [compiler] -> (intermediate file) -> [linker] -> (executable program)

Two, define the header file yourself

Introduce your own defined header file

  • You can use for example #inclue"…/head/xx.h", using "" will give priority to searching the directory of the current source file
  • For example, use the defined B file in the A file to directly import the B header file, while the B header file only has declarations, the B's .c file is the specific implementation, and the B's .c file introduces the B header file
  • The reason why the .c file of B can be found through the header file of B is because the .c file is added to the project

Three, macro function

  • Macros with parameters become macro functions
  • Note that the macro function does not operate, but expands in place, so the macro function generally uses () to ensure that the passed expression will not be confused, but this is not a panacea. So when using a macro function, make sure that the expression passed in during the call does not affect the subsequent
  • The parameters and return value of the
    macro function have no type requirements. Macro function case
#include <stdio.h>
#define MAX(a, b) (a) > (b) ? (a) : (b)
#define IS_HEX_CHARACTER(ch) \
((ch) >= '0' && (ch) <= '9') || \
((ch) >= 'A' && (ch) <= 'F') || \
((ch) >= 'a' && (ch) <= 'f')
int Max(int a, int b) {
    
    
  return a > b ? a : b;
}
int main() {
    
    
  int max = MAX(1.0, 3);
  int max2 = MAX(1, MAX(3, 4));
  //函数是会进行计算之后赋值,而宏函数是就地展开
  int max3 = Max(1, Max(3, 4));
 //这里传入的表达式就是对后续结果照成了影响
  int max4 = MAX(max++, 5);

  printf("max2: %d\n", max2);

  printf("is A a hex character? %d\n", IS_HEX_CHARACTER('A'));
  return 0;
}

Fourth, conditional compilation

  • ifndef: If it is not defined, it is used to determine whether a macro is defined
  • ifdef: If defined, used to determine whether a macro is defined
  • if: if, used to determine whether a macro is defined or an expression, etc.
  • Examples of if are: #if define(MACRO) ==> #ifdef MACRO
  • endif: end

Code writing in c++ and c environment

#ifdef __cplusplus
extern "C" {
    
    
#endif

//.....

#ifdef __cplusplus
};
#endif

Determine the current version of c

#if __STDC_VERSION__ >= 201112
  puts("C11!!");
#elif __STDC_VERSION__ >= 199901
  puts("C99!!");
#else
  puts("maybe C90?");
#endif

You can also determine the current operating system, etc...

Five, custom implementation of the printing function

#include <stdio.h>
#include <stdarg.h>

void Printlnf(const char *format, ...) {
    
    
  va_list args;
      va_start(args, format);
  vprintf(format, args);
  printf("\n");
      va_end(args);
}

// "Hello ""world" ==> "Hello world"
// __FILE__
// __LINE__
// __FUNCTION__
// (../05.printlnf.c:20) main :

#define PRINTLNF(format, ...) printf("("__FILE__":%d) %s : "format"\n",__LINE__, __FUNCTION__, ##__VA_ARGS__)

#define PRINT_INT(value) PRINTLNF(#value": %d", value)

int main() {
    
    
  int value = 2;
  Printlnf("Hello World! %d", value);
  PRINTLNF("Hello World! %d", value);
  PRINTLNF("Hello World!");

  PRINT_INT(value); // value: 2
  int x = 3;
  PRINT_INT(x);

  PRINT_INT(3 + 4);
  return 0;
}

Six, mess

  • If you want to add the header file to the project in Clion, write include_direcories ("the name of the file where the header file is located") in cmake.txt

Guess you like

Origin blog.csdn.net/qq_45549336/article/details/112744523