Author's Foreword:
This is my gitee repository: https://gitee.com/qin-laoda/python-exercises
Interested little cuties can click in and have a look, there are codes I wrote in it, let's learn from them together
Since my self-introduction has already been introduced, in my first blog, if you are interested, you can check it out,
author's suggestion
Let's briefly introduce the steps to learn C language well:
=> 1. Learn to code, because all programmers start from coding, no one will become good without coding, unless you are writing a novel, or you are daydreaming
=> 2. It’s not enough when we learn to type code, because we are human, we will forget if we are human, no one will remember everything, unless you are a robot, in order for us to consolidate our knowledge, we must learn to do Notes, but also to summarize, I recommend:
Evernote (Youdao Cloud Notes) - notes can be retrieved, notes can not be lost, easy to review anytime, anywhere.
What is C language
How does the C language code we write run:
A simple C language code
code:
#include<stdio.h>
int main()
{
printf("我的C语言之旅开始了");
return 0;
}
result:
Let me explain:
#include<stdio.h> import header file stdio.h
int : shaping
int main() main function, there must be, and there is only one, as the entry point for the program to run
In fact, the way of writing main() is more, if you want to know, you can go to my gitee to get it
printf() function in stdio.h, library function for printing output
return returns; the returned type corresponds to the int of int main()
Other data types:
Note: C language does not have a string keyword
Floating point means decimal
float has fewer decimal places than double
2: integer
'2': character 2
Let's try the space size of these data types:
code show as below:
#include<stdio.h>
int main()
{
//单位是字节
printf("%d\n", sizeof(char));
printf("%d\n", sizeof(short));
printf("%d\n", sizeof(int));
printf("%d\n", sizeof(long));
printf("%d\n", sizeof(long long));
printf("%d\n", sizeof(float));
printf("%d\n", sizeof(double));
return 0;
}
The result is as follows:
Here some cuties will have doubts, why are the bytes of long the same as int?
because:
sizeof(char)<sizeof(short)<sizeof(int)<=sizeof(long)<=sizeof(long long)
variables and constants
The above data types are prepared for creating variables and constants
Below I write some variables:
#include<stdio.h>
int main()
{
int age = 10;
char name = 'w';
float weight = 45.5f;//加个f变成float类型
float height = 45.5;//会默认为double类型的变量
printf("%f\n", weight);
printf("%f\n", height);
return 0;
}
45.5; The default is double type
45.5f; the default is float type
Double type data has more decimal places than float
Global and local variables
code show as below:
#include<stdio.h>
//全局变量
//当局部和全局变量同名时,本质上他们互不干扰,局部变量会优先
//当一个变量定义在一个函数外,这个变量对这个函数来说就是全局变量
int a = 0;
int main()
{
//局部变量
int a = 10;
printf("%d", a);
return 0;
}
In my opinion, global variables are for functions. When a variable is defined outside a function, the variable is a global variable for the function, and the variable defined inside the function is called a local variable.
So let's do an exercise:
Enter two integers and print the sum of the two numbers:
code:
#include<stdio.h>
int main()
{
int a = 0;
int b = 0;
int sum = 0;
//&取地址操作符
//scanf()库函数--输入函数
scanf("%d %d", &a, &b);
sum = a + b;
printf("%d", sum);
return 0;
}
The result is as follows:
scanf() library function --------- input function
&a: Take the address of a
&b: take the address of b