1. What is a variable
When the value of a data needs to change or be uncertain, it should be represented by a variable. such as game points.
2. Defining variables
1> Purpose
Before any variables can be used, they must be defined first.
The purpose of defining variables is to allocate a piece of storage space in memory to variables, which makes it easier to store data later.
If you define more than one variable, you allocate different storage space for each variable.
2> format
Variable type variable name;
such as int num;
* Variable name belongs to identifier
* Variable Type
* Different types of variables occupy different sizes of storage space. Memory is extremely limited, allocating the appropriate storage space
* Data types stored by constraint variables (easy operation)
3. Use of variables
1> Assignment Value
To store something in a variable is to assign a value. With a semicolon after the assignment statement;
A = 88;
Note: The equals sign here is not "equal" in mathematics, but rather the assignment operator in C, which assigns the right constant 10 to the left variable a
The first assignment, which can be called "Initialize"
Two types of initialization
Define first, then initialize: int A; A = 10;
The definition is initialized at the same time: int a = 10;
2> modification
You can modify the value of a variable and assign it multiple times. Each assignment will overwrite the original value
A = 10;
A = 20;
The last value of variable A is 20
Use printf to output the value of one or more variables
int a = ten, c = 11;
printf ("a=%d, c=%d", A, c);
Double\float\char output, some tips for formatting characters
Double height = 1.55;
char blood = ' A ';
printf ("height=%.2f, blood type is%c", height, blood);
Simple add-and-subtract operation
int a = 10 + 20;
* Do not use when not initialized (the following wording is not recommended)
int score;
printf ("score=%d", score);
Transfer of values between 3> variables
You can assign the value of one variable to another variable
int a = 10;
int b = A;
Continuous assignment
A = b = 10;
4. Common errors
1> variable name is the same as int a = 10; int a = 12;
The scope of the 2> variable is not correct
Process of creating and releasing variables
code block Scope {int a = 10;}
5. Classroom Code
#include <stdio.h>//with printf you have to
int main ()
{
Variables: As long as there is indeterminate data, you should define the variables to save
To pay attention to, often forget to play
int score = 100;
int time = 9;
%d is a format character (placeholder) that can only output integers
\ n to wrap.
printf ("Score is%d\n", score);
float height = 1.77f;
%f used to output decimals, default is 6 decimal places
printf ("Height is%f\n", height);
Add. 2 after output is 2 decimal places
printf ("Height is%f.2\n", height);
char a = ' a ';
c% used to output a character
printf ("A's value is%c\n", a);
return 0;
}
Common format characters:
1.%d or%i integer (int)
2.%f Decimal (float,double)
3.%c character (char)
Learning notes, C language variables