C language savior (zero-based entry C language--1)

content

1. If a worker wants to do good things, he must first sharpen his tools

2. Write the first code

 Create new project >>

3. Data Type

4. Classification of variables

Write a code to complete the addition of 2 integers

5. The life cycle of a function

one more question

6. Find the length of the string

The end mark of the array is "\0". The following program does not give the specific \0 position, so the output is a random value.

 7. Hex conversion

1. Decimal to binary

8. Operators

 What is the result of running the following code?

9. Homework


1. If a worker wants to do good things, he must first sharpen his tools

What tools do I need to compile the C language?

VS 2022 (blog explanations are based on VS2019) download Visual Studio Tools - free installation for Windows, Mac, Linux

 · Qt Creator (can be installed and run across platforms, open source)  Index of /archive/qt (The following is a Qt usage tutorial shared by other bloggers .


2. Write the first code

 Create new project >>

 After setting the path and name, you will find that you are here

Right click on the source file and do the following

 

 Note:  ; separate is a statement

//写C语言代码,首先要写出主函数  两个//代表注释,在写代码的时候经常使用注释可以帮助我们很好的理解代码



#include <stdio.h>         //库函数的使用,得包含对应的头文件

int main()                 //主函数,是程序的入口,主函数有且仅有一个
{
	printf("Hello,World"); //printf是C语言提供的打印库函数

	return 0;
}


//这时候按Ctrl+F5,程序就会运行起来



3. Data Type

sizeof is used to calculate the size of a type

 
 

%d - prints an integer %s - prints a string %c - prints a character %u prints an unsigned integer

int main()
{
	
	printf("%u\n", sizeof(char));//1个字节,代表char类型8个bit位
	printf("%u\n", sizeof(short));//2个字节,代表short类型16个bit位
	printf("%u\n", sizeof(int));//4个字节,代表in类型32个bit位
	printf("%u\n", sizeof(long));//4个字节,代表long类型32个bit位
	printf("%u\n", sizeof(long long));//8个字节,代表long long类型有64个bit位
	printf("%u\n", sizeof(float));//4个字节,代表float类型有32个bit位
	printf("%u\n", sizeof(double));//8个字节,代表double类型有64个bit位


	return 0;
}


4. Classification of variables

int b = 1000;//全局变量 - 定义在大括号的外部

int a = 1000;

int main()
{
	int a = 100;//局部变量 - 定义在大括号的内部
	//局部变量的名字和全局变量的名字冲突的情况下,局部优先

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

	return 0;
}

At this point the result of a is 100.

Notice:

 1. scanf function, format input function, that is, input data into the specified variable from the keyboard according to the format specified by the user.

 2. (#define _CRT_SECURE_NO_WARNINGS 1 is to make the scanf function not report an error, because we ignore the return value of the scanf function)

Write a code to complete the addition of 2 integers

#include <stdio.h>
#define _CRT_SECURE_NO_WARNINGS 1  

int main()
{
	int num1 = 0;//初始化一个变量,num1仅仅是我们自定义的一个变量名
	int num2 = 0;//
	int sum = 0;//存放加法的结果
	//输入
	scanf("%d %d", &num1, &num2);//&下节课会讲
    //目前可以理解为num1,num2两个变量给输入函数用来存放.

	//相加
	sum = num1 + num2;

	//输出
	printf("%d\n", sum);

	return 0;
}


//下面写成函数的写法



int Add(int x, int y)
{
	int z = 0;
	z = x + y;
	return z;
}

int main()
{
	int num1 = 0;
	int num2 = 0;
	int sum = 0;
	scanf("%d %d", &num1, &num2);
	//求和
	//sum = num1 + num2;
	sum = Add(num1, num2);

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

	return 0;

5. The life cycle of a function

In general, a name used in a piece of program code is not always valid/available, and the scope of code that qualifies the availability of the name is the scope of the name.

1. The scope of a local variable is the local scope where the variable is located.

2. The scope of global variables is the life cycle of the entire project variable, which refers to a period of time between the creation of the variable and the destruction of the variable

3. The life cycle of local variables is: entering the scope life cycle begins and ends the scope life cycle

4. The life cycle of global variables is: the life cycle of the entire program.

#include <stdio.h>

int main()
{
	{
		int a = 100;
		printf("%d\n", a);
	}
	printf("%d\n", a);//此时程序报错,a已经在{}局部变量执行完毕销毁了


	return 0;
}

Static modification of local variables to extend the life cycle

understand:

 1.while is a loop, the inside of the wide number () is to judge whether the loop continues, non-0 is true to continue, 0 is false to jump out of the loop

2.test(), test is a function created by ourselves, execution means to enter the function to execute, and then return

#include <stdio.h>



void test()
{
  static int a = 1;
//.static修饰全部变量(或者函数),改变了作用域,让静态的全局变量只能在自己的源文件内部使用
//外部文件用extern就无法调用了,会提示无法解析外部符号
  a++;
  printf("a=%d\n",a);
}

int main()
{
	int i = 0;
    while(i<5)
   {
     test();
     i++;

   }

	return 0;
}

The result is 23456 (a is not destroyed)


one more question

 The result is


6. Find the length of the string

#include <stdio.h>


strlen - 库函数
求字符串的长度
#include <string.h>

int main()
{
	char arr1[] = "abc";
	char arr2[] = { 'a', 'b', 'c', '\0'};

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

	/*printf("%s\n", arr1);
	printf("%s\n", arr2);*/

	return 0;
}

 Note: If you remove the \0 in the arr2 array at this time, the result will be different

The end mark of the array is "\0". The following program does not give the specific \0 position, so the output is a random value.

strlen is to get the effective length of the string, the ending '\0' is not counted.

The rules obtained by strlen are very simple: check from the front to the back until it encounters '\0', then terminate the detection.
 


 7. Hex conversion

1. Decimal to binary

 Decimal infinite division by 2, the remainder of the result is either 1 or 0, after the last division to 1, add the order from the divided 1

In the same way, binary to decimal, such as 00001010, the rightmost 0 represents 2 to the power of 0, 1 represents the power of 2, 0 represents the power of 2, and 1 represents the power of 2, the result is 10 (beginners Base conversion is really not recommended - online tool )

 The '130' of the character represents the 88 obtained by converting the octal number 130 into decimal, as the character 'X' represented by the ascii code value


8. Operators

 

int main()
{
	int a = 0;//初始化
	//
	a = 20;//赋值
	//a = a + 10;
	a += 10;//复合赋值符

	a -= 15;//  换个意思是a=a-15
	printf("a=%d\n", a);//a的结果是15
	return 0;
}



int main()
{
	//printf("%d\n", 7 / 3);//2....1
	//printf("%f\n", 7 / 3.0);

	printf("%d\n", 7 % 3);//2...1

	return 0;
}

 What is the result of running the following code?

#include <stdio.h>



int main()
{

  int a = 0;
  printf("%d",~a);//逻辑反操作符

	return 0;
}

//结果是-1

Negative numbers are represented in the computer's complement, because in a modulo measurement system, subtracting a number is equal to adding its complement, thereby realizing the purpose of converting subtraction operations into addition operations. (Similar to the clock adjustment time, it can be added or subtracted)
As long as it is an integer, the complement is stored in the memory.
The complement of positive numbers, the source code, and the inverse code are the same.
Negative numbers need to be calculated. The source code is inverted (the sign bit is the first bit not to be inverted) to get the inverse code, and then the inverse code + 1 is obtained to get the complement code.

 

Like and (&&), or (||) in mathematics, the result is 0 if any one of a&&b is 0, and the result is true if any one of a||b is non-zero ()


9. Homework

Daily training is a must! Here is a website programming language beginner training camp recommended for beginners to brush questions

Topic name:

Find the greater of two numbers

Topic content:

Write a function to find the greater of two integers

Such as:

Enter: 10 20

Output maximum value: 20

Guess you like

Origin blog.csdn.net/weixin_63543274/article/details/122690390