[Learning notes] [C Language] variable, learning notes variable

Source: Internet
Author: User

[Learning notes] [C Language] variable, learning notes variable
1. What is a variable?

When the value of a data item needs to be changed frequently or uncertain, It should be represented by a variable. For example, game points.

2. Define Variables

1> Purpose

Any variables must be defined before use.

The purpose of defining a variable is to allocate a storage space to the variable in the memory to facilitate future data storage.

If multiple variables are defined, different buckets are allocated for these variables.

2> Format

Variable name;

For example, int num;

* The variable name is an identifier.

* Variable type

* Different types of variables occupy different storage spaces. Memory is extremely limited and proper storage space is allocated

* Type of data where the constraint variable is stored (easy operation)

3. Use of Variables

1> value assignment

Save something to the variable, that is, assign values. Assign a value with a semicolon;

A = 88;

Note: The equal sign = here is not equal in mathematics, but a value assignment operator in C. It is used to assign the constant 10 on the right to variable a on the left.

The first assignment, which can be called "initialization"

Two initialization Modes

First defined, then initialized: int a; a = 10;

Initialize the definition at the same time: int a = 10;

2> modify

You can modify the value of a variable and assign values multiple times. Each assignment will overwrite the original value.

A = 10;

A = 20;

The final value of variable a is 20.

Use printf to output values of one or more variables

Int a = 10, 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: % c", height, blood );

Simple addition and subtraction

Int a = 10 + 20;

* Do not use it when Initialization is not performed (the following statement is not recommended)

Int score;

Printf ("score = % d", score );

3> passing values between variables

You can assign a value to another variable.

Int a = 10;

Int B =;

Continuous assignment

A = B = 10;

4. Common Errors

1> same variable nameInt a = 10; int a = 12;

2> the variable scope is incorrect.

Variable creation and release process

Code block scope {int a = 10 ;}

5. classroom code

# Include <stdio. h> // required when printf is used

Int main ()

{

// Variable: variables should be defined to save as long as there is uncertain data

// Note that you often forget

Int score = 100;

Int time = 9;

// % D is a format character (placeholder) that can only output Integers

// \ N for line feed

Printf ("score: % d \ n", score );

Float height = 1.77f;

// % F is used to output decimal places. The default value is 6 decimal places.

Printf ("height is % f \ n", height );

// After adding. 2, the output is a 2-digit decimal number.

Printf ("height: % f.2 \ n", height );

Char a = 'a ';

// C % is used to output a character

Printf ("the value of a is % c \ n", );

Return 0;

}

Common Format characters:

1.% d or % I INTEGER (int)

2.% f decimal (float, double)

3.% c (char)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.