C language-data types, constants, variables, variable scopes, printf, scanf functions

Source: Internet
Author: User

4. Data type Introduction

Data: Text data, audio, video, images, numbers

There are 5 Big data types in C: Basic type, constructed type, null type, pointer type, custom type

Basic data type:

integer long int integer int (4) end integer short int

Real single-precision float (4) Double precision Doubles (8)

Character char (1)

Memory:

1> memory allocation: 4G storage size: 4*1024*1024*1024byte; The basic unit of memory is a single byte (8bit)

2> Memory Area: Stack (store local variables)

Heap (storing dynamically allocated data, such as: Class object, function malloc requires the system to divide

Memory, dynamically allocated memory requires the program to release itself)

Constant area (holding constant)

Code Area (Code area: Local variables within the code allocate memory here)

Global variable area (static zone)

5. Constants

Fixed amount of constant

1) Integral type constant

2 0b opening 0b00001010;

8 0 opening 012;

10 Default 10

0x 0xff01

2) real-type constants

Single precision: 2.3f

Double precision: 4.5 (default)

3) Character type constants

' A ' ' ' $ ' \ n ' (escape character)

4) String Constants

"ABC" "A"

6. Variables

Storing data on a regular variable in a computer

A variable represents a memory area in memory

Use process:

Define----Initialization----use

1) Definition of variable:

int A;

int a,b,c;

After the variable definition is complete, there is a value:

1) Random 2) The previous program resides in the memory of the 3) system

2) Initialization of variables

1) Define first, then initialize

int A;

A = 10;

2) The defined colleague is initialized

int a =-1;

Initialize all: int a = -1,b=0;

Partial initialization: int A, b =-1;

3) class with other variables.

int a = 0;

int b = A;

4) Continuous initialization

int a,b,c;

A = b = c = 10;

3) Use of variables

int a = 0;

int b;

b = a+10;

A = b;

4) Variable names follow the naming rules for identifiers

7. Scope of variables

Scope of use of variables

Variables: Local variables and global variables

Local variables: variables defined in a function or code block

Scope: Encountered "}" scope end from defined position

Global variables: Defined outside of the function

Scope: Starting from the defined place, to the end of the current file

Attention:

1) The inside of the block can access global variables

2) The variable inside the block cannot be accessed outside the block

3) The inside of the block can define variables with the same name as global variables (masking)

Why variables are initialized:

Since the program ends, the operating system frees up memory, but the value in memory is still there, so the variables we define might use the last memory released by the system.

8. printf function

The output function of the printf function standard

The principle of output: printf to output the content---> Output buffer--and output to the console

Format: printf ("Format description", variable list)

Format specifier:

%d integral type%i can be

%MD%0MD

%c character type

%f Real Type%e%g

%o Octal

%p Address

%x 16 Binary

9, scanf function (blocking function-wait for the user's input, if not input will always wait)

Standard input functions in the C language

Receive what the user entered from the keyboard

Format: scanf ("Format control", address list) address how come? &-Fetch address Symbol

1) Accept a number

int a=-1;

scanf ("%d", &a);

Note: Enter, tab, and space are ignored before entering characters

scanf ("%5d", &a); %MD M is the number of bits of data to get

2) Accept multiple numbers

3) The number and character mix are accepted.

4) The operating principle of the scanf input into the buffer, and then output to the console;

Problems with receiving multiple numbers

int a=-1,b=-1;

scanf ("%d%d", &a,&b);

printf ("a =%d,b =%d\n", b);

An issue that accepts a specified number of digits

int a=-1;

scanf ("%5d", &a);

printf ("A =%d\n", a);

Character blending input

int a=-1;

Char ch;

scanf ("%d#%c", &a,&ch); "," is an ordinary delimiter, the time to enter data is also used "," delimited

scanf ("%d%c", &a,&ch); Problematic, involving the operating principle of scanf, later on

printf ("a =%d,ch =%c\n", a,ch);

About \ nthe problem,

int a=-1;

scanf ("%d\n", &a); \ n line break, try to avoid this notation

printf ("A =%d", A, b);

int a=-1,b=-1;

Char ch;

char c;

scanf ("%d", &a); Receive three content, format: 34 b

scanf ("%c", &ch); Space

scanf ("%c", &ch); B

scanf ("%c", &c); Space

scanf ("%d", &b); 34

printf ("a =%d,ch =%c,b =%d\n", a,ch,b);

C language-data types, constants, variables, variable scopes, printf, scanf functions

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.