vc++如何编写c语言_编写C / C ++程序时要记住的要点(规则)

vc++如何编写c语言

You should not place unnecessary comments to those statements which are already cleared.

您不应在已清除的语句上添加不必要的注释。

For example:

例如:

/*The following statement will 
print age on the screen*/
printf("Age:  %d\n",age);

You should not use underscore (_) to start identifier name (like: variable, constant, etc.) unless it’s not required. It can be used to start with those variables which are unique or system related.

除非不需要,否则不要使用下划线(_)开头标识符名称(例如:变量,常量等)。 它可用于从唯一或与系统相关的变量开始。

For example:

例如:

int _age; //not recommended
char* _name;  //not recommended

long _baudrate;  //recommended
unsigned  char  _portNo;   //recommended

Do not use an uninitialized variable; the output/result can be unpredictable.

不要使用未初始化的变量; 输出/结果可能无法预测。

For example:

例如:

//finding sum of all array elements
int sum;
for(i=0;  i<n; i++)
	sum += arr[i];

Here, sum is not initialized and we are using sum to add the array elements. So, there is no guarantee to get correct output.

这里, sum未初始化,我们使用sum添加数组元素。 因此,不能保证获得正确的输出。

You should not use semicolon (;) at the end of the function header. If you do this, compiler will through an error.

您不应在函数标题的末尾使用分号(;)。 如果这样做,编译器将出现错误。

For example: (incorrect way)

例如:(不正确的方式)

int findSum (int x, int y);
{
	//body of the function 
}

Relation expressions evaluate to 0 or 1. Where, 0 is considered as false and 1 is considered as TRUE. But, if is there any expression or variable that you are using within a condition whose result is an integer value. It will be considered as True.

关系表达式的计算结果为0或1。其中,将0视为错误,将1视为TRUE。 但是,如果在结果为整数值的条件内是否使用了任何表达式或变量。 它将被视为True。

Note that:

注意:

  • Only 0 is considered as 'False'

    只有0被视为“假”

  • Positive and negative integers are considered as 'True'

    正整数和负整数被视为“ True”

For example:

例如:

if (0) { ... }  //condition is false here
if(990) { ...}  //condition is true here
if ( -120 ) { ... }  // condition is true here

Do not use semicolon (;) at the end of the if statement. If you terminate if statement by the semicolon, then the statement written in the if statement body will always execute whether is true or not.

不要在if语句的末尾使用分号(;)。 如果用分号终止if语句,则无论是否为true,将始终执行在if语句主体中编写的语句。

There will be two cases,

有两种情况

Case A ) if only if statement is there

情况A)如果仅存在陈述

For example:

例如:

int b=0;
if(b ! =0);
	result = a/b;

Here condition is true, still statement result = a/b; will be executed. Because compiler will consider two separate statement 1) if(b !=0); and 2) result =a/b;

这里条件为真,仍然声明结果= a / b; 将被执行。 因为编译器将考虑两个单独的语句1) if(b!= 0); 2) 结果= a / b;

Case B ) If there is ‘if else statement’

情况B)是否有'if else陈述'

For example:

例如:

int b=0;
if(b != 0);
	resunt = a/b;
else
	result=0;

Here condition is true, but if statement terminated by semicolon. Then, compiler will generate an error due to else statement because else statement can only be used with if statement and here if statement is terminated by the semicolon.

这里的条件为true,但是if语句以分号终止。 然后,由于else语句 ,编译器将生成错误,因为else语句只能与if语句一起使用,此处if语句由分号终止。

Do remember to use ‘address of’ (&) operator while reading values from the scanf() function. Compiler will not generate error but output may unpredictable.

在从scanf()函数读取值时,切记要使用'address of'( & )运算符。 编译器不会产生错误,但是输出可能无法预测。

For example:

例如:

int num;
printf("Enter number: ");
scanf("%d",a); //invalid

Do remember that array index start with 0 (not from 1) and the index of last element is 'N-1'. Here, N is the total number of elements.

请记住,数组索引从0开始(不是从1开始),最后一个元素的索引为“ N-1”。 在此,N是元素总数。

For example: If there are 10 elements in an array arr. Then the index of first element will be 0 and the last element’s index will be 9.

例如:如果数组arr中有10个元素。 那么第一个元素的索引将为0,最后一个元素的索引将为9。

We can access the array elements by using its name and index like: arr[0], arr[1], ..., arr[9]

我们可以通过使用其名称和索引来访问数组元素,例如: arr [0] , arr [1] ,..., arr [9]

Since, you should not use semicolon after the loop statement. But, if you want to run a blank loop that should not execute any statement (a loop without its body statement), we can use loop statement with semicolon. Generally such types of looping statements are used to use delays between two statements.

既然如此,您不应在loop语句后使用分号。 但是,如果要运行不执行任何语句的空白循环(没有主体语句的循环),则可以将循环语句与分号一起使用。 通常,此类循环语句用于在两个语句之间使用延迟。

For example:

例如:

int i ;

printf("Hello world!\n");
for(i=0; i<=10000; i++);
printf("Bye...\n");

翻译自: https://www.includehelp.com/c/important-points-rules-to-remember-while-writing-c-cpp-program.aspx

vc++如何编写c语言

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值