c语言编程优化界面_C编程优化技术

c语言编程优化界面

As we all know that C programming is a widely used and most popular programming language and it is general purpose programming language which is used to develop any type of applications like low level and high level.

众所周知,C编程是一种广泛使用且最受欢迎的编程语言,它是通用编程语言,可用于开发任何类型的应用程序,例如低级和高级。

So, if you are learning C programming language or if you are a C language programmer, you must read these C programming optimization techniques and use all given techniques to improve your programming skills.

因此,如果您正在学习C编程语言,或者您是C语言程序员,则必须阅读这些C编程优化技术,并使用所有给定的技术来提高您的编程技能。

C语言优化技巧/技术 (C language optimization tips/techniques)

1) main() function should be used properly by using return type and command line arguments.

1)应该通过使用返回类型和命令行参数来正确使用main()函数。

Correct forms:

正确的形式:

    int main(int argc, char* argv[])
    int main (int argc, char** argv)

Read: main() in c

读取: c中的main()



2) Each variable should be declared in separate line, with sufficient spaces.

2)每个变量应在单独的行中声明,并留有足够的空格。

While declaring variables, remember two things

在声明变量时,请记住两件事

  1. Declare each variable in a separate line - it will help to put inline (single line comment after the variable declaration).

    在单独的行中声明每个变量-这将有助于内联(变量声明后的单行注释)。

  2. Initialize variable with default values while declaration - it will help to prevent unpredictable behavior of the variable (variable may give unpredictable result if it is using without initialization).

    在声明时使用默认值初始化变量-这将有助于防止变量发生不可预测的行为(如果变量未经初始化使用,则可能会产生不可预测的结果)。

There should be one tab space between data type, variable name and initialization (variable initialization) value.

数据类型变量名称和初始化 (变量初始化)值之间应有一个制表符空间。

Correct declaration:

正确声明:

int		height	=0;		//height of the object
int		weight	=0;		//width of the object 	
float		result	=0.0f;		//result of the expression 	
char		choice	='N';		//store user's choice



3) There is no need to use integer data type all time for small values.

3)对于小值,不需要一直使用整数数据类型。

There are other data types, which are used to store small integer values, see the below given table and use according to the variable range.

还有其他数据类型,用于存储较小的整数值,请参见下表,并根据变量范围使用。

Data type Required memory Value range
char (signed char)1 byte (8 bits)-128 to 127
unsigned char1 byte (8 bits)0 to 255
short (signed short)2 bytes (16 bits)-32768 to 32767
unsigned short 2 bytes (16 bits)O to 65535
数据类型 所需的内存 取值范围
字符(签名字符) 1个字节(8位) -128至127
无符号的字符 1个字节(8位) 0至255
短号(带符号的短号) 2个字节(16位) -32768至32767
无符号短 2个字节(16位) O至65535

If a variable’s value is positive in all cases, you may use ‘unsigned’ with the data type, it ensure that only positive values will be stored.

如果在所有情况下变量的值均为正,则可以对数据类型使用“无符号”,以确保仅存储正值。

For example: If you are going to declare a variable to store age of a person and we know that minimum age is 0 and maximum age is 100 or 150 (which, is not possible still we can consider maximum age is 150).

例如:如果您要声明一个变量来存储一个人的年龄,并且我们知道最小年龄为0,最大年龄为100或150(这仍然是不可能的,我们可以考虑最大年龄为150)。

Then, the following declaration is correct for it,

然后,以下声明是正确的,

    unsigned char   age =   0;

Note: for an unsigned type of variable, make sure its value must be zero or positive, if there is any negative value, value will be truncated.

注意:对于无符号类型的变量,请确保其值必须为零或正,如果有任何负值,则值将被截断。



4) Do not declare more global variables

4)不要声明更多的全局变量

Global variable’s scope is life time of the program that means if there is any variable declared in global scope it can be used in all functions and it will live till program’s execution.

全局变量的范围是程序的生命周期,这意味着如果在全局范围内声明了任何变量,则可以在所有函数中使用它,并且该变量将一直存在,直到程序执行为止。

Therefore, try to declare variables inside the function blocks; they will de-allocate/destroy when program’s execution leaves the function.

因此,请尝试在功能块中声明变量。 当程序执行离开函数时,它们将取消分配/销毁。



5) Use constant or macro to declare a static array.

5)使用常量或宏来声明静态数组。

If there are multiple declarations of arrays with the fixed values, it will hard to update/change (in future). We can use Macro or constant to declare an array.

如果存在多个具有固定值的数组声明,则(以后)将难以更新/更改。 我们可以使用常量来声明一个数组。

Incorrect method:

方法错误:

int main()
{
	char	first_name	[100];
	char	second_name	[100];
	//...
}

Correct method 1:

正确的方法1:

#define	MAX_CHAR	100
int main()
{
	char	first_name	[MAX_CHAR];
	char	second_name	[MAX_CHAR];
	//...
}

Correct method 2:

正确的方法2:

const unsigned char	MAX_CHAR	= 100;

int main()
{
	char	first_name	[MAX_CHAR];
	char	second_name	[MAX_CHAR];
	//...
}



6) Use switch statement instead of if else to check integral values.

6)使用switch语句代替检查其他整数值。

If you are writing code to check integral values (like integers, characters), it is recommended that you should use switch statement rather than if else statements.

如果要编写代码以检查整数值(例如整数,字符),建议您使用switch语句而不是if else语句

In switch case statement program’s execution jumps to matched case and execute the statement written in that case.

在switch case语句中,程序的执行跳转到匹配的case,然后执行在这种情况下编写的语句。

While, in if else statement, each condition (one by one) is checked till the true condition.

同时,在if else语句中,每个条件(一个接一个地检查)直到真正条件为止。

Incorrect method:

方法错误:

if(choice == 1)
{
	//statements
}
else if(choice == 2)
{
	//statements
}
else if(choice == 3)
{
	//statements
}
...
else
{
	//default statement
}

Correct method:

正确方法:

switch(choice)
{
	case 1:
		//statements
		break;

	case 3:
		//statememts
		break;
	
	...
	
	default:
		//default statements
		[break;] //optional
}

In such cases, switch statements may fast.

在这种情况下, switch语句可能会变快

How?

怎么样?

Let suppose, if last case (condition) required, if statement will reach there by checking all above conditions, while switch statement will directly jump to that case and execute the statements. Therefore, second method will be faster.

假设,如果需要最后一种情况(条件),则if语句将通过检查所有上述条件到达那里,而switch语句将直接跳转到该情况并执行该语句。 因此,第二种方法会更快。



7) Conditions breakdown

7)条件明细

If there are multiple conditions and you are checking them by using if else statement, you may break down them, by using parent and child conditions.

如果存在多个条件,并且正在使用if else语句检查它们,则可以使用父子条件来分解它们。

Consider the given example:

考虑给定的示例:

if(option == 1){
}
else if(option == 2){
}
else if(option == 3){
}
else if(option == 4){
}
else if(option == 5){
}
else if(option == 6){
}
else if(option == 7){
}

We can breakdown the conditions in multiple parts like,

我们可以将条件分解为多个部分,例如,

if(option <= 4){
	if(option == 1){
	}
	else if(option == 2){
	}
	else if(option == 3){
	}
	else if(option == 4){
	}
}
else{ 
	else if(option == 5){
	}
	else if(option == 6){
	}
	else if(option == 7){
	}
}

In such case, conditions check will be less and program’s performance will be faster.

在这种情况下,条件检查会更少,程序的性能会更快。

Read more...

阅读更多...

I hope this post will help you to improve your programming skill, if you have any C programming optimization techniques, please write in the comment box.

我希望这篇文章能帮助您提高编程技能,如果您有任何C编程优化技术 ,请在评论框中写。

翻译自: https://www.includehelp.com/c/c-programming-optimization-techniques.aspx

c语言编程优化界面

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值