首页 > 编程语言 >【c&c++】VScode报错error: ‘::main‘ must return ‘int‘ void main()

【c&c++】VScode报错error: ‘::main‘ must return ‘int‘ void main()

时间:2023-04-24 18:22:41浏览次数:43  
标签:return 函数 int void 报错 printf main

在运行指针时终端出现error: ‘::main’ must return ‘int’ void main()错误。
源代码如下:

#include<stdio.h>
void main()
{
    int a,*p,b,c,d,e;
    a=100;
    p=&a;  /* (*&a) 先进行&a运算,得a的地址,再进行*运算,即变量a的值 */
    b=*&a; 
    printf("a=%d\n",a);
    printf("p=%d\n",p);
    printf("*p=%d\n",*p);
    printf("*&a=%d\n",b);
    c=(*p)++;
    printf("(*p)++=%d\n",c); /* (*p)++相当于a++,表示p所指向的元素值加1*/
    printf("a=%d\n",a);
}

报错如下:

 原因及解决方案如下:
(1)原因:
有的C编译器要求baimain()函数必须要有一个int的返回值。用以获知main()函数的执行情况。
(2)解决方案:

将void main()修改为以下皆可:
int main(void)
int main(int argc, const char *argv[])
int main(int argc, const char *argv[], const char *env[])

一般main()函数正常运行结束,就返回一个0;
如果出错,就返回相应的int型的出错代码值(由编程者约定)。
在 C89 标准中,main( ) 是可以接受的。Brian W. Kernighan 和 Dennis M. Ritchie 的经典巨著 The C programming Language 用的就是 main( )。不过在最新的 C99 标准中,只有以下两种定义方式是正确的:

int main(void){/百度百科示例代码/}//整数类型主函数(无类型)

int main(int argc,char **argv)//整数类型主函数(整数类型 统计参数个数,字符类型*数组指针至字符[])

main 函数的返回值类型必须是 int ,这样返回值才能传递给程序的激活者(如操作系统)。
  如果 main 函数的最后没有写 return 语句的话,C99 规定编译器要自动在生成的目标文件中(如 exe 文件)加入return 0; ,表示程序正常退出。不过,我还是建议你最好在main函数的最后加上return 语句,虽然没有这个必要,但这是一个好的习惯。
  也就是说在最新的C语言标准中强制要求main函数的返回值类型为int,main函数的返回值是传递给操作系统,让操作系统判断程序的执行情况(是正常结束还是出现异常)

error: '::main' must return 'int'解决办法

运行出现错误:

error: '::main' must return 'int'

原因:

1、C语言标准允许main函数为void类型。按照C++的标准中main必须是int类型。

2、编译器的原因,有的编译器要求int()函数必须要有一个int型的返回值

把 void main()改成 int main() ,结尾加上 return 0 :

int main()
{
  ......//程序内容
  return 0;
}

遂编译成功

 

标签:return,函数,int,void,报错,printf,main
From: https://www.cnblogs.com/opensmarty/p/17350420.html

相关文章

  • ElementUI: Uncaught (in promise) cancel 报错
    场景:使用element confirm组件时,点击【取消】按钮,提示错误 Uncaught(inpromise)cancel 代码如下:open(){this.$confirm('此操作将永久删除该文件,是否继续?','提示',{confirmButtonText:'确定',cancelButtonText:'取消',......
  • pip 安装库是报错ERROR: Cannot unpack file C:\Users\LX\AppData\Local\Temp\p
    使用pip安装python库的时候出现报错:ERROR:CannotunpackfileC:\Users\LX\AppData\Local\Temp\pip-unpack-apk_4xkw\simple(downloadedfromC:\Users\LX\AppData\Local\Temp\pip-req-build-htbv29co,content-type:text/html;charset=utf-8);cannotdetectarch......
  • web前端pdf.js预览pdf实例创建报错:Array. prototype` contains unexpected enumerable
    使用pdf.min.js是预览pdf文件,但是在实例化时异常报错,下面是实例化的代码varloadingTask=window.pdfjsLib.getDocument(url);console.log(loadingTask);this.pageNum=1;this.pageRendering=false;this.pageNumPending=null;loadingTask.promise.then((pdfDoc_)=>......
  • uiautomatorviewer.bat 多种报错问题的解决办法
    问题一:使用Android_sdk--tools里的uiautomatorviewer.bat定位页面元素时报错:Remoteobjectdoesn'texists解决办法:使用uiautomatorviewer.bat时要关闭Appium。因为它们都使用同一个端口来连接模拟器。 问题二:使用uiautomatorviewer.bat定位页面元素时报错:Unexpected......
  • springboot集成redis时总报错Connection refused: no further information: localhost
    nacos上配置的关于redis的key值不是springboot需要的固定写法如:sping.redis.host=spring.redis.port=sping.redis.password=spring.redis.database=我写的是一个自定义的key如com.dream.redis.host改为springboot认识的即可其他和springboot集成的组件类似,切记......
  • grafana面板上出现“Only queries that return single…”异常
    问题现象:数据读取不上,如下图问题根因:启动停止了很多次postgresexport并且这个数据库环境进行恢复快照过,导致Prometheusserver中存在相同实例不同的数据,进而导致通过语法无法确定唯一的数据出现报错。解决方案:通过Prometheus的api对实例的数据进行清空,让export重新获取新的数......
  • npm i 报错 unable to resolve dependency tree
    错误:问题原因:安装包各个版本冲突解决办法:npmi--legacy-peer-deps忽略各种报错命令npmi--legacy-peer-deps--ignore-scripts--registry=https://registry.npm.taobao.org然后重新安装 npminstall 或者 cnpmi ......
  • Java 常见报错解决方案
    1.常见的java异常分类Throwable类有两个直接子类:Exception:出现的问题是可以被捕获的Check异常:派生自Exception的异常类,必须被捕获或再次声明抛出Runtime异常:派生自RuntimeException的异常类。使用throw语句可以随时抛出这种异常对象thrownewArithmeticException(…);......
  • 记录一次nodejs操作mongodb报错
    记录一次使用Mongoose操作mongodb报错Mongoose查询回调函数报错BookModel.findOne({name:'Rust'},(err,data)=>{if(err){console.log('读取失败');return;}//输出data变量的值console.log(data);......
  • ubuntu创建conda虚拟环境时报错如下:
     ubuntu创建conda虚拟环境时报错如下:Fetchingpackagemetadata...  CondaHTTPError:HTTP000CONNECTIONFAILEDforurl<https://mirrors.tuna.tsinghu  a.edu.cn/anaconda/cloud/msys2/linux-64/repodata.json>  Elapsed:-  AnHTTPerroroccurr......