C语言的基本语法、数据类型、运算符以及一些常见的输入输出操作。
一、C语言基本语法
C语言的基本语法包括变量声明、函数定义、条件语句、循环语句等。下面是一个简单的C语言程序示例:
```c #include <stdio.h> int main() { int a, b, sum; a = 5; b = 10; sum = a + b; printf("The sum of a and b is: %d\n", sum); return 0; } ```
二、C语言数据类型
C语言支持多种数据类型,包括整型、浮点型、字符型等。以下是一个演示不同数据类型的示例:
```c #include <stdio.h> int main() { int integer; float floatNum; char character; double doubleNum; integer = 10; floatNum = 10.5; character = 'A'; doubleNum = 10.5; printf("Data types demonstration:\n"); printf("Integer: %d\n", integer); printf("Float: %f\n", floatNum); printf("Character: %c\n", character); printf("Double: %lf\n", doubleNum); return 0; } ```
三、C语言运算符
C语言提供了多种运算符,包括算术运算符、关系运算符、逻辑运算符等。以下是一个演示不同运算符的示例:
```c #include <stdio.h> int main() { int a, b, c; a = 10; b = 5; c = a + b; // 算术运算符 c = a - b; c = a * b; c = a / b; c = a % b; // 取模运算符 c = a > b; // 关系运算符 c = a < b; c = a == b; c = a != b; c = a && b; // 逻辑运算符 c = a || b; c = !a; printf("Operator demonstration:\n"); printf("Addition: %d\n", c); printf("Subtraction: %d\n", c); printf("Multiplication: %d\n", c); printf("Division: %f\n", c); printf("Modulo: %d\n", c); printf("Greater than: %d\n", c); printf("Less than: %d\n", c); printf("Equal to: %d\n", c); printf("Not equal to: %d\n", c); printf("Logical AND: %d\n", c); printf("Logical OR: %d\n", c); printf("Logical NOT: %d\n", c); return 0; } ```
四、C语言输入输出操作
C语言提供了标准输入输出库,如`stdio.h`,用于实现输入输出操作。以下是一个演示输入输出操作的示例:
```c #include <stdio.h> int main() { int number; printf("Please enter a number: "); scanf("%d", &number); printf("You entered: %d\n", number); return 0; } ```