Indispensable Windows Native (4), windowsnative
[Download source code]
Indispensable Windows Native (4)-C language: preprocessing command, input, output
Author: webabcd
Introduction
Indispensable C language for Windows Native
- Preprocessing command
- Input
- Output
Example
1. Pre-processing commands
CPreprocessor. h
#ifndef _MYHEAD_PREPROCESSOR_#define _MYHEAD_PREPROCESSOR_ #ifdef __cplusplus extern "C"#endif char *demo_cPreprocessor();#endif
CPreprocessor. c
/** Pre-processing command ** starts with "#". The pre-processing command does not end ), it refers to the work done before compilation */# include "pch. h "# include" cPreprocessor. h "# include" cHelper. h "// # define-macro definition command. during compilation and preprocessing, all the" macro names "in the program are replaced by the strings in the macro definition, this is called "macro replacement" or "macro expansion" # define PI 1000 // # define identifier string # define s pi * r // One macro can also reference another macro # define S2 (r) PI * r // macro can contain parameters # define PI 3.14 // If the macro name is duplicated, the latter will replace the previous # define INTEGER int // macro definition. Of course, the data type can also be defined, because the essence is to replace one string with another string # define ISW INDOWS 1 # define X (n) n = 0? 0 \:-1 // a long # define can be divided into multiple rows with "\". // Add another sentence here, if a single line comment ends with "\", the next line is also noted. \ In addition, ansi c only supports "/**/" comments, the "//" comment/* is not supported, the essence of macro definition is to replace another string with one string. If it is # define x 1 + 2, the expression 3 * x * 4 is equivalent to 3*1 + 2*4, the result is 11. If it is # define x (1 + 2), the expression 3 * x * 4 is equivalent to 3*(1 + 2) * 4, the result is 36 * // * # include-File Inclusion command # include "stdio. h "Double quotation marks indicate that the file is first searched in the current directory (absolute path and relative path are supported). If not found, search for # include <math. h> use angle brackets to search for the directory containing files, rather than the current directory of the file: project-> right-click-> properties-> Configuration properties-> VC ++ directory-> contains directory */char * demo_cPreprocessor () {INTEGER r = 2; // macro scale is equivalent to int r = 2; float x = S; // macro scale is equivalent to 3.14 * r, that is, 3.14*2*2 float y = S2 (2); // macro expansion is equivalent to 3.14*2*2 int z = X (100 ); //-1 // The Conditional compilation Commands include: # ifdef, # else, # endif, # ifndef, # if # ifdef PI // if the macro definition defines PI (ifdef is short for if defined); # else // if not, do not # else; # endif // # ifdef and # endif must be paired # ifndef PI // if no PI is defined in the macro definition (ifndef is short for if not defined ); # else // if not needed, do not # else; # endif // # ifndef and # endif must be paired # if ISWINDOWS // if the macro definition ISWINDOWS is non-zero, it is true (ISWINDOWS is an integer); # else // if not needed, do not # else; # endif // # if and # endif must be paired with return float_toString (y);} // after which the macro definition of PI is no longer available (undef is short for undefine) # undef PI
2. Input and Output
CIO. h
#ifndef _MYHEAD_IO_#define _MYHEAD_IO_ #ifdef __cplusplus extern "C"#endif char *demo_cIO();#endif
CIO. c
/** Input and Output */# include "pch. h "# include" cIO. h "# include" cHelper. h "void io_printf (); void io_scanf (); void io_getchar (); char * demo_cIO () {// format the output printf io_printf (); // format the input scanf io_scanf (); // string input getchar () io_getchar (); return "read the code and comment" ;}// format the output printfvoid io_printf () {// printf-print formatted. The following lists only common usage // the return value of printf: the actual number of output characters: int a = 88; float B = 3.14159; char * c = "webabcd wanglei ";// % D-output a signed integer in decimal format // %. 2f-float output, 2 digits after decimal point // %. 7 s-output as a string, take the first 7 digits of printf ("% d, %. 2f, %. 7s \ n ", a, B, c); // output result: 88, 3.14, the returned value of webabcd // printf is 18 // % o-output an unsigned integer in octal format // % # o-output an unsigned integer in octal format, prefix 0 printf ("% o, % # o \ n", a, a); // output result: 130,013 0 // % x or % X-output an unsigned integer in hexadecimal format // % # x-output an unsigned integer in octal format, add the prefix 0x // % # X-to output the unsigned integer in octal format, and add the prefix 0X printf ("% x, % # x, % # X \ n ", a, a, a); // output result: 58, 0x58, 0X58 // % 10.2f-float-type output, two digits after the decimal point. The display width must be at least 10 characters. If not, enter printf ("% 10.2f \ n", B) with spaces ); // output result: 3.14 (with 6 Leading spaces)/** type description: * % d/% I-integer * % o-Unsigned octal character * % u-Unsigned decimal x % X/% x-Unsigned hexadecimal character * % f-floating point type * % e/% E-scientific notation * % g/% G-select a shortest form in % f and % e/% E * % c-character * % s/% s-output by string, until '\ 0' * % p-outputs pointer in hexadecimal format * %-% *** there are two other length formats: * h-outputs by short integer, for example: % hd-output by short integer; printf ("% hd", 32769) output is-32767 * l-output by long integer, for example: % Lf-output by double; % ld-long int; % lld-long int * // format the input scanfvoid io_scanf () {// scanf-scan formatted, the following lists the common usage // return values of printf: the number of successfully received value assignment parameters: int a; float B; char c [100]; // % 2 s-indicates that only the first two digits of the input string are used. // if no character exists between two formatting parameters, scanf ("% d % f % 2 s", & a, & B, c) can be separated by space, TAB, or press Enter ); // enter 1 3.14 webabcd and the result is: the value of a is 1, and the value of B is 3.14, the first three elements of c are 'w''e'' \ 0' // the return value of scanf is 3 // if there is a character between two formatting parameters, the interval is based on the specified characters. Scanf ("a = % d, B = % f, c = % s", & a, & B, c); // input: a = 1, B = 3.14, c = webabcd} // string input getchar () void io_getchar () {// when the program calls the getchar () function, the program waits for the user to press the key, user-entered characters are stored in the keyboard buffer until you press enter (the carriage return character is also placed in the buffer) // when the user presses enter, getchar () the function starts to read one character from the keyboard buffer every time until the characters in the buffer are read/* char c = NULL; while (c = getchar ())! = '\ N') {printf ("% c", c);} * // getch (), getche () and other conio. h, but it does not exist in linux, so it will not be written}
OK
[Download source code]