为什么此代码显示错误,因为声明终止不正确? [英] why this code shows an error as declaration terminated incorrectly?

查看:87
本文介绍了为什么此代码显示错误,因为声明终止不正确?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

#include< stdio.h>
#include< conio.h>
void main();
{
   int a,b,c,d;
   clrscr();
   a=20,b=21,c=23;
   d=a+b+c;
   printf("sum of values",d);
getch();
}

推荐答案

我认为问题在于:

I think that the problem is here:
void main();



C中的分号告诉编译器声明 [ ^ ]已完成。

在这种情况下,我们定义的函数'main'是一个声明复合语句 [ ^ ]。

在正文出错之前结束这样的声明。正确的代码是:


The semicolon in C tells to the compiler that a statement[^] is complete.
In this case we are defining the function 'main' which is a declaration Compound statement[^].
Closing such a declaration before the body is an error. The correct code is:

void main()
{
   ...    //Function body code
}



为了完整起见,我会在正文之前添加一些其他复合语句,其中正文不是强制性的,不是错误。

即一段时间,一个for等。


For sake of completeness I would add that closing before body some other compound statement, where the body is not mandatory, is not an error.
I.e. a while, a for, etc.

for (i=0; i<10; i++)
  ;    //Do nothing



这篇文章也可以写成:


This coulld be write also as:

for (i=0; i&lt;10; i++);    //Equivalent to former


它可能是
printf("Sum of values: %d", d);

请参阅:

http: //www.cplusplus.com/reference/cstdio/printf [ ^ ]。



-SA


这篇关于为什么此代码显示错误,因为声明终止不正确?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆