C language learning

C language learning (3)

Extracurricular words: setbacks are the normal life. When we encounter an epidemic situation, we should not blindly enlarge the pain to fill our hearts. We must learn to adjust our hearts, face it calmly, and unite the people of the whole country, so as to defeat the epidemic. Come on, China! Come on, Wuhan!!!

‚Äč

Your answer

——Reprinted from Bili bili up: Yellow Bone in Qingjiang River

Outline of this section

array
Modular design of program

array

Definition

An array is an ordered sequence of elements.

[1] If you name a collection of a limited number of variables of the same type, the name is an array name. The variables that make up an array are called the components of the array, also called the elements of the array, and sometimes called subscript variables. The number used to distinguish the elements of an array is called a subscript. Array is in the program design. For the convenience of processing,
A form in which elements of the same type are organized in an unordered form. [1] These unordered collections of like data elements are called arrays.
An array is a collection used to store data of the same type.

Note: quoted from Baidu Encyclopedia
Description: a collection of data of the same type composed of basic data types is called an array
In this section, we will review the basic data types in Section 1
Basic data type:

The eight data type structures can form an array, and the structure body, common body and pointer type in the derived data type can form an array (later learning)
The storage of array in memory space is continuous storage, that is, one layer of data next to another

For example:

Memory usage:

Standard definition method

Method 1: (definition uninitialized)

		signed int a[10];//Array name is a, type is signed integer
		signed short a[10];// Array name is a, type is signed short
		signed long a[10];// Array name is a, type is signed long integer
		unsigned int a[10];// Array name is a, type is unsigned integer
		unsigned short a[10];// Array name is a, type is unsigned short
		unsigned long a[10];// Array name is a, type is unsigned long integer
		char a[10];// Array name is a, type is character
		float a[10];// Array name is a, type is single precision floating point
		double a[10];// Array name is a, type is double precision floating point

In addition to this, there is a second way to define an array. Take signed integer as an example (define and initialize)
Method two:

		int a[] = {1,2,3,4,5};//Array name is a, type is int, size is 5 * 4 bytes
		int a[10] = {1,2};//Array name is a, type is int, size is 10 * 4 bytes

The third (not commonly used, dynamic allocation: that is, when the program runs to this step, it begins to allocate space)
Method three:

		int *a = new int[5];//Array name is a, type is int, size is 5 * 4 bytes

Be careful:
The array name is the address of the array in memory, the first element of the array is stored in the memory of the array name, and the second element + 4 bytes is the address of the second element (int type).

Programming case 1

#include <stdio.h>
	
int main()
{
		int i;
		int a[5]={1,2,3,4,5};
		for(i=0;i<5;i++)
			printf("%d\n",a[i]);
		printf("\n");
		for(i=0;i<5;i++)
			printf("%d\n",a[0]+i);
		return 0;
}

Case output

Output results:

Modular design of program

C language uses function to realize modularization of program

Function definition method:

Definition of function:
Return type name (formal parameter table column)
{
	Function body statement 
	return expression;
}
Function call:
Function name (actual parameter table column);

//Quoted from Baidu Encyclopedia

type

Functions are divided into library functions and user-defined functions.
In order to make it convenient for users to write programs, C language has developed a large number of library functions, which are defined in the. h file. Users can call these functions to achieve powerful functions.

Function usage:

The function should be placed in front of the main function as usual. If the main function is placed in front, the function declaration should be written in front of the main function.

Programming case 2

#include <stdio.h>

int minus(int x,int y)
{
	return x-y;//Returns the value of x-y
}

int add(int x,int y)
{
	return x+y;//Returns the value of x+y
}

int main()//Main function
{
	int a,b;
	printf("Please input a Value");
	scanf("%d",&a);
	printf("Please input b Value");
	scanf("%d",&b);
	printf("a+b=%d\n",add(a,b));//add(a,b) is a function call
	printf("a-b=%d\n",minus(a,b));//minus(a,b) is a function call
	return 0;
}

Case output

Programming case 3

#include <stdio.h>

int add(int x,int y);//Function declaration
int minus(int x,int y);//Function declaration
int main()//Main function
{
	int a,b;
	printf("Please input a Value");
	scanf("%d",&a);
	printf("Please input b Value");
	scanf("%d",&b);
	printf("a+b=%d\n",add(a,b));//add(a,b) is a function call
	printf("a-b=%d\n",minus(a,b));//minus(a,b) is a function call
	return 0;
}

int minus(int x,int y)
{
	return x-y;//Returns the value of x-y
}

int add(int x,int y)
{
	return x+y;//Returns the value of x+y
}

Case output

It can be seen from this that the two programs are equivalent, and there are two ways to write functions

Programming exercises

int a[10]={1,2,3,4,5,6,7,8,9,10};

The sum, difference, multiplication and division of the first item and the second item, the sum, difference, multiplication and division of the second item and the third item are calculated and output respectively from the above, and then the sum, difference, multiplication and division of the ninth item and the tenth item are analogized.

Published 4 original articles, won praise 2, visited 4967
Private letter follow

Tags: Programming C

Posted on Wed, 05 Feb 2020 08:04:17 -0500 by pck76