Lecture 2: C language numeric types and variables

Introduction: The areas in boldare the more important content, and the ones marked in red are the most important ones. At the same time, I will also provide examples for more important or complex knowledge points To help you understand, you can focus on the key points when reviewing for the exam, which will save time. At the same time, if there is anything you don’t understand, or if there is something wrong with what I wrote, please leave a message below. I basically do it every day Everyone will watch it. Finally, thank you for your support!

Let’s start our lecture now

The learning content of this lecture is as follows:

1 Introduction to data types

2 signed and unsigned

3 Value range of data type

4 variables

5 Arithmetic operators: +, -, *, \, %

6 Assignment operator: = and compound assignment

7 Unary operators: + +, –, +, –

8 Forced type conversion

9 Introduction to scanf and printf

Text begins:

1 Introduction to data types

C language provides rich data types to describe various data in life.

Use integer types to describe integers and character types to describe characters. Only when the compiler knows the type of data can it know how to operate the data.

The following picture is for everyone to understand:

1-1 character type

1 char //-->English words are character character types
2 (signed)char //Signed character type --> will correspond to the following ASCII
3 unsigned char //Unsigned character type

1-2 Integer type

1-3 Floating point type

float
double
long double

1-4 Boolean type

The C language originally did not set up a separate type for the Boolean type, but used 0 to represent false and non-zero to represent true.

The Boolean type was also introduced in C99, specifically to represent true and false.

The use of Boolean types must include the header file

The value of a Boolean type variable is: true or false

The following is a code demonstration

1-5 Various data types length

1-5-1 sizeof operator

sizeof is a keyword and an operator. It is specially used to calculate the length of the data type of the operator. The unit is bytes. The operator of sizeof can be a type, variable or expression.

If the operator of sizeof is not a type but an expression, the following parentheses can be omitted.

The expression following sizeof does not participate in the calculation result.

The calculation result of sizeof is of type size_t.

The C language only stipulates that the return value of the sizeof operator is an unsigned integer, and does not specify a specific type, but leaves it to the system to decide. In different systems, the type of the return value may be unsigned int, unsigned long, or even unsigned long long. The corresponding printf() placeholders are %u, %lu and %llu respectively. This is not conducive to the portability of the program–>different programs placed in different systems have different corresponding values.

The C language provides a solution and creates a type alias size_t to uniformly represent the return value type of sizeof. The return value type of sizeof corresponding to the current system may be unsigned int or unsigned long long.

#include <stdio.h>
int main()
{
 int a = 10;
 printf("%zd\
", sizeof(a));
 printf("%zd\
", sizeof a);//a is the name of the variable, you can omit the () after sizeof
 printf("%zd\
", sizeof(int));
 printf("%zd\
", sizeof(3 + 3.5));//Equivalent to an integer plus a double-precision floating point type
 return 0;
 }

You can see that the expression must be enclosed in parentheses, otherwise it will run with a very strange value.

1-5-2 Length of data type

 printf("%zd\
", sizeof(char)); //The number of bytes is 1
 printf("%zd\
", sizeof(_Bool));//The number of bytes is 1
 printf("%zd\
", sizeof(short));//The number of bytes is 2
 printf("%zd\
", sizeof(int));//The number of bytes is 4
 printf("%zd\
", sizeof(long));//The number of bytes is 4
 printf("%zd\
", sizeof(long long));//The number of bytes is 8
 printf("%zd\
", sizeof(float));//The number of bytes is 4
 printf("%zd\
", sizeof(double));//The number of bytes is 8
 printf("%zd\
", sizeof(long double));//The number of bytes is 8

This is the output under VS2022 X64 configuration

1-5-3 The expression in sizeof is not evaluated

//Test: the expression in sizeof is not calculated #include <stdio.h>
int main()
{
 short s = 2;
 int b = 10;
 printf("%d\
", sizeof(s = b + 1));
 printf("s = %d\
", s);//The value of s is still 2 when running
 return 0;
}

When the code is compiled, sizeof is determined based on the type of expression. The type is commonly used, but the execution of the expression must be executed during the running of the program. Sizeof has been processed during compilation, so during running The expression will not be executed.

2 signed and unsigned

C language uses signed and unsigned keywords to modify character and integer types.

The signed keyword means that the type does not have a sign and can only represent 0 and positive integers.

For the int type, it is signed by default, which means that int is equivalent to signed int.

Since this is the default situation, the keyword signed is generally omitted, but it is not wrong if it is written.

signed int a;
// Equivalent to int a;

The advantage of declaring an integer variable as unsigned is that the maximum integer value that can be represented by an array of the same length is doubled.

For example, the value range of 16-bit signed short int is: -32768~32767, the maximum is 32767; and the value range of unsigned short int can refer to the definition given in limits.h.

The following definitions are related definitions in limit.h in the VS2022 environment.

#define SHRT_MIN (-32768) //The minimum value of a signed 16-bit integer
#define SHRT_MAX 32767 //Maximum value of signed 16-bit integer
#define USHRT_MAX 0xffff //Maximum value of signed 16-bit integer
#define INT_MIN (-2147483647 - 1) //Maximum value of signed integer
#define INT_MAX 2147483647 //Maximum value of signed integer type

The int in unsigned int can be omitted, so the above variable declaration can also be written as follows

unsigned a;
signed char c; // The range is -128 to 127-->You can check the ASCII value by yourself
unsigned char c; // Range is 0 to 255

Note that the C language stipulates that whether the char type has a positive or negative sign by default is determined by the current system.

That is to say, char is not equivalent to signed char. It may be signed char or unsigned char.

This is the same as int, int is equivalent to signed int

3 Value range of data type

There are many data types mentioned above, especially the integer types including short, int, long, and long long. Why? ,

In fact, each data type has its own value range, which is the range of the maximum and minimum values of the stored values. With a rich range of types, we can choose the appropriate type in the appropriate scenario. If you want to see the limit values of different data types on your current system:

The limits. file describes the value range of the floating point type.

The float.h header file describes the value range of the floating point type.

For the sake of code portability, when you need to know the limit value of a certain integer type, you should try to use these constants.

·SCHAR_MIN, SCHAR_MAX: the minimum and maximum values of signed char.
? SHRT_MIN, SHRT_MAX: the minimum and maximum values of short.
? INT_MIN, INT_MAX: the minimum and maximum values of int.
? LONG_MIN, LONG_MAX: The minimum and maximum values of long.
? LLONG_MIN, LLONG_MAX: the minimum and maximum values of long long.
? UCHAR_MAX: The maximum value of unsigned char.
? USHRT_MAX: The maximum value of unsigned short.
? UINT_MAX: The maximum value of unsigned int. ? ULONG_MAX: The maximum value of unsigned long.
? ULLONG_MAX: The maximum value of unsigned long long. 

4 Variables

4.1 Creation of variables

Now that we understand types, what do we do with types? Types are used to create variables

What are variables? In C language, values that change frequently are called variables, and values that do not change are called constants.

The syntax for variable creation is as follows:

1 data_type name;
2 | |
3 | |
4 data type variable name




1 int age; //integer variable
2 char ch; //Character variable
3 double weight; //Floating point variable

A variable is given an initial value when it is created, which is called initialization.

1 int age = 18;
2 char ch = 'w';
3 double weight = 48.0;
4 unsigned int height = 100;

4-2 Classification of variables

4-2-1 Global variables: Variables defined outside curly braces are global variables

Global variables have a wider scope of use. If you want to use them in the entire project, there are ways to use them.

4-2-2 Local variables: A variable defined inside curly braces is a local variable.

The scope of use of local variables is relatively limited and can only be used within the local scope where they are located.

1 #include <stdio.h>
2
3 int global = 2023; //Global variable 4
5 int main()
6 {
7 int local = 2018;//local variable
8 printf("%d\
", local);
9 printf("%d\
", global);
10 return 0;
11}

What if the local variable and global variable have the same name?

#include <stdio.h>
int n = 1000;
int main()
{
 int n = 10;
 printf("%d\
" n);//What is the printed result?
 return 0;
}

In fact, when a local variable and a global variable have the same name, the local variable takes precedence.

Where are global variables and local variables stored in memory?

Generally when we learn C/C++ language, we will pay attention to thethree areas in the memory: stack area, heap area, and static area

1 Local variables are placed in the stack area of memory

2 Global variables are static areas placed in memory

3 The heap area is used for dynamic memory management (let’s leave this aside for now, we’ll talk about it later)
In fact, the division of memory areas will be more detailed, which will be introduced later in the relevant knowledge of the operating system.

5 Arithmetic operators: +, -, *, %

When writing code, calculations must be involved.

In order to facilitate operations, C language provides a series of operators, one of which is called arithmetic operators. They are: + – * / %, these operators are all binary operators

Note: Operators are also called: operators, which are different translations but have the same meaning.

5-1 + and –

Both + and – operate with two operands. The two ends of the operator are their operands. This type of operator is also called binary operator

#include <stdio.h>
int main()
{
 int x = 4 + 22;
 int y = 61 - 23;
 printf("%d\
", x);
 printf("%d\
", y);
 return 0;
}

5-2 *

#include <stdio.h>
int main()
{
 int num = 5;
 printf("%d\
", num * num); // Output 25
 return 0;
}

5-3 /

Operator / is used to perform division.

If both ends of the division sign are integers, the integer start is performed, and the result obtained is also an integer.

include <stdio.h>
int main()
{
 float x = 6 / 4;
 int y = 6 / 4;
 printf("%f\
", x); // Output 1.000000
 printf("%d\
", y); // Output 1
 return 0;
} 

In the above example, although the type of variable x is float (floating point number), the result obtained by 6/4 is 1.0 instead of 1.5. The reason is thatinteger division in C language is an integer division, which only returns the integer part and discards the decimal part.

If you want to get the result of a floating point number,two operators must have at least one floating point number, then C language will perform floating point division.

#include <stdio.h>
int main()
{
 float x = 6.0 / 4; // or written as 6 / 4.0
 printf("%f\
", x); // Output 1.500000
 return 0;
}

Let’s look at another example:

#include <stdio.h>
int main()
{
 int score = 5;
 score = (score / 20) * 100;
 return 0;
}

In the above code, you may think that after calculation, the score will be equal to 25, but in fact the score is equal to 0. This is because score/20 is an integer divisor and will result in an integer value of 0, so the result obtained after ?100 is also 0.

In order to get the expected results, you can change the divisor 20 to 20.0, so that integer division becomes floating point division.

#include <stdio.h>
int main()
{
 int score = 5;
 score = (score / 20.0) * 100;
 return 0;
}

5-4%

The operator % represents the modulo operation, which returns the remainder of the division of two integers. Thisoperator can only be used on integers, not floating point types.

#include <stdio.h>
int main()
{
 int x = 6 % 4; // 2
 return 0;
}

The rule for modulo negative numbers is that the sign of the result is determined by the sign of the first operand

#include <stdio.h>
int main()
{
 printf("%d\
", 11 % -5); // 1
 printf("%d\
",-11 % -5); // -1
 printf("%d\
",-11 % 5); // -1
 return 0;
}

6 Assignment operator: = and compound assignment, such as

int a = 100;//Initialization
a = 200;//Assignment, here is to reassign the value of a to 200

The assignment operator = is an operator that can assign a value to a variable at any time.

6-1 Continuous assignment
The assignment operator can also assign values continuously, such as:

int a = 3;
int b = 5;
int c = 0;
c = b = a + 3;//Continuous assignment, assignment from right to left. 

Although the C language supports this kind of continuous assignment, the written code is not easy to understand. It is recommended to write it in pieces, so that it is easier to observe the execution details of the code.

int a = 3;
int b = 5;
int c = 0;
b = a + 3;
c = b;

Written this way, the details of each assignment can be easily observed during debugging.

6-2 Compound assignment operator

When writing code, we may often perform increment and decrement operations on a number, as shown in the following code:

int a = 10;
a = a + 3;
a = a-2;

C language provides a more convenient way to write code like this:

int a = 10;
a + = 3;
a -= 2;

The C language provides compound assignment operators to facilitate us in writing code. These assignment operators are:

 + = -=
*= /= %=
//The next operator will be explained later
>>= <<=
 & amp;= |= ^=

7-1 + + and —

+ + is a self-increasing operator, which is divided into prefix + + and postfix + +. — is a self-decreasing operator, which is also divided into prefix — and postfix –.

7-1-1 prefix + +

int a = 10;
a = a + 1;
b = a;
printf("a=%d b=%d\
",a , b);

int a = 10;
int b = --a;//--The operand is a, it is placed before a?, that is, preceded--
printf("a=%d b=%d\
",a, b);//The output result is: 9 9

7-2 + and –

Here + and – are both unary operators.

Operator + has no effect on positive and negative values. It is an operator that can be completely omitted, but it will not report an error if written

1 int a = + 10; is equivalent to int a = 10; 

Operator – used to change the sign of a value. If you add – in front of a negative number, you will get a positive number. If you add – in front of the row number, you will get a negative number.

int a = 10;
int b = -a;
int c = -10;
printf("b=%d c=%d\
", b, c);//The b and c of this? are both -10
int a = -10;
int b = -a;
printf("b=%d\
", b); //The b of this? is 10

int a = 3.14;
//a is of int type, and 3.14 is of double type. If the two types are inconsistent, the compiler will issue a warning 

9 Introduction to scanf and printf

9-1 printf

9-1-1 Basic usage

The function of printf() is to output the parameter text to the screen. The f in its name stands for format, which means that the format of the output text can be customized.

#include <stdio.h>
int main(void)
{
 printf("Hello World");
 return 0;
} 

printf() is defined in the header file stdio.h of the standard library. Before using this function, this header file must be introduced at the head of the source code file.

9-1-2 Placeholder

printf() can specify placeholders in the output text

The so-called placeholder means that this position can be substituted with other values.

// Output There are 3 apples
#include <stdio.h>
int main()
{
 printf("There are %d apples\
", 3);
 return 0;
}

#include <stdio.h>
int main()
{
 printf("%s will come tonight\
", "zhangsan");
 return 0;
}

#include <stdio.h>
int main()
{
 printf("%s says it is %d o'clock\
", "lisi", 21);
 return 0;
}

This is the introduction here. We will talk about it next time. You can digest the above content first.