The second day of learning C language: C language data types and variables (Part 2)

Table of contents:

        1. Introduction and storage of variables

        2. Arithmetic operators, assignment operators, unary operators

        3.Introduction to scanf and printf

1. Introduction and storage of variables

    1.1.Creation of variables

      Now you understand what types are. Types are used to create variables.

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

      How are variables created? Here is an example:

data_type   name;
//数据类型  变量名;
int age; // integer variable
char ch; // character variable
double weight; // Floating point variable

     When a variable is created, it needs to be given an initial value. This process is called initialization. as follows:

// 初始化
int age = 18;
char ch = 'D';
double weight = 70.5;
unsigned int height 100;

   1.2. Classification of variables

       Divided into: global variables, local variables

        Global variables: Variables defined outside curly braces are global variables. Global variables are used in a wider scope,

                          If you want to use it in the entire project, there is a way to use it.

       Local variables: Variables defined inside curly braces are local variables.

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

         Code demo:

#include <stdio.h>

int global = 2023; // 全局变量

int main()
{
    int locol = 2018; // 局部变量
    printf("%d\n", locol);
    printf("%d\n", global);
    return 0;
}

   1.2.1. Pay attention to the naming of variables

        If a global variable is defined as n, and a variable named n is also defined in the local variable, the value of n of the local variable is first used in the process. This needs to be noted. The following code demonstrates:

#include <stdio.h>

int n = 1;

int main()
{
    int n = 2;
    printf("%d\n", n); // 这里打印出来的值应是2,所以变量名命名的时候需要注意。
    return 0;
}

  1.3. Where are global variables and local variables stored in memory?

        When learning C/C++ language, we will focus on three areas: stack area, heap area, and static area;

        1. Local variables are placed in the stack area of ​​memory;

        2. Global variables are placed in the static area of ​​memory;

        3. The heap area is used to dynamically manage memory

       As shown below:

2. Arithmetic operators, assignment operators, unary operators

    Arithmetic operators (operators): +, -, *, /, %

    Assignment operators: =, +=, -=, *=, /=, %=, >>=, <<=, &=, |=, ^=

    Unary operators: ++, --, +, -

   2.1. The arithmetic operator code is demonstrated as follows:

// 算术操作符
// +、-、*、/、%
int main()
{
	int a = 1;
	int b = 2;
	int c = 3;
	int d = 4;
	int e = 6;
	float f = 7.1;
	printf("%d\n", a + b);
	printf("%d\n", b - a);
	printf("%d\n", b * c);
	printf("%d\n",e / d); // 于C语⾔⾥⾯的整数除法是整除,只会返回整数部分,丢弃⼩数部分。
	printf("%f\n", e / f); // 浮点数的除,返回就不会丢弃小数部分。
	printf("%d\n", e % d); // 运算符 %,即返回两个整数相除的余值。这个运算符只能⽤于整数,不能⽤于浮点数。
	// 负数取模的情况:
	// 由一下得出 第⼀个运算数的正负号决定了结果的正负号
	printf("%d\n",11 % 5);  // 1
	printf("%d\n", -11 % -5); // -1
	printf("%d\n", -11 % 5); // -1
	
	return 0;
}

   operation result:

  2.2.Assignment operator

   Giving an initial value to a variable when it is created is called initialization, and giving another value after the variable is created is called assignment.

int a = 0; // 初始化
a = 200; // 赋值

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

   2.2.1 Continuous assignment

             as follows:

int a = 3;
int b = 4;
int c = 5;
c = b = a + 3; // 虽然C语言支持这样写,但是不方便阅读代码,建议拆开进行写


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

   2.2.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 such code, as follows:

int a = 10;
a += 3;
a -= 2;  // 还有更多的复合赋值符,上面我也列出来了,都可以去尝试一下。

   2.3. Unary operator

        ++, --, + (positive), - (negative) are unary operators.

         2.3.1.++ and --

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

        2.3.2. Prefix++

int a = 10;
int b = ++a; // ++的操作数是a,是放在a前面的,就是前置 ++
printf("%d\n", b);

// 计算⼝诀:先+1,后使⽤;
// a原来是10,先+1,后a变成了11,再使⽤就是赋值给b,b得到的也是11,所以计算技术后,a和b都是11.
int a = 10;
a = a+1;
b = a;
printf("a=%d b=%d\n",a , b);

     2.3.3.Post++

int a = 10;
int b = a++;//++的操作数是a,是放在a的后⾯的,就是后置++
printf("a=%d b=%d\n",a , b);

// 计算⼝诀:先使⽤,后+1
// a原来是10,先使⽤,就是先赋值给b,b得到了10,然后再+1,然后a变成了11,所以直接结束后a是
11,b是10
int a = 10;
int b = a;
a = a+1;
printf("a=%d b=%d\n",a , b);

     2.3.4. Prefix-- 

// 如果你懂了前面我写的前置++,那前置--是同理的,只是把加1,换成了减1;
// 计算⼝诀:先-1,后使⽤
int a = 10;
int b = --a; // --的操作数是a,是放在a的前⾯的,就是前置--
printf("a=%d b=%d\n",a , b);//输出的结果是:9 9

     2.3.5.Post--

// 同理后置--类似于后置++,只是把加一换成了减一
// 计算⼝诀:先使⽤,后-1
int a = 10;
int b = a--; // --的操作数是a,是放在a的后⾯的,就是后置--
printf("a=%d b=%d\n",a , b);//输出的结果是:9 10

     2.3.6.+ and -

        Here + is a positive sign and - is a negative sign, both are unary operators.

        Operator + has no effect on positive and negative values ​​and is an operator that can be completely omitted.

        But no error will be reported if I write it.

    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 a positive number, you will get a negative number.

int a = 10;
int b = -a;
int c = -10;
printf("b=%d c=%d\n", b, c); // 这⾥的b和c都是-10
int a = -10;
int b = -a;
printf("b=%d\n", b);  // 这⾥的b是10

3.Introduction to scanf and printf

    3.1.printf

        Basic usage: The function of printf() is to output 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"); // 会在屏幕输出 Hello World
 return 0;
}

   3.1.How to wrap the line after printf output?

        printf() will not automatically add a newline character at the end of the line. After running,

        The cursor will stay at the end of the output and will not wrap automatically.

        In order to move the cursor to the beginning of the next line, you can add a newline character \n at the end of the output text.

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

        If there is a line break inside the text, it is also achieved by inserting a line break character, as shown in the following code:

#include <stdio.h>
int main(void)
{
 printf("Hello\nWorld\n");

 printf("Hello\n");
 printf("World\n");
 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.

    3.2. scanf

        When we have a variable, we can use the scanf function to input a value to the variable. If we need to output the value of the variable on the screen, we can use the printf function. Here is an example:

        Basic usage: The scanf() function is used to read the user's keyboard input.

        When the program reaches this statement, it will stop and wait for user input from the keyboard.

        After the user enters data and presses the Enter key, scanf() will process the user's input and store it in a variable.

        Its prototype is defined in the header stdio.h.

        The syntax of scanf() is similar to printf().

    scanf(%d, &i);

        The first parameter %d of scanf() indicates that the user input should be an integer. %d is a placeholder, % is the placeholder flag, and d represents an integer. The second parameter & indicates that the integer entered by the user from the keyboard is stored in the variable i.

        Note: The & operator must be added in front of the variable (except pointer variables), because scanf() passes not a value, but an address, that is, the address of variable i points to the value entered by the user. If the variable here is a pointer variable (such as a string variable), then there is no need to add the & operator.

     3.2.1.Return value of scanf

        The return value of scanf() is an integer, indicating the number of variables successfully read.

        If no items were read, or the match failed, 0 is returned. If a read error occurs or the end of file is reached before any data is successfully read, the constant EOF is returned.

        

         Press ctrl+z three times in the VS environment to end the input. We can see that r is 2, which means that 2 values ​​​​were read correctly. If you don't enter a single number, just press ctrl+z three times and the output r will be -1, which is EOF.

        3.2.2. Placeholder

        The commonly used placeholders for scanf() are as follows, which are consistent with the placeholders for printf():

  • %c: character        

  • %d: integer

  • %f: float type floating point number

  • %lf: double type floating point number

  • %Lf: long double type floating point number

  • %s: string

  • %[]: Specify a set of matching characters in square brackets (such as %[0-9]). When encountering characters that are not in the set, the matching will
    stop.

        Note: Among all the above placeholders, except for %c, the leading whitespace characters will be automatically ignored. %c does not ignore whitespace characters and always returns the current first character, regardless of whether it is a space. 

Guess you like

Origin blog.csdn.net/m0_58724783/article/details/131946688