Data types, constants, variables, printf, scanf, and operators

Source: Internet
Author: User

    1. Data type
    2. Constant
    3. Variable
    4. printf Function Introduction
    5. scanf function Introduction
    6. Operator
Data type

The data type is how the data is stored in memory.

There are 5 Big data types in C: Basic type, constructed type, pointer type, NULL type, and definition type.

Common data types in the C language:


Amount of space occupied by various data types in memory: (in bytes) (1 byte = 8 bit)

16-bit compilers

32-bit compilers

64-bit compilers

Char

1

1

1

Int

2

4

4

Float

4

4

4

Double

8

8

8

Short

2

2

2

Long

4

4

8

Long Long

8

8

8

Void * (pointer variable)

2

4

8

Range of representations of various data:

Constant

Constants are data that is fixed in memory and cannot be changed by its contents.

Common categories:

    1. Integral type constant

A) decimal constant, consistent with the natural decimal notation

b) binary constants, starting with 0b (0B), for example: 0b1100

c) Octal constant, starting with 0, for example: 045

d) hexadecimal constants, starting with 0x, for example: 0x123

    1. Real-type constants

A) A single-precision constant, with a decimal followed by an F for a single-precision constant. Example: 3.14f

b) Double-precision constants, in accordance with the natural fractional representation method. Example: 3.1415926 3.8e5 (or 3.8E5 equivalent to 3.8 x 105)

    1. Character-type constants

Represented by a single character enclosed in single quotation marks.

A) ordinary characters: For example: ' A ' B ' ' $ ' and so on

b) escape character: for example: ' \ n ' \ t ' percent ', etc.

    1. String constants

A series of strings enclosed in double quotation marks. For example: "Hello world!\n" "" means an empty string

    1. Symbolic constants

Also called a macro constant. Example: #define PI 3.1415926

Variable

The so-called variable, which means that the value of a space in memory is variable.

1. Definition of variable: variable type variable name

2. Initialization of variables

A) When the definition is initialized: int a = 10;

All initialization when defined: int a = ten, b=11;

Partial initialization when defining: int a =2, b;

b) First define after initialization: int b; b = 10;

c) initialize with other variables: int a=10, B; b = A;

d) Continuous initialization: Double pi, Pai; Pi = Pai = 3.14;

3. Use of variables

Value and stored value!

4. Scope of variables

Different scopes can be divided into local variables and global variables:

Local variables, also known as internal variables, are defined inside a function or inside a block of code, ranging from the definition of a variable to the end of a function or the end of a block of code. Internally, variables with the same name can be defined and externally, and internally these variables will mask external variables with the same name.

Global variables: Also known as external variables, are defined outside the function and are scoped to the end of the file, starting with the definition. (the variable can be used from anywhere in the same file, from where it was defined.) )

    1. Why do variables differentiate between types?

The size and storage of different types of data stored in memory is not the same, in order to effectively use memory space and efficient access, variables must be divided into different types.

In

printf Function Introduction

printf is a standard output function that is included in the standard input and output header file stdio to output results in an accurate format.

The invocation format of the printf function: printf ("Format control string", output item list); For example: Prinf ("name =%s, age =%d", name, age);

Format Control string:

is composed of format characters (conversion specifiers, flags, field widths, precision), and ordinary characters.

List of output items:

It can be a constant, a variable, or an expression, or there can be no output item (for example, only a series of strings), and the output item must be in the same type and quantity as the format control, otherwise the result is unpredictable. When there are multiple project outputs, each output item is separated by commas.

The full format of printf format control:?

%-0 M.N L or h format characters?

The following is a description of the constituent format description:?

1%: Indicates the starting symbol for the format description, which is indispensable.

2-: Yes-Indicates left-aligned output (right-side fill space), such as omitting the right-aligned output (left fill space).

3 0:0 indicates that a specified vacancy is filled in 0, such as omitting the specified vacancy.

4 m.n:m refers to the width of the field, which is the number of characters that the corresponding output item occupies on the output device. n refers to precision. The number of decimal digits used to describe the actual number of outputs. For numeric types, when n is not specified, the implied precision is n=6 bit.

5 L or h:l refers to the integer type long, which refers to the double type of the real type. H is used to Fu Xiu the format character of an integral type to a short type.

Format characters

The format character is used to specify the data type and output format of the output item.

1 D format: used to output decimal integers. There are several uses:%ld: Output Long integer data.

2 o format: outputs integers in unsigned octal form. The long integer type can be output in the format "%lo". You can also specify the field width to be output in "%MO" format.

3 x format: outputs integers in unsigned hexadecimal form. The long integer type can be output in the format "%LX". You can also specify the field width to be output in "%MX" format.

4 u format: outputs integers in unsigned decimal form. The long integer type can be output in the format "%lu". You can also specify the field width to be output in "%MU" format.

5 C Format: output one character;

6 s format: Used to output a string

7 f format: Used to output real data. The double type can be output in "%LF" format. For single precision, the first 7 digits are valid digits, the default output is 6 decimal places, the first 15 digits of the double precision are valid digits, the default output is 6 decimal places.

8 g format: Automatically selects a shorter output in f format or e format, and does not output meaningless 0.

9 p Format: output memory address

E format: Outputs real numbers in exponential form.

Additions to the printf function:

    1. Role of%*.*: printf ("%*.*f", 5, 3, a); * Can be understood as placeholders, for example, this is the variable a by the field width of 5, after the decimal point retains 3 decimal places output.
    2. printf is in strict accordance with the format control output, if an integer variable, output in%f format, the result is not what you want (not expected).

In

scanf function Introduction

The SCANF function prototype is included in the standard input output header file Stdio.h to accept keyboard input. The scanf function is a blocking function.

Format: SANCF ("Format control string", enter address list); For example: SANCF ("%s,%d", &name, &age);

Note the point:

    1. scanf ("%d%d", &a, &d); Here between%d and%d is separated by a space, then when the input two numbers need to be separated by a space or carriage return, no other characters
    2. scanf ("%d,%d", &a, &b); Here%d and%d are separated by commas, separated by commas when entering two digits.
    3. scanf ("%f", &F1); %M.NF can not be used when accepting a real number;
    4. scanf ("%f\n", &F1); This is not an escape character for the carriage return, and it is required to enter the number before the SCANF function is terminated. The format control character channeling other characters in addition to the format control are output as-is.
    5. scanf ("%3d", &num); This means that only the first 3 digits of the input number are received, for example: 12345 is entered, then num = 123;
    6. scanf ("%d,%*d,%d", &a, &d); This indicates that the input 3 number is received, the value of the first number to a, and the value of the third number to D. What%*d here means is to skip the received number.

The operating principle of the scanf function:

When the scanf function is called, a buffer is created, and when the user enters the content on the keyboard, the buffer takes note of the data and, depending on the format control, finds the variables and passes the values according to the address list, so that it affects the mixed reception of different types of data. such as scanf ("%d%c%d", &a, &ch, &b); Enter "Ten A 10" on the keyboard, then will give 10 to A, "(space) to CH, the ' A ' to B, but B is the integer variable, so do not receive this value, so the value of B does not change." To avoid this, do not enter a space before the character is entered, for example, enter "10A 10", or use a comma delimiter, such as scanf ("%d,%c,%d", &a, &ch, &b), and use commas to separate data when entering. We recommend that you use a comma delimiter.

operator operator Precedence vs. associative arithmetic operator type conversion expression assignment Operation Shizhan, self-decrement sizeof operator comma operator relational operator logical operator Trinocular operator

Data types, constants, variables, printf, scanf, and operators

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.