"C Language Elementary"
<Chapter One-Getting to Know the C Language>
[Disclaimer] Due to the limited level of the author, the shortcomings of this article are unavoidable. I hope readers can criticize and correct me.
[Status] Updating...
[Contact information] QQ: 1300300100
[Last update] April 1, 2020
[Errata record] None
table of Contents
Preface
C language is a process-oriented, abstract general-purpose programming language, which is widely used in low-level development. C language can compile and process low-level memory in a simple way. C language is a process-oriented, abstract general-purpose programming language, which is widely used in low-level development. C language can compile and process low-level memory in a simple way. C language is a high-efficiency programming language that only produces a small amount of machine language and can run without any operating environment support. Although C language provides many low-level processing functions, it still maintains cross-platform features. C language programs written in a standard specification can be executed on many computer platforms including operating platforms such as embedded processors and supercomputers. Compile.1. Hello, world.
- main function
- The main function is the entry point of the program
- There is only one main function
(there can be multiple .c files in a project, but there can be only one main function in multiple .c files)
int main(void) //主函数
{
;
}
- Environmental test code
"Hello, world!"
"Long programming road, from here on..."
#include <stdio.h> //头文件
int main(void)
{
printf("hello world!\n");
return 0;
}
Compile + link + run the code
Shortcut key: Ctrl+F5 The program is executed too fast and the result is not seen.
In order to see the result, we need to set the properties of VS2013 (VS2019 does not need to set the project properties)
[解决方案资源管理器] -> [右键项目名称] -> [属性] -> [链接器] -> [系统] -> [子系统]
-> [选择' 控制台(/SUBSYSRTEM:CONSOLE) ']
The result of the operation is shown in the figure:
Two, data type
- Types of
Code demonstration (Note: #include is not written by default, only the key part of the code is intercepted)
int main(void)
{
char ch = 'a';
int age = 20;
float weight = 50.8
double d = 0.0
return 0;
}
- %
Code demo
#include <stdio.h>
int main()
{
printf("%d\n", 100);
printf("%c\n", 'a');
float foo = 5.0;
printf("%f\n", foo);
double pi = 3.14;
printf("%.2lf\n", pi); // .xf(x为小数点保留几位数)
char str1[] = "hello";
printf("%s\n", str1);
int a = 10;
printf("%p\n", &a);
return 0;
}
- Data type size
sizeof function
sizeof() - 计算类型或者变量的所占空间的大小
Code demo
#include <stdio.h>
int main(void)
{
print("%d\n", sizeof(char));
return 0;
}
>>> 1
data = pd.read_csv(
'https://labfile.oss.aliyuncs.com/courses/1283/adult.data.csv')
print(data.head())
The unit in the computer: bit-the bit
is recognized in the computer Binary 1 0
Octal: 0-7
Decimal: 0-9
Three, variables and constants
- Create variable
It is recommended to declare the variable
type variable name = 0; recommended
type variable name; not recommended
int var = 0; 创建一个变量
Code demo
#include <stdio.h>
int main(void)
{
int age = 20;
double weight = 62.5;
age = age + 1;
weight = weight - 10;
printf("%d\n", age);
printf("%lf\n", weight);
return 0;
}
- Global variables and local variables
Global variables: {} Externally defined
Local variables: {} Internally defined
* It is not recommended to write the names of global variables and local variables the same, which is prone to ambiguity.
int global_variable; '全局变量' {
}外部
int main(void)
{
int local_variable; '局部变量' {
}内部
return 0
}
When the names of local variables and global variables are the same, the local variables take precedence and affect the local
int var = 100;
int main(void)
{
int var = 10;
printf("%d\n", var);
return 0
}
>>> 10
- Use of variables
Write a code to find the sum of two integers
int main(void)
{
int a = 0;
int b = 0;
int sum = 0;
scanf("%d %d", &a, &b);
sum = a + b;
printf("sum = %d\n", sum);
return 0;
}
- Problem with error after running
Because VS does not support scanf input, and an error is reported.
We only need to add to the first line of the code: the first line of the
original file, plus
#define _CRT_SECURE_NO_WARNINGS 1
#define _CRT_SECURE_NO_WARNINGS 1