Personal initial c language notes

Table of contents

#3. Life cycle and scope of variables

3.1. Scope of local variables: the local scope where the variable is located

3.2. Life cycle (describing a time period)

4. Constants

4.1. Literal constants:

4.2.const modified constant variables:

4.3.#define defined identifier constants:

4.4. Enum constants

5. String + escape character + comment

5.1 String:

5.2 Escape characters:

5.3 Comments



one,

1. When a local variable is not initialized, its value is random.

Initialization: char ch=W

2. () is a code block

Local variables: Variables defined within () are local variables

Global variables: variables defined outside ()

When the names of local variables and global variables conflict, the local variables take precedence.

int a=100; global variable

int main

{

int a=10;local variable

printf("%d\n",a);

return 0;

}

1.//stdio.h standard input and output header file

#define _CRT_SECURE_NO_WARNINGS

#include

int main()

{

int num1 = 0;

int num2 = 0;

int sum = 0;

scanf("%d %d", &num1, &num2);

sum = num1 + num2;

printf("%d\n", sum);

return 0;

}

Or in the new compiler it can be written as:

#define _CRT_SECURE_NO_WARNINGS

#include

int main()

{

int num1 = 0;

int num2 = 0;

scanf("%d %d", &num1, &num2);

int sum = num1 + num2; The new version of the variable is defined wherever it is used.

printf("%d\n", sum);

return 0;

}

2. Local search tools: Everything

Search: newc++file.cpp

#3. Life cycle and scope of variables

3.1. Scope of local variables: the local scope where the variable is located

Scope of global variables: entire project

(Describe a scope)

int g = 100;

int main()

{

int a = 10;

{

int b = 20;

printf("%d\n", b);

printf("%d\n", a);

printf("%d\n", g);

}

return 0;

}

3.2. Life cycle (describing a time period)

Local variables: Enter the scope of the local variable, the life cycle begins, exit the scope, and the life cycle ends

Global variables: the life cycle of the entire program

int g = 100;

int main()

{

{

int a = 10;

printf("a=%d\n", a);

}

return 0;

}

4. Constants

4.1. Literal constants:

100 3.14 abc w

4.2.const modified constant variables:

Although the num below is a quantity that cannot be modified, it is still a variable in essence.

const int num=10;

printf("%d\n", num);

num=20; cannot be modified here

printf("%d\n", num);————Compilation error

Example: Explain that const is still essentially a variable

const int n=100;

int arr[n]={0};——————Error, n is still a variable, but it cannot be modified. n cannot be placed in arr. Only constants can be placed in arr——only arr[ can be written 100] (Only the c99 standard can be written like this, supports variable-length arrays, and allows variables to specify the array size)

4.3.#define defined identifier constants:

The essence is constant

#define MAX 100

int main()

{

int a=MAX;

int arr[MAX]; OK

printf("%d",a);

return 0;

}————————100

4.4. Enum constants

Enumeration: enumerate one by one

Gender: male, female, confidential

Define enumeration type:

enum Sex

{ MALE,

FEMALE,

SECRET three enumeration constants

};

int main()

{ printf("%d\n", MALE);

printf("%d\n", FEMALE);

printf("%d\n", SECRET);

return 0;

}————————Three enumeration constants 0, 1, 2

5. String + escape character + comment

5.1 String:

A string of characters enclosed in double quotes

"abc" "a" " " 空

strlen only seeks the length before \0, for example, abc is 3

#include

int main()

{

//You can put the string in the character array

char arr1[]="abc"; V (hide a \0 after abc)

char arr2[] = { 'a','b','c' }; (should be changed to char arr2[] = { 'a','b','c','\0' };

printf("%d\n", strlen(arr1)); strlen only requires the length before \0, for example, abc is 3

printf("%d\n", strlen(arr2));

return 0;

}——————————3 15 (random value 15)

5.2 Escape characters:

change the original meaning

escape character

Definition

\?

When writing multiple question marks in a row, prevent them from being parsed into three letters.

\’

used to represent character constants'

\"

Used to represent character constants"

\\

Used to represent character constants\

\a

Warning character, buzzer sounds

\b

backspace character

\f

paper feed character

\n

newline

\r

Enter

\t

horizontal tab

\v

vertical tab

\ddd ddd represents 1~3 octal digits\130——>x

\xdd dd represents 2 hexadecimal digits\x30——>0

example:

int main()

{

printf("%d\n",strlen("c:\test\628\test.c"));

return 0;

}————————>14

5.3 Comments

There are two comment methods in C language: c++ comment style // default usage style√

The c comment style /* */ is defective and does not support nested comments.

Guess you like

Origin blog.csdn.net/zhang_si_hang/article/details/121570433