 Integer
 Real
 Character
 String
 Decimal integer
◦ Consist of 0 through 9, +, -
 Octal integer
◦ Consist of 0 through 7, with a leading 0
 Hexadecimal integer
◦ Consist of 0 to 9, and a through f preceded by 0x or
0X
 The largest integer value is machine-dependent
 Qualifiers
◦ U or u: unsigned integer
◦ l or L: long integer
◦ UL or ul: unsigned long integer
◦ Short integer
 short is no longer than int and that long is no
shorter than int.
 Example2.1 Representation of integer constants on a 16-bit
computer.
#include<stdio.h>
main()
{
printf("Integer valuesnn");
printf("%d %d %dn",32767,32767+1,32767+10);
printf("n");
printf("Long integer valuesnn");
printf("%ld %ld %ldn",32767L,32767+1L, 32767+10L);
}
 (1) decimal point
◦ Consist of 0 through 9, . ,+ , -
◦ 3.14159, .94 , -.58 , +1.234
 (2)Exponential notation
mantissa e exponent
◦ .56e3 2.3e-3 -2.3E-3
◦ The exponent must be an integer number
 Suffixes f or F indicate a float constant
 L or l indicate a long double
 “Default values of constants” on page35
 Floating-Point Round-off Errors
 Take a number, add 1 to it, and subtract the
original number. What do you get? You get 1. A
floating-point calculation, may give another
answer:
 A character enclosed within ‘ ’
◦ ‘6’ ‘=‘ ‘;’ ‘ ‘
◦ Character constants have integer values, For example
◦ ‘a’ and 97
◦ ‘0’ and 48
 Backslash character constants
◦ Table2.5 on page28
◦ ‘ooo’ and ‘xhh’
 a sequence of characters enclosed in “ ”, for
example
◦ “hello” “34” “+()” “x” “n”
 “x” and ’x’
 A variable is data name that may be used to store
a data value.
◦ Variable names correspond to locations in the
computer's memory
◦ Every variable has a name, a type, a size and a value
◦ Whenever a new value is placed into a variable, it
replaces (and destroys) the previous value
◦ Reading variables from memory does not change them
 The declaration of variables must be done before
they are used.
 declaration form
data-type v1,v2,…,vn;
 for example
◦ int count;
◦ float sum;
◦ double ratio;
◦ char ch;
 To initialize a variable means to assign it a
starting, or initial, value.
 In C, this can be done as part of the declaration.
 char ch=‘ ’;
 int cows = 32,
 int dogs, cats = 94;

constants

  • 2.
     Integer  Real Character  String
  • 3.
     Decimal integer ◦Consist of 0 through 9, +, -  Octal integer ◦ Consist of 0 through 7, with a leading 0  Hexadecimal integer ◦ Consist of 0 to 9, and a through f preceded by 0x or 0X
  • 4.
     The largestinteger value is machine-dependent  Qualifiers ◦ U or u: unsigned integer ◦ l or L: long integer ◦ UL or ul: unsigned long integer ◦ Short integer
  • 5.
     short isno longer than int and that long is no shorter than int.
  • 6.
     Example2.1 Representationof integer constants on a 16-bit computer. #include<stdio.h> main() { printf("Integer valuesnn"); printf("%d %d %dn",32767,32767+1,32767+10); printf("n"); printf("Long integer valuesnn"); printf("%ld %ld %ldn",32767L,32767+1L, 32767+10L); }
  • 7.
     (1) decimalpoint ◦ Consist of 0 through 9, . ,+ , - ◦ 3.14159, .94 , -.58 , +1.234  (2)Exponential notation mantissa e exponent ◦ .56e3 2.3e-3 -2.3E-3 ◦ The exponent must be an integer number  Suffixes f or F indicate a float constant  L or l indicate a long double  “Default values of constants” on page35
  • 8.
     Floating-Point Round-offErrors  Take a number, add 1 to it, and subtract the original number. What do you get? You get 1. A floating-point calculation, may give another answer:
  • 9.
     A characterenclosed within ‘ ’ ◦ ‘6’ ‘=‘ ‘;’ ‘ ‘ ◦ Character constants have integer values, For example ◦ ‘a’ and 97 ◦ ‘0’ and 48  Backslash character constants ◦ Table2.5 on page28 ◦ ‘ooo’ and ‘xhh’
  • 10.
     a sequenceof characters enclosed in “ ”, for example ◦ “hello” “34” “+()” “x” “n”  “x” and ’x’
  • 11.
     A variableis data name that may be used to store a data value. ◦ Variable names correspond to locations in the computer's memory ◦ Every variable has a name, a type, a size and a value ◦ Whenever a new value is placed into a variable, it replaces (and destroys) the previous value ◦ Reading variables from memory does not change them
  • 12.
     The declarationof variables must be done before they are used.  declaration form data-type v1,v2,…,vn;  for example ◦ int count; ◦ float sum; ◦ double ratio; ◦ char ch;
  • 13.
     To initializea variable means to assign it a starting, or initial, value.  In C, this can be done as part of the declaration.  char ch=‘ ’;  int cows = 32,  int dogs, cats = 94;

Editor's Notes

  • #4 整型常量:三种表示方法 十进制整数:0-9,+,- 八进制整数:0-7,以0开头 十六进制整数:0-9,a-f,或A-F,以0x开头
  • #5 整数的范围:与机器有关 修饰符:整型数加后缀u或U表示无符号整型数;加后缀l 或L表示长整型
  • #6 设计一个程序,看一下整数越界会输出什么? (%d输出整数,%u输出无符号整数。)
  • #8 (1)十进制小数形式 (2)指数形式 Mantissa: 尾数 Exponent: 指数
  • #10 字符型常量具有整数值,为与其对应的ASCII码 ‘\ooo’表示三位八进制数,
  • #13 变量的声明 变量必须先声明,后使用 声明形式: 变量类型 变量1,变量2,…,变量n;
  • #14 变量初始化 可以在声明变量的同时进行初始化