for循环的C运行时错误 [英] C runtime error with for loop

查看:179
本文介绍了for循环的C运行时错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我写了一小段代码,我认为会工作,我得到的所有值显示在控制台,但不久之后,我得到一个运行时错误。有人知道为什么吗?

  #include< stdio.h> 

int数组[10];

void main(){
int i; (i = 0; i <10; i ++){
array [i] = i;


printf(%i,array [i]);
}
return;



$ b $ p $输出:

 运行时错误时间:0内存:2248信号:-1 
0123456789

任何帮助将不胜感激,谢谢!

解决方案

  int main()

并删除语句 return; 或将其替换为返回0;



将main函数声明为void,并使用没有表达式的return语句会导致程序未定义的行为。

I wrote a small snippit of code that I thought would work, and I am getting all the values displayed in the console but shortly thereafter I am getting an runtime error. Anyone have any idea why?

#include <stdio.h>

int array[10];

void main() {
    int i;

    for(i = 0; i < 10; i++){
    array[i] = i;    
    printf("%i", array[i]);
    }
    return;
}

Output:

Runtime error   time: 0 memory: 2248 signal:-1
0123456789

Any help would be appreciated, thanks!

解决方案

Declare function main as

int main() 

and either remove statement return; or substitute it for return 0;

Declaring function main as void and using return statement without an expression results in undefined behaviour of the program.

这篇关于for循环的C运行时错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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