[C language from 0 (self-study record)] constants and variables

Posted by silvrfoxx on Sat, 08 Jan 2022 10:11:11 +0100

catalogue

1, Constant:

1. Definition method:

2. Data type:

(1) Integer constant:

(2) Real constant:

(3) Character constant:

2, Variable:

1. Define format:

2. Data type:

3. Integer variable input / output:

3, Input (scanf) output (printf) format:

1. Output (printf):

2. Input (scanf):

4, Naming rules:

5, Example:

1, Constant:

The amount whose value does not change during program operation.

1. Definition method:

(1) Macro definition:

#define constant name constant value / / no semicolon after it

(2) Use const:

const Data type constant name = Value;

The first method is often used to define constants in C language, and the second method is not safe.

2. Data type:

(1) Integer constant:

Three forms:

① Decimal: 0 ~ 9

② Octal: 0 + 0 ~ 7 (starts with 0 and can be followed by numbers 0 ~ 7) classic error: 0148

③ Hex: 0x + 0~9,a~f (starting with 0x or 0x, followed by numbers 0 ~ 9, or letters a ~ f (a ~ F)

Classic error: 0Xabz

If you want to represent a long integer constant, add L or l after the value.

(2) Real constant:

Two forms:

① Decimal: it consists of numbers and decimal points.

② Integer: use e or an integer immediately after e to represent the power with the base of 10, which can be preceded by decimals or integers.

(Note: e or e must have numbers before and after, and must be followed by integers; spaces cannot be added between letters and numbers.)

(3) Character constant:

Four forms:

① General: a pair of single quotation marks ('') enclose one character (excluding spaces), and there must be one character!!! ('' is illegal)

② Escape: it must start with a backslash \, such as' \ n 'line break', '\ t' tab ',' \ \ 'indicates \,' \ "'indicates',' \ '' indicates', etc.

③ Octal: '\ ddd' up to three octal digits (0 ~ 7)

④ Hex: '\ oxhh' up to two hexadecimal digits (0~9 a~f)

2, Variable:

The amount whose value can be changed during program operation. (must be defined before use)

1. Define format:

Data type variable name = Value; //"Value" can be followed by an expression that can calculate the result

Note: variables with the same name cannot be defined in the same function body {}; When defining multiple variables at the same time, separate them with commas; The variable has no initial value, but the initial value is any number.

2. Data type:

data typeOccupied space
Short (short shaping)2 bytes
int (integer)4 bytes
Long (long integer)4 bytes for Windows, 4 bytes (32 bits) for Linux, 8 bytes (64 bits)
long long (long integer)8 bytes
char (character type)1 byte
float (single precision floating point)4 bytes
Double (double precision floating point)8 bytes

Note: ① the number of bytes occupied by integer data in memory is related to the operating system used;

② when a small data type is assigned to a large data type, there will be no error, because the compiler will automatically convert, but conversely, the high bit may be lost

Sizeof: sizeof is used to calculate the size of a data type in bytes (b). Sizeof is not a function, so it does not need to contain any header files. Its return value is an unsigned int. 1B=32bit

Format: sizeof (data type); Sizeof (variable name); Sizeof variable name;

#include <stdio.h>
//Calculate data type size
int main()
{
int a = 10;
short b = 20;
long c = 30;
long long d = 40;
char e = 'a';
float f = 0.1;
double g = 1.0;
printf("Integer:%d byte\n", sizeof(a));
printf("Short integer:%d byte\n", sizeof(b));
printf("Long integer:%d byte\n", sizeof(c));
printf("Long integer:%d byte\n", sizeof(d));
printf("character:%d byte\n", sizeof(e));
printf("Single precision floating point:%d byte\n", sizeof(f));
printf("Double precision floating point:%d byte\n", sizeof(g));
return 0;
} 3. Input and output of integer variable: #include <stdio.h>

int main( )
{
int a = 123;    //Define variable a and assign value in decimal system
int b = 0567;   //Define variable b and assign values in octal
int c = 0xabc;  //Define variable c and assign values in hexadecimal

printf("%d,%o,%x", a,b,c);
return 0;
} #include <stdio.h>

int main( )
{
int a = 12;    //Define variable a and assign value in decimal system

printf("%d,%o,%x", a,a,a);  //Output the value of a in decimal, octal and hexadecimal respectively
return 0;
} signed can be omitted, unsigned can not be omitted, and unsigned can only be given positive value, otherwise it will be garbled.

#include <stdio.h>

int main( )
{
unsigned int a = 12;
unsigned int b = -12;

printf("%u\n", a);
printf("%u", b);

return 0;
} 3, Input (scanf) output (printf) format:

1. Output (printf):

① Format 1:

printf("Format control“)；

Function: output data in the specified format according to format control (i.e. output the things in "" as is).

② Format 2:

printf("Ordinary character/placeholder ",Output table column);

Function: output values in format.

The placeholder starts with% and is replaced by two points, and the ordinary characters are output as is; The output value is calculated before output.

Format characters used in printf function
Format characterexplain
d,iOutputs integers in signed decimal form (positive numbers do not output symbols)
oOutput integer in octal unsigned form (leading character 0 is not output)
x,XThe integer is output in hexadecimal unsigned form (leading character 0x is not output). When x is used, a~f is output in lowercase form, and when x is used, it is output in uppercase letter.
uOutputs an integer in unsigned decimal form.
cOutput in character form, only one character is output.
sOutput string.
fOutput single and double precision in decimal form, and output 6 decimal places implicitly.
e,EOutput real numbers in exponential form. When e is used, the index is expressed in "e" (e.g. 1.2e+02), and when e is used, the index is expressed in "e".
g,G

Select the format with shorter output width in% f or% e format, and do not output meaningless 0. When G is used, if it is output in the form of index, the index is expressed in capital letters.

The format additional characters used in the printf function
Characters (% _m.nf)explain
lUsed for long integers, which can be added before the format characters d, o, x and u
M (represents a positive integer)Minimum data width
N (represents a positive integer)For real numbers, it means to output n decimal places; For a string, it indicates the number of characters intercepted
_The output number or character is left within the field

2. Input (scanf):

Format:

Format control:

① Ordinary characters: ordinary characters must be entered as they are

② Placeholder% d: characters other than numbers can be skipped automatically (carriage return, space, Tab)

③ Placeholder% c: you cannot enter two characters with spaces between them because spaces are also one character

When inputting numerical data, such as entering carriage return, space, Tab key, or encountering illegal characters (characters not belonging to numerical values), the data is considered to be over.

4, Naming rules:

1. System keywords cannot be used;

2. The first character must be a letter or underscore, followed by letters, numbers and underscores;

3. Letters are case sensitive.

5, Example:

#define _CRT_SECURE_NO_WARNINGS  //scanf security problem is solved or #pragma warning(disable:4996)
#include <stdio.h>
//Calculate the area of a circle
#define PI 3.14
int main()
{
float r, s;
scanf("%f",&r);
s = PI * r * r;
printf("%f", s);
return 0;
}

or

#define _CRT_SECURE_NO_WARNINGS  //scanf security problem is solved or #pragma warning(disable:4996)
#include <stdio.h>
//Calculate the area of a circle
int main()
{
const float PI = 3.14;
float r, s; 