Talking about code style

Hi~ Long time no see!

Have you ever had such troubles?

Whenever we finish typing the code, the program may run, but the result is not output, probably because of a brace.

This leads to the low quality of our software, and sometimes a small symbol can cause our entire program to crash.

So today let's talk a little bit about coding style.

1. Try not to use global variables (because global variables are likely to produce uncontrollable results)

2. Follow the rules of declaring before using (we also mentioned this in the function chapter. If you use it without declaring it, the compiler will not understand your instructions)

3. Learn to type spaces and indent when typing code

#include<stdio.h>//错误示例
char reverse_string(char* string);
int main()
{
char arr1[] = "abc";
reverse_string(arr1);
printf("\n");
return 0;
}
char reverse_string(char* string)
{
if (*string == '\0')	
{
return;
}
reverse_string(string + 1);
printf("%c", *string);
}

Let's take a look at the modified code below. Isn't it a lot more refreshing? 

#include<stdio.h>//正确示范

char reverse_string(char* string)//先写子函数
{
	if (*string == '\0')
	{

		return;

	}
	reverse_string(string + 1);
	printf("%c", *string);
}//子函数和主函数中间有空行


int main()//主函数
{
	char arr1[] = "abc";
	reverse_string(arr1);
	printf("\n");
	return 0;
}

4. One line of code does only one thing

#include<stdio.h>

int main()
{
    int a=0;//错误:int a,b;
    int b=0;
    return 0;
}

5. The if, for, while, and do statements are on a line by themselves, and the execution statement must not follow it ( regardless of the length, the curly braces must be added!!! )

if(a>b)//正确
{
    t=a;
    a=b;
    b=t;
}
if(a>b){t=a;a=b;b=t;}//错误示例

6. A space should be added after the keyword, and no space should be added after the function name to distinguish the keyword

while ()//关键字

function(int a,int b);//函数

7. Split too long code

#include<stdio.h>//错误示例
{
if (i love you&& you love me && mi xue bing cheng tian mi mi)
{
    printf("YES\n");
}
return 0;
}
#include<stdio.h>//正确示例
{
if ((i love you)
    &&(you love me) 
    &&(mi xue bing cheng tian mi mi))//记得加括号哦
{
    printf("YES\n");
}
return 0;
}

8. The modifier position is immediately adjacent to the variable name

char *name;

So today's code style explanation is here!

Thank you for reading this. There are bound to be mistakes in the blog post. Please point it out actively.

 

Guess you like

Origin blog.csdn.net/m0_60653728/article/details/122601334