基本数据类型
字符型
char c = 'a';
整数型
int a = 7, b = 8, c;
c = a + b;
c = a - b;
数组
int array[10] = {0, 1, 2, 3, 4, 5, 6, 7, 8 ,9};
char name[10] = "Underwood";
array[0] = 10;
name[0] = 'u';
结构体
struct A {
int number;
char name[10];
};
struct A s;
s.number = 10;
s.name[0] = 'a';
指针
int a = 0;
int *pa = &a;
*pa = 5;
char name[10];
char *pc = name;
*pc = 'a';
pc += 1;
*pc = 'b';
struct A {
int number;
char name[10];
};
struct A s;
struct A *ps = &s;
ps->number = 10;
ps->name[0] = 'a';
流程控制
分支语句
int age = 18;
if (age >=18)
printf("An adult\n");
else if (age >= 3)
printf("A child\n");
else
printf("A baby");
循环语句
for循环:
int i, sum = 0;
for (i = 0; i < 10; i++)
sum += I;
while循环:
int i = 0, sum = 0;
while (i < 10) {
sum += i;
i++;
}
函数
void check_age(int age) { if (age >=18) printf("An adult\n"); else if (age >= 3) printf("A child\n"); else printf("A baby");}
check_age(18);