Table of contents
2.1 Find the area of green belt - simple procedure
2.5.2 Expression statements and assignments
2.6 Sequential control structure
2.7.4 Mixed operation type conversion
Preface
ChatGPT
C is a general-purpose, procedural computer programming language developed by Dennis Ritchie at Bell Labs in the early 1970s. It is designed to be a simple, efficient and portable language for system-level programming and application development.
The following are some of the main features of C language:
1. Structured programming : C language supports structured programming, which provides sequential execution, conditional statements (such as if-else and switch) and loop structures (such as for and while), making the program easier to read, understand and maintain.
2. Intermediate language : C language has the characteristics of both high-level language and low-level language. It provides rich control structures and operators, can directly access memory and hardware, and allows programmers to perform low-level operations, such as bit operations and pointer operations.
3. Portability : Programs written in C language are highly portable because its syntax and features remain basically the same on different computer systems. This allows programs to be compiled and executed on different platforms without requiring too many modifications to the code.
4. Powerful standard library : C language provides a rich standard library, which contains various functions and tools for processing input and output, string operations, memory management, mathematical operations, etc. These functions can improve the efficiency of program development and can be used across platforms.
5. System-oriented programming : Because C language has the ability to directly access memory and hardware, it is often used to develop underlying software such as operating systems, embedded systems, and drivers. The C language also provides features such as bit operations and pointers, allowing programmers to better control system resources.
6. Extensibility : C language allows programmers to write custom functions and libraries to extend the functionality of the language. This makes the C language very suitable for the development of large projects, where code can be organized into a modular structure and reused through function calls.
Although the C language is relatively low-level, it is still a very important and widely used programming language. It provides programmers with powerful control capabilities and performance advantages, and has been widely used in various fields, such as system programming, embedded development, game development, etc. Many modern programming languages, such as C++, Java, and Python, have been influenced by the C language and have been extended and improved on it.
2. Sequential programming
2.1 Find the area of green belt - simple procedure
The following is a simple program written in C language to find the width of the green belt:
#include <stdio.h>
int main() {
float length, width, area;
printf("请输入绿化带的长度(单位:米):");
scanf("%f", &length);
printf("请输入绿化带的宽度(单位:米):");
scanf("%f", &width);
area = length * width;
printf("绿化带的面积为: %.2f 平方米\n", area);
return 0;
}
This program first uses printf
a function to output prompt information, and then uses scanf
a function to read the length and width of the green belt from user input and store it in the corresponding variables. Next, calculate the area of the greenbelt by multiplying the length and width and store the result in area
a variable. Finally, use printf
a function to output the area of the green belt to the screen.
2.2 Basic symbols:
In the C language, there are some basic symbols used to represent different meanings, including character sets, specific characters, identifiers, separators, and comments.
2.2.1 Character set
C language uses the ASCII character set as the basic character set, which includes uppercase and lowercase letters, numbers, punctuation marks, special characters, etc.
-
visual characters
are characters that can be displayed on the screen or printed out, including the following types:
- Alphabetic characters: including lowercase letters a~z and uppercase letters A~Z.
- Numeric characters: including numbers from 0 to 9.
- Special characters: including some common special symbols, such as #, +, -, *, /, {}, etc.
-
Invisible characters
are characters that cannot be directly displayed or printed and are often used to control the format, layout, and transmission of text. Invisible characters include the following types:
- Text control characters: used to control the format and layout of text, such as line breaks ('\n'), tab characters ('\t'), etc.
- Media control characters: Characters used to control data transmission and communication, such as start character (SOH), end character (EOT), etc.
- Special control characters: such as space character, carriage return character, backspace character, etc.
It should be noted that the ASCII character set only defines 128 characters and cannot represent characters in other languages (such as Chinese, Japanese, and Korean). In order to represent more characters, extended character encoding standards emerged later, such as Unicode and UTF-8, which can represent various characters worldwide.
2.2.2 C specific characters
Specific symbols in the C language can be divided into three categories: keywords, delimiters, and operators.
Keywords
Words with a fixed spelling that have a special meaning and role. They have a special independent function in a programming language. For example, in C language, keywords include if
, else
, while
, for
etc.
delimiter
A special symbol consisting of one character used to mark the beginning and end of different parts or statements in a program.
# ( ) [ ] { } , ’ ” ; : \
operator
A special symbol consisting of one or more characters, used to perform various operations, such as mathematical operations, logical operations, assignments, etc. Operators in C language include arithmetic operators (such as +
, -
, *
, /
), relational operators (such as <
, >
, ==
), logical operators (such as &&
, ||
), etc.
2.2.3 Identifier
In C language, identifiers are symbols used to represent the naming of variables, functions, types, etc. Identifiers must start with a letter or an underscore and can contain letters, numbers, and underscores. Identifiers are case-sensitive .
2.2.4 Spacer
- In C language, spaces, line terminators, horizontal tabs, vertical tabs, and form feeds are called spacers.
- Used to separate different symbols and statements;
- There should be at least one separator between any two adjacent lexical units consisting of identifiers, reserved words, and literal constants;
- Multiple separators used together are equivalent to one;
- No separators are allowed within any lexical unit. Error example:
- 317 is written as 3 1 7
- <= is written as < =
2.2.5 Comments
In C language, comments are used to describe and explain the code, and the compiler ignores the content of the comments. C language supports two annotation forms:
- Single-line comments: Use
//
the beginning, and//
the content from the end of the line is considered a comment. - Multi-line comments:
/*
start with,*/
end with, and everything in between is considered a comment.
2.3 Data
In C language, data can be divided into literal constants, constant identifiers and variables.
2.3.1 Literal Constants
Literal constants are fixed values that appear directly in the code and do not change. For example, integer constants (such as 123), floating point constants (such as 3.14), character constants (such as 'a') and string constants (such as "Hello, World!") are all literal constants. Literal constants are used directly in the code and do not need to be assigned to variables.
2.3.2 Constant Identifiers
A constant identifier is used to represent a symbol with a fixed value. Once defined, its value cannot be modified . In C language, you can use const
keywords to declare a variable as a constant, for example:
const int MAX_VALUE = 100;
The value of a constant identifier is initialized when declared and cannot be modified in subsequent code.
2.3.3 Variables
Variables are symbols used to store and represent data, and their types can be declared and defined as needed. Variables can be modified and assigned values during program execution.
2.3.4 Code examples
#include <stdio.h>
int main() {
// 字面常量
int num1 = 10; // 整数字面常量
float num2 = 3.14; // 浮点数字面常量
char letter = 'A'; // 字符字面常量
char string[] = "Hello, World!"; // 字符串字面常量
// 常量标识符
const int MAX_VALUE = 100; // 常量标识符
// 变量
int num3;
num3 = 20;
printf("num1: %d\n", num1);
printf("num2: %.2f\n", num2);
printf("letter: %c\n", letter);
printf("string: %s\n", string);
printf("MAX_VALUE: %d\n", MAX_VALUE);
printf("num3: %d\n", num3);
return 0;
}
Output:
2.4 Statements
In C language, a statement is an instruction or command used to perform a specific operation. Common statements include assignment statements, conditional statements, loop statements, etc. Each statement ;
ends with a semicolon ( ).
2.5 Expressions
An expression is a sequence of operands and operators that perform a specific calculation or operation.
2.5.1 Expressions
An expression is a combination of variables, constants, and operators. For example, a + b
, 5 * x
and 3.14 / y
are all expressions.
2.5.2 Expression statements and assignments
An expression statement refers to an expression as part of an entire statement. An assignment statement is a special expression statement used to assign a value to a variable.
2.5.3 Code examples
#include <stdio.h>
int main() {
int a = 5, b = 3;
int result;
result = a + b; // 赋值语句,将a + b的结果赋给result
printf("a + b = %d\n", result);
result = a - b; // 赋值语句,将a - b的结果赋给result
printf("a - b = %d\n", result);
result = a * b; // 赋值语句,将a * b的结果赋给result
printf("a * b = %d\n", result);
result = a / b; // 赋值语句,将a / b的结果赋给result
printf("a / b = %d\n", result);
return 0;
}
2.6 Sequential control structure
The sequential control structure is a program control structure that executes statements in sequence according to the order in which the code is written. Sequential control structures have no conditional judgments or loops and are the simplest control structures. The above code is a simple sequence control structure.
2.7 Data types
2.7.1 Data types
A data type is the type used to represent a variable or expression. Common data types include:
int
: Integer typefloat
: Floating point typechar
:Character typedouble
: Double precision floating point number typevoid
: empty type
2.7.2 Floating point types
Used to represent numeric values with a decimal part. In C language, two floating point types can be float
used double
.
2.7.3 Integer character types
Used to represent integer and character data. In C language, two integer character types can be int
used .char
2.7.4 Mixed operation type conversion
In C language, when different types of data are mixed for operation, type conversion will be performed according to certain rules. For example, when performing operations on integers and floating-point numbers, the integers are automatically converted to floating-point numbers.
2.7.5 Code examples
#include <stdio.h>
int main() {
int num1 = 10;
float num2 = 3.14;
char ch = 'A';
double result;
result = num1 + num2; // 整数和浮点数进行混合运算,整数自动转换为浮点数
printf("整数和浮点数相加的结果:%.2f\n", result);
result = num1 + ch; // 整数和字符进行混合运算,字符自动转换为整数
printf("整数和字符相加的结果:%d\n", result);
return 0;
}
2.8 Input/output
Input/output refers to the process of exchanging data between a program and the user or external devices.
2.8.1 I/O
I/O refers to input (Input) and output (Output) operations, which are used to read data from or output data to external devices .
2.8.2 Character IO
Character IO refers to input and output operations in character units. In C language, you can use getchar
the and putchar
functions to perform character IO operations.
#include <stdio.h>
int main() {
char ch;
printf("请输入一个字符:");
ch = getchar();
printf("您输入的字符是:");
putchar(ch);
return 0;
}
2.8.3 Format input
Format input refers to reading data from an input source (such as the keyboard) in a specific format. In C language, you can use scanf
functions for format input.
2.8.4 Format output
Format output refers to outputting data to an output destination (such as a screen or file) in a specific format. In C language, you can use printf
functions to format output.
#include <stdio.h>
int main() {
int age;
float height;
printf("请输入您的年龄和身高(用空格分隔):");
scanf("%d %f", &age, &height);
printf("您的年龄是:%d 岁,身高是:%.2f 米\n", age, height);
return 0;
}