[Basic knowledge of C language (final)]

Tip: This article is a personal summary of the basic knowledge of C language for the first time. If the content is incorrect, please contact me in time for correction.

  • Reprint please indicate the original, thank you.


Preface

Hello everyone, I am Xiao Ming. Today I will summarize the basic knowledge related to C language for the first time.


insert image description here

提示:以下是本篇文章正文内容:

1. Array

1. Definition of array

When we store a number, we can create a variable, such as int x = 6; but if we want to store ten or even hundreds, we need to use an array .

So, let's look at the format of the array:

Data type variable name [ number of arrays ] = { data content };

or

Data type variable name [ number of arrays ] = { data content };
// When defining the array, the number of arrays can be omitted

For example:

#include <stdio.h>
int main()
{
    
    
	int arr1[5] = {
    
    1,2,3,4,5};
	int arr2[] = {
    
    4,5,6,7,8};
	
    return 0;
}

arr1 defines an integer array with a maximum of 5 elements. When changing, the number of arrays needs to be adjusted according to the data content.
arr2 defines an integer array, holds 5 elements, and omits the number of arrays. Subsequent changes do not require adjusting the number of arrays.

2. Subscript of array

When we know the format of the array, how do we get the value?
In C language, each element of the array has a subscript, and the subscript starts from 0 .

As shown below:
insert image description here

The elements in the array can be accessed through subscripts. If we want to get 2 in the array, we need to use arr1[1] to access.

For example:

#include<stdio.h>
int main()
{
    
    
	int arr[4] = {
    
     12, 345, 678, 999 };
	printf("%d\n",arr[2]);

	return 0;
}

operation result:
insert image description here

As shown in the figure above, when we output, the subscript used is 2, and the data 678 with subscript 2 in the array will be printed on the screen when printing.

2. Function

A function is a group of statements that together perform a task. Every C program has at least one function, the main function main() , and we can define other additional functions for all simple programs.

Code is usually divided according to how each function performs a specific task.

We can divide the code into different functions. And it's up to us to decide how to divide the code into different functions.

For example:

#include<stdio.h>
int Add(int m, int n)
{
    
    
    return m + n;
}
int main()
{
    
    
    int x = 11;
    int y = 22;

    printf("%d\n", Add(x, y));

    return 0;
}

operation result:
insert image description here

We first declared an Add() function, which can return the value of the addition of two digits. When the program is running, it first enters the main function main() , then defines the values ​​of x and y, and then uses printf () function, the values ​​of x and y are passed to the m and n of the Add() function , and the value of the addition of the two numbers is returned, and then printed on the screen.

3. Operators

1. Common arithmetic operators

insert image description here

For example:

#include<stdio.h>
int main()
{
    
    
    int x = 10;
    int y = 3;

    int a = x + y; //加
    int b = x - y; //减
    int c = x * y; //乘
    int d = x / y; //除
    int e = x % y; //取余、取模

    printf("%d\n%d\n%d\n%d\n%d\n", a, b, c, d, e);

    return 0;
}

operation result:
insert image description here

2. Other common operators:

insert image description here

In addition to these common operators, we will also use some other operators. I will explain their functions in detail in the detailed explanation.

4. Pointer

In C language, a pointer is a special variable that is used to store the memory address of a variable. In other words, pointer memory is the memory space used to store pointer variables.

1.Memory

Memory is a particularly important memory on a computer. Programs in the computer are all run in memory.

  • In order to use memory effectively, the memory is divided into small memory units, and the size of each memory unit is 1 byte.
  • In order to effectively access each unit of memory, the memory unit is numbered, and these numbers are called the address of the memory unit.
    insert image description here
  • Variables are allocated space when memory is created, and each memory unit has an address.
    Take out the variable address as follows:
#include <stdio.h>
int main()
{
     
     
    int num = 0;
    // &num;//取出num的地址
    //取出的是第一个字节的地址(首地址)
    printf("%p\n", &num);//打印地址,%p是以地址的形式打印
    return 0;
}

operation result:insert image description here

2. Definition of pointer

So how should the address be stored? Here you need to define a pointer variable.

int num = 0;
int *p;
p = &num;

Use of pointers:

#include <stdio.h>
int main()
{
    
    
    int num = 10;
    int *p = &num;
    *p = 99;
    //修改*p就修改了num的值
    
    return 0;
}

3. The size of the pointer

#include <stdio.h>
int main()
{
    
    
    printf("%d\n", sizeof(char*));
    printf("%d\n", sizeof(short*));
    printf("%d\n", sizeof(int*));
    printf("%d\n", sizeof(double*));
    return 0;
}

operation result:
insert image description here

The pointer size is 4 bytes on 32-bit platforms and 8 bytes on 64-bit platforms.

5. Structure

The structure (struct) in C language is a self-defined data type that can combine different types of data to form a new data type.

Structures are mainly used in the following scenarios:

1. When the program needs to represent multiple different types of variables, such as character, floating point, integer and other types, you can use a structure to use these data as a whole.
2. Structures can easily package data, thereby simplifying program writing and reading.
3. Structure is particularly important in embedded programming because it can improve the efficiency of the code.

The definition form of the structure is:

struct structure name { variable or array contained in the structure; };

A structure is similar to an array. It is also a collection of data, and its overall use does not make much sense. Arrays use subscripts [ ]to obtain individual elements, and structures use dots .to obtain individual members.

The general format for obtaining structure members is:

Structure variable name . Member name ;

For example:
describing a student, the student contains: name + age + height information.
Structures can connect different types of data

#include <stdio.h>
struct Stu
{
    
    
	char name[20];
	int age;
	float height;
};
int main()
{
    
    
	int num;
	struct Stu stu1 = {
    
     "张三", 20, 175.2 };
	struct Stu stu2 = {
    
     "李四", 19, 181.9 };
	struct Stu stu3 = {
    
     "王五", 21, 168.4 };
	printf("姓名:%s 年龄:%d 身高%.1f\n", stu1.name, stu1.age, stu1.height);
	printf("姓名:%s 年龄:%d 身高%.1f\n", stu2.name, stu2.age, stu2.height);
	printf("姓名:%s 年龄:%d 身高%.1f\n", stu3.name, stu3.age, stu3.height);

	return 0;
}

operation result:
insert image description here

It should be noted that the structure is a custom data type, which is a template for creating variables and does not occupy memory space; only structure variables contain actual data and require memory space to store.

end

Okay, after reading this far, you have finished reading the entire content of this blog.
insert image description here

Guess you like

Origin blog.csdn.net/mingloveyang/article/details/132508492