- 1 output
#include <stdio.h>
int main()
{
printf("Hello, world!\n");
return 0;
}
#include <stdio.h>
int main()
{
printf("Hello, ");
printf("world!\n");
return 0;
}
这两个程序的输出结果一样:
Hello, world!
Each time printf() is called, printing begins at the position where the previous call to printf() left off.
- 2 symbolic constants
#define PI 3.14159
- 3 field width
printf("%c%3c%5c\n", 'A', 'B', 'C');
结果:
A B C
The field width can be specified in a format as an integer occurring between the
%
and the conversion character.
- 4 input
scanf("%d", &x);
The format
%d
is matched with the expression&x
, causing scanf() to interpret characters in the input stream as a decimal integer and to store the result at the address of x.
- 5 ++/--
int a, b, c=0, d=0;
a=++c;
b=d++;
printf("a=%d, b=%d\n", a, b);
结果:
a=1, b=0
- 6 typedef
//stdint.h
typedef int int32_t;
- 7 EOF
//stdio.h
/* End of file character.
Some things throughout the library rely on this being -1. */
#ifndef EOF
# define EOF (-1)
#endif
- 8 continue v.s. break
for(int i=1; i<10; ++i)
{
if(i%3==0)
continue;
printf("%d\t", i);
}
结果:
1 2 4 5 7 8
The continue statement causes the current iteration of a loop to stop and causes the next iteration of the loop to begin immediately.
for(int i=1; i<10; ++i)
{
if(i%3==0)
break;
printf("%d\t", i);
}
结果:
1 2
The break statement causes an exit from the intermost enclosing loop or switch statement.
- 9 condition operator
x = (y<z) ? y : z
等价于
if(y<z)
x=y
else
x=z
- 10 switch
char c='a';
switch(c)
{
case 'a':
printf("apple\n");
break;
case 'b':
printf("banana\n");
break;
case 'c':
printf("cat\n");
break;
case 'd':
printf("dog\n");
break;
default:
printf("other\n");
}
结果:
apple