Fall in love with C: note--"ansi"

Source: Internet
Author: User
Tags define local variadic

1 cc='cc-d_posix_c_source=200809l-g-ansi-std=c99-wall-wextra'2  gcc='gcc-d_posix_c_source=200809l-ansi-std=c99-wall-wextra-o2-march=haswell'
  • The Register storage class is used to define local variables that are stored in registers rather than in RAM. This means that the maximum size of a variable is equal to the size of the register (usually a word), and it cannot be applied to a unary ' & ' operator (because it has no memory location), and registers are used only for variables that require quick access, such as counters, and it should also be noted that defining ' register ' Does not mean that variables will be stored in registers, which means that variables may be stored in registers, depending on hardware and implementation constraints;
  • The static storage class instructs the compiler to maintain the existence of a local variable for the lifetime of the program without creating and destroying it every time it enters and leaves the scope, so using the static modifier local variable can maintain the value of the local variable between function calls, and when static modifies the global variable, The scope of the variable is limited to the file in which it is declared;
  • The extern modifier is typically used when two or more files share the same global variable or function;
  • When a local variable is defined, the system does not initialize it, you must initialize it yourself, and when you define a global variable, the system automatically initializes it;
  • A typedef is interpreted by the compiler, and #define statements are processed by the precompiled compiler;
    • intGetCharvoid) function to read the next available character from the screen and return it to an integer; FILE*fopen (Const Char* FileName,Const Char*mode);intFclose (FILE *FP);intFPUTC (intC, FILE *FP);intFputsConst Char*s, FILE *FP);intfprintf (FILE *FP,Const Char*format, ...) intFGETC (FILE *FP);Char*fgets (Char*buf,intN, FILE *FP); fgets () reads n from the input stream that the FP points to - 1 characters; it will copy the read string to the buffer buf, and append a \0  character to terminate the string at the end;intFSCANF (FILE *FP,Const Char*format, ...) function to read a string from a file, but when the first space character is encountered, it stops reading, and when the string is read, scanf () stops reading as soon as it encounters a space  . "This wastest" is a three string for scanf ();
    • The following two functions are used in binary input and output:
      • size_t fread (void *ptr, size_t size_of_elements, size_t number_of_elements, FILE *a_file); size_t fwrite c4>constvoid *ptr, size_t size_of_elements, size_t number_of_elements, FILE *a_file);   -usually an array or struct;
  • c Preprocessor (c preprocessor) abbreviated to CPP; some important preprocessor directives are listed below:
    • #define Defining Macros
    • #include contains a source code file
    • #undef cancel a defined macro
    • #ifdef If the macro is already defined, returns the true
    • #ifndef If the macro is not defined, returns the true
    • #if if the given condition is true, compile the following code
    • #elif/* Optional * / If the previous #if the given condition is not true and the current condition is true, compile the following code
    • #endif end a #if ... #else conditional compilation block
  • Scheduled macros: ANSI C defines a number of macros that you can use in programming, but you cannot modify these predefined macros, and the preprocessor defined operator is used in constant expressions to determine whether an identifier has been defined with #define, or if the specified identifier is already defined. The value is true (not 0), otherwise false (0);
    • __date__ the date when the program was compiled, a character constant expressed in "MMM DD YYYY" format;
    • __time__ when the program compiles, a character constant expressed in "HH:MM:SS" format
    • __file__ this will contain the current file name, a string constant
    • __line__ this will contain the current line number, a decimal constant
  • The C language provides the perror () and strerror () functions to display text messages related to errno, usually with a value exit_success when the program successfully finishes an operation and exits normally, where exit_success is a macro, defined as 0; If there is an error condition in the program, exit the program with the status value Exit_failure, which is defined as-1;
    • The Perror () function displays the string you passed to it, followed by a colon, a space, and a textual representation of the current errno value;
    • The strerror () function returns a pointer to the text representation of the current errno value;
  • variadic functions: You need to use the Stdarg.h header file, which provides functions and macros that implement Variadic functions, as follows:
    • Define a function, the last argument is an ellipsis, the second parameter (the one preceding the ellipsis) is of type int, which indicates the number of arguments, and the previous position can still define the general parameters;
    • Create a va_list type variable in the function definition that is defined in the Stdarg.h header file;
    • Use the int parameter of the function and the Va_start macro to initialize the va_list variable as a list of arguments;
    • Use VA_ARG macros and va_list variables to access each item in the parameter list in sequence;
    • Use the macro va_end to clean up the memory assigned to the va_list variable;
      • Include <stdarg.h>void func (charint  num, ...) {    va_list test;    Va_start (test, num);    Va_arg (test, type); / **    /va_end (test);}
  • dynamic allocation of memory functions;
    • void *calloc (int n, int  size); Allocates an array object that contains n elements, each with a size of  size  bytes, and initializes all elements to 0;
    • Span style= "font-size:12px" >void *malloc (int n);  returns
    • void *realloc (void *address, int newsize);  reallocate memory, extend memory to  newsize, if the first parameter is null, same as malloc effect;
    • void free (void *address);  releases The block of memory pointed to by address;
  • In the C language, a string is actually an array of one-dimensional characters terminated with the null character '% ';
    • strcpy (s1, S2); Copy string s2 to string S1;
    • strcat (s1, S2); The connection string S2 to the end of the string S1;
    • Strlen (S1); Returns the length of the string S1, not including the trailing null character;
    • strcmp (s1, S2); Returns 0 if S1 and S2 are the same, if S1<s2 returns less than 0 if S1>S2 returns greater than 0, compared by ANSI encoding;
    • STRCHR (s1, ch); The character lookup function returns a pointer to the position where the character Ch first appeared in the string S1;
    • Strstr (s1, S2); A string lookup function that returns a pointer to the position where the string S2 first appeared in the string S1;

Fall in love with C: note--"ansi"

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.