[Original] The simplest C language, error output, log printing and C predefined macros

Checking whether a function call made an error mainly depends on the return value, so you can effectively check the return value:

#define NO_ERR 0

void
check_err(const int stat, const int line, const char *file) {
    if (stat != NO_ERR) {
        (void)fprintf(stderr, "line %d of %s: %s\n", line, file, nc_strerror(stat));
        exit(1);
    }
}

int stat = fun();
check_err(stat, __LINE__, __FILE__);

 

In addition, which macros can be used in C language?

 

Standard C language preprocessing requires the definition of certain object macros. The name of each predefined macro begins and ends with one or two underscore characters. These predefined macros cannot be undefined (#undef) or redefined by the programmer. The following predefined macro table was copied by me.
__LINE__ The line number of the current program line, expressed as a decimal integer constant
__FILE__ The current source file name, indicating a string constant
__DATE__ converted calendar date, expressed as a string constant in the form of Mmm dd yyyy, Mmm is generated by asctime.
__TIME__ The converted time represents a string constant of the form "hh: mm: ss", which is generated by asctime. (Asctime seems to refer to a function)
__STDC__ The editor is ISO compatible when implementing a decimal integer constant
__STDC_VERSION__ How to implement compound C89 integer 1, the value of this macro is 19940SL; if the implementation conforms to C99, the value of this macro 199901L; otherwise, the value is undefined
__STDC_EOBTED__ (C99) is 1 when implemented as a host implementation, and 0 is implemented as an independent implementation
__STDC_IEC_559__ (C99) floating point is defined as 1 when implementing the composite IBC 60559 standard, otherwise the value is undefined__STDC_IEC_559_COMPLEX__
(C99) The complex number operation is defined as 1 when implementing the compound IBC 60559 standard, otherwise the value is undefined
__STDC_ISO_10646__ (C99) is defined as a long integer constant, yyyymmL represents the wchar_t value compound ISO 10646 standard and the revision supplement of the specified year and month, otherwise the value is undefined

C ++ also defines __cplusplus

__FILE__, __LINE__ and __DATE__ in C language are all in the header file #include <stdio.h>

If the compiler is not standard, it may only support a few of the above macro names, or not at all. Remember that the compiler may also provide other predefined macro names.
The __LINE__ and __FILE__ macros indicate that the #line directive can change its value. Simply put, when compiling, they contain the current line number and file name of the program.
The meaning of __STDC__ macro is defined at compile time. In general, if __STDC__ is defined, the compiler will only accept standard C / C ++ code that does not contain any non-standard extensions. If the implementation is standard, the macro __STDC__ contains the decimal constant 1. If it contains any other number, the implementation is non-standard.
__cplusplus The compiler that is consistent with standard c ++ defines it as a value that contains at least 6 values. Compilers that are inconsistent with standard c ++ will use values ​​with 5 digits or less.

 

 


Specific examples of commonly used macros are as follows:

The __LINE__ in C language is used to indicate the position information of the line statement in the source file, for example:


#include <stdio.h>

 

main ()
{
printf ("% d \ n", __ LINE__);
printf ("% d \ n", __ LINE__);
printf ("% d \ n", __ LINE__);
};
This program is compiled with gcc on Linux, Compile under Windows VS2013 can pass, the execution results are:
7

8

9

 


You can also reset the value of __LINE__ through the statement #line, for example:
#include <stdio.h>


#line 200 // Specify __LINE__ of the next line as 200
main ()
{
printf ("% d \ n", __ LINE__);
printf ("% d \ n", __ LINE__);
printf ("% d \ n" , __ LINE__);
}; The
output after compilation is:
202
203
204

 

 


The __FILE__ in C language is used to indicate the file of the source file where the statement of this line is located.

E.g:


#include <stdio.h>
int main ()
{
printf ("% s \ n", __ FILE__);
}
compile and generate a.out in gcc, the output result after execution is:
test.c
compile and execute result under VS2013 of windows For:
d: \ work \ c & c ++ \ project \ project1 \ project1 \ main.cpp
-------------------------------- -------------------------------------------------- -------------------------------------------------- --------------
C language __DATE__ and __TIME__ said time and date
#include <stdio.h>
void main (void)
{
    printf ("% s \ n", __DATE__);
    printf ("% s \ n", __ TIME__);
    getch ();
}
Result:
Nov 24 2017
21:15:27

__STDC__ is a predefined macro. When it is defined, the compiler will compile your c program according to ansic standards.
__cplusplus is used to define whether it is a C ++ compiler
# include <stdio.h>
int main (void)
{
 #ifdef _cplusplus
  printf ("C ++ \ n");
 #endif
 
 #ifdef __STDC__
  printf ("C \ n");
 #endif
 return 0;
} The
output is:
C,
but if you define _cplusplus
#include <stdio.h>
#define _cplusplus
int main (void)
{
 #ifdef _cplusplus
  printf ("C ++ \ n");
 #endif
 
 #ifdef __STDC__
  printf ("C \ n");
 #endif
 return 0;
}
Then the output is:
C ++
C

In addition, gcc also supports __func__, and __FUNCTION__, which indicates the function, but this keyword is not supported by vc6.0 under windows, for example as follows
#include <stdio.h>
void main (void)
{
    printf ("% s \ n ", __ FUNCTION__);
    printf ("% s \ n ", __ func__);
}
The output result after compilation is
main
main

Note: "#line", "__LINE__", "__FILE__" and "__func__" are all case sensitive.

Guess you like

Origin www.cnblogs.com/lyggqm/p/12761356.html