08 Constant

1, the constant is a fixed value, can not be changed during program execution, these fixed values, also called literal

2, the constant may be any of the basic data types, such as integer constants, floating-point constants, character constant, or string literals, there are enumerated constants

3, the constant value can not be changed after the definition

4, integer constant

  ① integer constants can be decimal, octal or hexadecimal constants, the prefix specified radix: 0x or 0X for hexadecimal, octal 0, without the prefix, the default decimal. May be an integer constant with a suffix suffixes U and L are combined, U denotes unsigned integer (unsigned), long integer L (long). Suffix can be uppercase, lowercase may be, U and L in any order.

  ② illustrated integer constant

    85 decimal

    0213 octal

    0x4b in hexadecimal

    30 integer

    30u unsigned integer

    30l long integer

    Unsigned long 30ul

5, floating-point constants

  ① floating constant consists of an integer part, a decimal point, the decimal part and an exponent part. You can use decimal floating point or exponential notation to represent constants

  ② floating constant For example:

    3.1415926 double constants

    Scientific notation 3.14159E-5

    3.1f float constants

6, character constants

  ① character constants are enclosed in single quotes, for example, 'x' may be stored in a variable of type char, character constant can be an ordinary character, an escape sequence may be (e.g., '\ t')

  ② For example:

    'X'  'A'   'b'  '1'  '\t'

7, string constants

  ① or string literal constant is enclosed in double quotes, a character string containing the character constants similar to ordinary characters, escape sequences, and universal characters, space delimiters may be used, to a very long string constants branch

  For example ②

    " hello world "

    "Beijing"

    " hello  \

      world "

. 1 #include <stdio.h>
 2  void main () {
 . 3      int N1 = 0213 ;   // octal 
. 4      int N2 = 0x4b ;   // hexadecimal 
. 5      char C1 = ' A ' ;
 . 6      char C2 = ' \ T ' ;
 . 7      char str1 [ 20 is ] = " Beijing Hello " ;   // "Beijing hello" string constant 
. 8      char str2 [ 100 ] = " Hello \
 9 world";    //等价  "hello world"
10 
11 
12     printf("n1=%d  n2=%d  str1=%s  str2=%s", n1, n2, str1, str2);
13 }

 

 8, constant definition

  ① constants defined in two ways:

    Use #define preprocessor

    Use const keyword

  ② # define preprocessor

    #Define preprocessor form defining constants

      #define constants were constant value 

. 1 #include <stdio.h>
 2  
. 3  #define PI 3.14   // define constant values PI constant value 3.14 
. 4  int main () {
 . 5      Double Area;
 . 6      Double R & lt = 1.2 ; // radius 
. 7      Area = R & lt * PI * R & lt ;
 . 8      the printf ( " area:%. 2F. " , area);
 . 9      return  0 ;   // before the main function is int, return value is needed, so in order to comply with the syntax, returns a zero 
10 }

    Print Results:

      Size: 4.52

  ③const keyword

    const declaration specifies the type of constant use

      const data type constant name = constant value; // That is a statement 

. 1 #include <stdio.h>
 2  
. 3  const  Double the PI = 3.14 ;   // time constant defined const, and needs a semicolon 
. 4  int main () {
 . 5      Double Area;
 . 6      Double R & lt = 1.2 ;
 . 7      Area = the PI * R & lt * R & lt;
 . 8      the printf ( " area:%. 2F. " , area);
 . 9 }

    Print Results:

      Size: 4.52

  And #define the difference ④const

    When defining constants const, tape type, tape type without DEFINE

    const is the time to compile and run the function, and define a role in the compilation of pre-processing stage

    define simply replaces, no type checking, simple string replacement can lead to boundary effects

    Const time constants can be debugged, it defines are not debug, mainly pre-compilation stage has been replaced, and there is no debugging it

    const can not be redefined, we can not define two the same, and define cancel the definition of a symbol through undef, then redefined

    can define with #ifdef #ifndef #endif to use, you can make the code more flexible, for example, we can enable or disable the debugging by #define.

  ⑤ error-prone example  

. 1 #include <stdio.h>
 2  
. 3  #define A. 1
 . 4  #define B + A. 3
 . 5  void main () {
 . 6      // DEFINE is a simple alternative 
. 7      Double D1 = A / B * . 3 ;
 . 8      the printf ( " .%. 2F D1 = " , D1);    // 10.00 
. 9  
10      analysis:
 . 11          D1 = A / A + . 3 * . 3 = . 1 + . 9 = 10 
12 is }
 1 #include<stdio.h>
 2 #define A 1
 3 #define B (A+3)
 4 void main() {
 5     double d1 = A / B * 3;
 6     printf("d1=%.2f", d1);   //0.00
 7 
 8     分析:
 9         d1=A/(A+3)*3=1/4*3=0*3=0
10 }

To improve the accuracy required to give 0.75, A = 1.0 to make

. 1 #include <studio.h>
 2  #define the DEBUG
 . 3  void main () {
 . 4  #ifdef the DEBUG
 . 5      the printf ( " OK, debug information " );
 . 6  #endif 
. 7  #ifndef the DEBUG
 . 8      the printf ( " Hello, additional information " );
 . 9  #endif 
10 }

Print Results:

  ok, debugging information

 

Guess you like

Origin www.cnblogs.com/shanlu0000/p/12339909.html