Indispensable Windows Native (2), windowsnative
[Download source code]
Indispensable Windows Native (2)-C language: constant, variable, basic data type
Author: webabcd
Introduction
Indispensable C language for Windows Native
- Constant
- Variable
- Basic Data Type
Example
CDataType. h
#ifndef _MYHEAD_DATATYPE_#define _MYHEAD_DATATYPE_ #ifdef __cplusplus extern "C"#endif char *demo_cDataType();char *demo_constant();char *demo_integer();char *demo_float();char *demo_char();#endif
CDataType. c
/** Constants, variables, and basic data-related knowledge points ** allocate memory when defining variables, for example, * int I; // memory * I = 100 is allocated during definition. // assign a value to the memory. ** Note: * 1. Declaring a variable indicates describing the variable type to the compiler, and allocate a bucket * 2. After declaring a variable, assign a value to it if necessary. unassigned variables cannot use */# include "pch. h "# include" cDataType. h "# include" cHelper. h "# define CONSTANT1 100 # define CONSTANT2 3.14 // The floating point type defined in a constant is double # define CONSTANT3" constant "char * demo_cDataType () {char * str1 = demo_constant (); char * str2 = demo_integer (); Char * str3 = demo_float (); char * str4 = demo_char (); // float a = 3.14; int B =; // automatically convert int c = (int) a; // forcibly convert return str_concat4 (str1, str2, str3, str4);} // constant knowledge point char * demo_constant () {char * str1 = int_toString (CONSTANT1); char * str2 = float_toString (CONSTANT2); char * str3 = CONSTANT3; // This is the const constant const int I = 0; // The difference between a const constant and a # define constant is as follows:/* if it is # define x 1 + 2, then the table 3 * x * 4 is equivalent to 3*1 + 2*4, the result is 11. If the expression is const int x = 1 + 2, 3 * x * 4 is equivalent to 3*3*4, the result is 36. We can see that a memory allocation is performed each time we reference # define. The const only allocates one memory at the time of definition. Strictly speaking, const is an immutable variable, in the ansi c standard, the following statement is wrong: const int n = 10; int ary [n]; because the ansi c standard specifies that constants must be used to define the length of an array, the essence of const is a variable (although immutable) */return str_concat3 (str1, str2, str3);} // integer knowledge point char * demo_integer () {// for 32-bit systems, it occupies 4 bytes (range:-2147483648 ~ 2147483647) int I = 0; // for 32-bit systems, it usually occupies 2 bytes (as long as the standard requirement is not greater than int) short si = 0; // for 32-bit systems, it usually occupies 8 bytes (as long as the standard requirement is not smaller than int) long li = 0L; // L or l represents a long integer // for 32-bit systems, it occupies 4 bytes of unsigned ui = 0U; // U or u represents an unsigned integer (its range is: 0 ~ 4294967295) // for 32-bit systems, it usually occupies 8 bytes (as long as the standard requirement is not smaller than int) unsigned long ul = 0UL; // UL or ul indicates the unsigned long integer. // It starts with "0" and ends with octal. The decimal result is 13 int x = 015; // The hexadecimal format starts with "0X". The decimal result is 65535 int y = 0 XFFFF. // demonstrate what is data overflow? // Assume that short occupies 2 bytes and its maximum value is 32767. Then, if we add 1, it will be-32768. The principle is as follows: // The binary value of signed 32767 is 0111111111111111, after 1 is added, it becomes "1000000000000000", that is, "-32768" indicates short abc = 32767; abc ++; //-32768 // Note: For signed data, "1st" indicates the symbol on the left, 0 indicates positive, 1 indicates negative // determines whether a number is signed if (ui> = 0 &&~ Ui> = 0 )//~ Return "unsigned int is unsigned"; else return "bug";} // floating point knowledge point char * demo_float () {// 4-byte float f = 3.14F; // F or f indicates the floating point type // 8-byte double f2 = 3.14F; // 16-byte long double f4 = 3.14F; // exponential form, that is, the scientific representation float x = 0.00314E3; // equal to the 3rd power of 0.00314*10 float y = 314E-2; // equal to the 3rd power of 314*10-2 (the negative power is the reciprocal of its positive power, that is, the value of 10 to the power of 2 is: 1/100) // use scientific notation to understand the storage form of floating point data in the memory. // In the binary scientific notation, S = M * 2 ^ N consists of three parts: Symbol bit + level code (N) + ending number (M) // For floa T-type data. The binary value is 32 bits, where the symbol bit is 1 bits, the order code is 8 bits, and the tail number is 23 bits. For double-type data, the binary value is 64 bits, and the symbol bit is 1 bits, the order code is 11 bits and the ending number is 52 bits. Return float_toString (y);} // character-type knowledge point char * demo_char () {// mark the character type char c = 'X' with single quotation marks '; // common escape characters // \ r-press ENTER // \ n-line feed // \ t-tabulation // \ B-return space //\\-\//\'- single quotation marks // \ "-double quotation marks /// an ASCII octal value followed, then the result is the corresponding character char * str1 = "\ 101"; // A // \ x followed by an ASCII hexadecimal value, the result is the corresponding character char * str2 = "\ x41"; // A // The storage format of the character in the memory: save the corresponding ASCII code value // mark the string type with double quotation marks. The string is actually the character array // The End mark of the string is '\ 0 ', that is, 0 in the ASCII code, that is, NULL // that is, the space occupied by the string is: the number of bytes of the string + 1 char str [] = "abc "; // The memory format is abc \ 0, which occupies 4 bytes return str_concat2 (str1, str2 );}
OK
[Download source code]