Article directory
1. Know the variables
Assembly language: Directly manipulate memory addresses.
Add 1 here, subtract 1 there.
C program: The memory address is given a name, called a variable.
a+1: The piece of memory pointed to by the variable a, the value is incremented by 1.
2, variable declaration
Declare a variable:
类型 变量名;
You can assign values when declaring:
类型 变量名=值;
If not assigned, there will be a default value.
Example:
Declare a variable of type int, named a, and store 3:
int a=3;
Access the value stored in a:
a
That code is:
#include<stdio.h>
int main(){
int a=3;
printf("%d\n",a);
return 0;
}
Effect:
3. Data Type
char
char represents a character, which can be letters, numbers, punctuation marks. ASCII code.
Surrounded by single quotes.
#include<stdio.h>
int main(){
char a='t';
printf("%c\n",a);
return 0;
}
Effect:
short,int,long,long long
Four integer types. values can be stored.
Difference:
The number of bytes occupied in memory, increasing from left to right.
#include<stdio.h>
int main(){
short a=1;
int b=2;
long c=3;
long long d=4;
printf("%d,%d,%ld,%lld\n",a,b,c,d);
return 0;
}
Effect:
float,double
Both decimal types can store decimals.
Difference:
double has more decimal places and occupies more bytes.
After the number of float, add an f to indicate that it is of type float.
#include<stdio.h>
int main(){
float a=3.14f;
double b=6.28;
printf("%f,%lf\n",a,b);
return 0;
}
Effect:
4, the number of bytes occupied
Memory is represented in binary.
A bit is the smallest unit of memory, representing a 1 or 0.
One byte byte is equal to 8 bits, eight bits.
1024 bytes is 1kb.
The number of bytes occupied by each type:
char:1
short:2
int:4
long:4
long long:8
float:4
double:8
8g U disk, can not hold 10g movies.
The value range of this type can be calculated from the number of bytes.
char has 1 byte and eight bits, so
it can store: two to the eighth power, 256 numbers.
The number of bytes may vary between compilers.
5, global, local
Global variable: not inside a code block { }.
Local: in { }.
When the name is the same, the local area takes precedence.
But the same name is not recommended.
#include<stdio.h>
int a=3;
int main(){
int a=6;
printf("%d\n",a);
return 0;
}
Effect:
A code block can be created manually.
#include<stdio.h>
int main(){
int a=3;
{
int a=6;
printf("%d\n",a);
}
printf("%d\n",a);
return 0;
}
Effect:
It can be seen that it is still local priority.
6. Life cycle
scope
Global: Globally valid.
local: where { }
life cycle
Global: start the app to terminate the app.
Local: The execution of the declaration statement begins and ends when it goes out of scope.
Extra: printf format
%c: character
%d: integer
%ld: long integer
%lld: long long integer
%f: floating point number
%lf: double floating point number
Extra: scanf receives data
Simple to use:
#include<stdio.h>
int main(){
int a;
scanf("%d",&a);
printf("加一之后是:%d\n",a+1);
return 0;
}
Understand the ampersand:
a is a variable that points to an address.
&a stands for that memory address.
Effect: Enter 345.