3.1 example program
platinum.c procedure
/* platinum.c -- your weight in platinum */ #include<stdio.h> int main(void) { float weight; /*Your weight*/ float value; /*Platinum price of equal weight*/ printf("Are you worth your weight in platinum?\n"); printf("Let's check it out.\n"); printf("Please enter your weight in pounds:"); /*Get user input*/ scanf("%f",&weight); /*Suppose the price of platinum is $1700 per ounce*/ value = 1700.0 * weight * 14.5833; printf("Your weight in platinum is worth $%.2f.\n",value); printf("You are easily worth that!If platinum prices drop,\n"); printf("eat more to maintain your value.\n"); return 0; }
3.11 new elements in the program
Integer type variable (int) / / previous examples appear
Floating point type variable (float) / / use% f in printf() to process floating point values
Scanf() function / / used to read keyboard input,%f indicating that scanf() function reads the input floating-point number, & weight is a variable that assigns the input value to the name. The scanf () function uses the & symbol to indicate where the variable was found
This procedure embodies interactivity
3.2 variable and constant data
3.3 data: data type keyword
Table 3.1 # data type keywords of C language
Keywords initially given by K & R | Keywords added by C90 standard | Keywords added by C99 standard |
int / / indicates integer type | signed | _ Bool Boolean (true or false) |
long | A variant that provides the basic integer type | _ Complex complex |
short | _ Imaginary imaginary number | |
unsigned | ||
char specifies letters and other characters (#% ¥ *), smaller integers | ||
float number with decimal point | void | |
double |
3.3.1 integer and floating point numbers
Floating point number: e notation 3.16E7 represents 3.16 * 10 ^ 7, and 7 is called the index of 10
3.4 basic data type of c voice
3.4.1 int type
- Declare int variable:
Declaration creation variable: int erns; int hogs,cows,goats;
Provided value: 1 cows = 112;
2. Get the value through the function (e.g. scanf()).
- initialize variable
Add the assignment operator (=) and the value to be assigned to the variable after the variable name
- int type constant
Integer constant or integer face value
- Print int value
%d indicates that printf () prints and displays a value in integer type
- Octal and hexadecimal (in C language, a specific prefix indicates which hexadecimal is used)
Ox or ox prefix indicates hexadecimal
The O prefix indicates octal
- Display octal and hexadecimal
Decimal display number, using% d
Octal display digits, using% o
Hexadecimal display number, using% x
To display the hexadecimal prefixes o, ox and ox, you must use% #o,% #x and% #x respectively.
bases.c procedure
/*basecs.c--Print decimal number 100 in decimal, octal and hexadecimal*/ #include<stdio.h> int main(void) { int x = 100; printf("dec = %d; octal = %o; hex = %x\n",x,x,x); printf("dec = %d; octal = %#o; hex = %#x\n",x,x,x); return 0; }
3.4.2 other integer types
- short int or short
- long int or long
- long long int or long long long
- unsigned int or unsigned is only used for non negative values
- C90, add unsigned long int or unsigned long and unsigned short int or unsigned short C99, add unsigned long long int or unsigned long long
3.4.3 characters used: char type
The char type is used to store characters
char grade = 'A';
//A single character enclosed in single quotes is called a character constant
- Single quotes apply only to characters, numbers, and punctuation (three ways to represent characters)
- Use ASCII code (for example, the ASCII value of beep character is 7, char beep = 7;)
- Use a special symbol sequence -- escape sequence to represent some special characters.
Escape sequence | meaning |
\a | Alarm (ANSI C) |
\b | Backspace |
\f | Page change |
\n | Line feed |
\r | enter |
\t | Horizontal tab |
\v | vertical tab |
\\ | Backslash (\) |
\' | Single quotation mark |
\" | Double quotation mark |
\? | question mark |
\0oo | Octal value (oo) must be a valid octal number, That is, each o can represent a number from 0 to 7) |
\xhh | Hexadecimal number (hh) must be a valid hexadecimal number, That is, each h can represent a number in 0~f) |
type | hexadecimal | octal number system | decimal system |
char | \x41 | \101 | N.A. |
int | 0x41 | 0101 | 65 |
unsigned int | 0x41u | 0101 | 65 |
long | 0x41L | 0101L | 65L |
unsigned long | 0x41UL | 0101UL | 65UL |
long long | 0x41LL | 0101LL | 65LL |
unsigned long long | 0x41ULL | 0101ULL | 65ULL |
charcode.c procedure
/* charcode.c-Displays the code number of the character*/ #include<stdio.h> int main(void) { char ch; printf("Please enter a character.\n"); scanf("%c",&ch); /*User input character*/ printf("The code for %c is %d.\n",ch,ch); return 0; }
3.4.4 _Bool type
_ Bool type is used to represent Boolean values, i.e. logical values true (1) and false (0)
3.4.5 portable type: stdint H and inttypes h
3.6altnames.c procedure
/*altnames.c --Portable integer type name */ #include <stdio.h> #include <inttypes. h> / / support portable types int main(void) { int32_t me32; //me32 is a 32-bit signed integer variable me32 = 45933945; printf("First, assume int32_t is int:"); printf("me32 = %d\n",me32); printf("Next, Let's not make any assumptions.\n "); printf("Instead,use a \"macro\"from inttrypes.h:"); printf("me32 = %" PRId32 "\n",me32); return 0; }
3.4.6 float, double and long double
number | Scientific notation | Exponential notation |
1000000000 | 1.0*10^9 | 1.0e9 |
123000 | 1.23*10^5 | 1.23e5 |
322.56 | 3.2256*10^2 | 3.2256e2 |
0.000056 | 5.6*10^-5 | 5.6e-5 |
3.7 showf_pt.c procedure
/* showf_pt.c -- Values of type float are displayed in two ways */ #include <stdio.h> int main(void) { float aboat = 32000.0; double abet = 2.14e9; long double dip = 5.32e-5; printf("%f can be written %e\n",aboat,aboat); //The next line requires the compiler to support C99 or its related features printf("And it's %a in hexadecimal, powers of 2 notation\n",aboat); printf("%f can be written %e\n",abet,abet); printf("%Lf can be written %Le\n",dip,dip); return 0; }
3.4.7 types of plural and imaginary numbers
- Three plural types: float_Complex,double_Complex,long double_Complex
- Three types of imaginary numbers: float_Imaginary,double_Imaginary,long double_Imaginary
. if it contains complex H header file, which can be replaced by complex_ Complex, replace with Imaginary_ Imagine, you can also replace the square root of - 1 with I
3.4.8 other types
Arrays, pointers, structures, and unions
3.7 examples of escape sequences
Listing 3.10 demonstrates how backspace (\ b), horizontal tab (\ t), and carriage return (\ r) work
3.10 escape.c procedure
/* escape.c -- Using escape sequences */ #include <stdio.h> int main(void) { float salary; printf("\aEnter your desired monthly salary:"); //1 printf(" $______\b\b\b\b\b\b\b");//2 scanf("%f",&salary); printf("\n\t$%.2f a month is $%.2f a year.",salary,salary * 12.0);//3 printf("\rGee!\n");//4 return 0; }
3.11 programming exercises