C language learning - initial C language (1)

variable, constant

Some values ​​in life are constant (such as: pi, gender, ID number, blood type, etc.) and
some values ​​are variable (such as: age, weight, salary).
Unchanged value, represented by the concept of constant in C language, becomes value represented by variable in C language.

How to define variables

Variables must be defined before they are used in C language. When defining a variable, you need to take a unique name, and at the same time declare what type of data to store in the variable. That is to say, only one type of data of the same type can be stored in a variable.
Note : A variable that defines an integer data type cannot store a character or decimal data.

int main()
{
    
    
	int age;
	age = 18;
	float weight = 45.45f;
	char ch = "a";
}

Variable Naming Rules

Variables are distinguished by variable names, but the rules for naming variables must also be followed:

  1. cannot start with a number
  2. cannot contain special characters
  3. keywords are not allowed
  4. Cannot manipulate 63 characters
  5. Variable names are case sensitive

Note: All keywords used in the C language , standard library function names , and common identifiers are expressed in lowercase letters only, while variable names are usually expressed in uppercase letters.

Classification of variables

In C language, variables are usually divided into local variables and global variables.
Local variables , also known as internal variables, can only be referenced internally and cannot be referenced by other objects or functions.
Global variables are external variables, variables created outside the function, which can be created anywhere in the program. It can be called by any function or object in the program.

int age = 30;//全局变量
int main()
{
    
    
	{
    
    
		int age = 20;//局部变量
	}
	return 0;
}

When the global variable and the local variable are the same, the local variable takes precedence.

Variable scope and duration

To use a variable, you must be clear about its scope and life cycle. Only in this way can the program run as expected.

scope

The scope of a variable refers to the area where the variable functions in the program, which can be understood as the area surrounded by { }.
Variables defined outside the function and before the program entry (main function) are effective from the declaration position to the end of the program. Such a scope is called the file scope.

  1. The scope of a local variable is the local scope in which the variable resides.
  2. The scope of global variables is the entire project.

life cycle

The lifetime of a variable is the time period from creation to destruction.

  1. Local variable life cycle: entering the scope life cycle begins, leaving the scope life cycle ends.
  2. Global variable life cycle: the life cycle of the entire program.

constant

The definition forms of constants and variables in C language are different.
Constants are divided into the following categories:

  • literal constant
  • const modified constant
  • Identifier constants defined by #define
  • enumeration constant
#include <stdio.h> 
//举例 
enum Sex 
{
    
     
MALE, 
FEMALE, 
SECRET 
}; 
//括号中的MALE,FEMALE,SECRET是枚举常量 
int main() 
{
    
     
    //字面常量演示 
    3.14;//字面常量 
    1000;//字面常量 
     
    //const 修饰的常变量 
    const float pai = 3.14f;   //这里的pai是const修饰的常变量 
    pai = 5.14;//是不能直接修改的! 
     
    //#define的标识符常量 演示 
#define MAX 100 
    printf("max = %d\n", MAX); 
     
    //枚举常量演示 
    printf("%d\n", MALE);
    printf("%d\n", FEMALE); 
    printf("%d\n", SECRET); 
    //注:枚举常量的默认是从0开始,依次向下递增1的 
    return 0; 
}

Note : There is also a term of constant variable , which has both the attributes of a constant, but is still a variable in essence. It cannot be used as the length of the array, but the constant defined by #define can be used as the length of the initial array.

#define m 10
int main()
{
    
    
    const int n=10;
    char arr[n];//错误的定义
    char arr1[m];//这个可以初始化数组的长度
}

Strings, Escaped Characters, and Comments

string

"hello\n"
This string of characters enclosed by double quotes is called a string/
Note : The end mark of the string is the escape character '\0', which is not counted or counted as a string when calculating the length of the string content, it will be automatically generated. When outputting, the output will stop only when this escape character is encountered.

#include <stdio.h>
//下面代码,打印结果是什么?为什么?(突出'\0'的重要性)
int main()
{
    
    
    char arr1[] = "hai";
    char arr2[] = {
    
    'h', 'a', 'i'};
    char arr3[] = {
    
    'h', 'a', 'i''\0'};
    printf("%s\n", arr1);
    printf("%s\n", arr2);
    printf("%s\n", arr3);
    return 0;
}

Output content:
Running result.png
It can be seen from the results that the output content of arr2 is uncertain because the position of the escape character '\0' is not determined.

escape character

Suppose we want to print a directory on the screen: c:\test\test.c
If we print as follows according to the original prompt:

int main()
{
    
    
    printf("c:\test\test.c\n");
    return 0;
    
}

In fact, the result of the program running:
escape character output result.png
this has to use escape characters, let's take a look at the escape characters.

escape character paraphrase
\? Used when writing multiple question marks in a row, preventing them from being parsed into three-letter words
\’ Used to represent character constants'
\“ Used to denote double quotes inside a string
\\ Used to denote a backslash, preventing it from being interpreted as an escape sequence
\a warning character, beep
\b backspace
\f Hexadecimal
\n new line
\r carriage return
\t horizontal tab
\v vertical tab
\ddd ddd represents 1~3 octal digits.
\xdd dd means 2 hexadecimal digits.

note

  • Unnecessary code in the code can be deleted directly or commented out.
  • Some codes in the code are more difficult to understand, you can add comment text.
#include <stdio.h>
int Add(int x, int y)
{
    
    
    return x+y;
}
/*C语言风格注释
int Sub(int x, int y)
{
    return x-y;
}
*/
int main()
{
    
    
    //C++注释风格
    //int a = 10;
    //调用Add函数,完成加法
    printf("%d\n", Add(1, 2));
    return 0;
}

Comments come in two flavors:

  • C-style comments / xxxxxx /

          缺陷:不能嵌套注释 
    
  • C++ style comments //xxxxxxxx

          可以注释一行也可以注释多行
    

select statement

Everyone is faced with many choices in life, and our program can also realize this function through specific conditions.
Select Structure Flowchart.png

#include <stdio.h>
int main()
{
    
    
    int coding = 0;
    printf("你会一直坚持下去吗?(选择1 or 0):");
    scanf("%d", &coding);
    if(coding == 1)
   {
    
    
       printf("坚持,你一定会成功\n");
   }
    else
   {
    
    
       printf("放弃,回家去种田\n");
   }
    return 0; 
}

loop statement

The cycle is day after day, year after year, doing the same thing, such as learning, is carried out every day.
Loop Structure Flowchart.png
So how to implement loop in C language?

  • while loop statement: When the condition is true, repeat the operation, otherwise exit the loop.
  • do-while loop statement: Repeat the operation until the condition is not true to exit the loop.
  • for loop statement: Use the loop variable to precisely control the number of repetitions.

This blog mainly introduces variables, constants, strings, escape characters, comments, selection statements and loop statements. In the next blog, I will continue to introduce functions, arrays, operators, common keywords, #define to define constants and macros, pointers and structures. I hope that the content I summarized can help readers learn C language better.

Guess you like

Origin blog.csdn.net/weixin_63284756/article/details/130100639