user1615559
user1615559

Reputation: 383

C runtime error with for loop

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!

Upvotes: 2

Views: 3940

Answers (3)

Keith Thompson
Keith Thompson

Reputation: 263177

There are several problems in your code that could in principle cause some kind of run-time error, but there's nothing that I'd expect to cause such an error.

#include <stdio.h>

int array[10];

void main() {

As others have mentioned, this is not one of the standard ways to define the main function. For your purposes, since you're not using command-line arguments, the correct definition would be:

int main(void) {

The C standard (1990, 1999, and 2011 editions) does permit a a hosted implementation to permit other forms for main; for example, Microsoft's compiler specifically permits void main(void) (and void main() would probably be acceptable as well). But there is no good reason to define main with a return type of void, since int main(void) is guaranteed to be correct.

If you define main using something other than one of the two standard forms (the other is int main(int argc, char *argv[]) or in a manner that your implementation documents, then your program's behavior is undefined.

On the other hand, the worst consequence I've seen for using void main() is a compile-time warning -- and the standard doesn't even require that. A compiler that accepts void main() without complaint and then generates code that causes a run-time error has, I would say, a serious bug, though it wouldn't actually violate the C standard. I'd be surprised if this were the cause of the error you're seeing.

    int i;

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

This is perfectly ok; it should print 0123456789. Printing multiple int values with nothing separating them is a bit odd, but it's not an error as such. ("%i" is equivalent to "%d", and "%d" is more common.)

But your output is not terminated by a newline. On many systems, the output will just be printed without an end-of-line marker, which might mess up your terminal display. On others, the trailing newline might be required, and failing to provide one causes undefined behavior. (You don't need a newline on all your output, just at the very end.)

    }

I'd add something like

putchar('\n');

here, just to be sure.

    return;

A return; with no expression is perfectly valid in a void function -- though it's unnecessary at the very end of the function, since reaching the closing } does an implicit return anyway. But if you define main correctly as int main(void), then this should be return 0;. Starting in C99, reaching the closing } of main is equivalent to executing a return 0;, so it's not strictly necessary -- but there are still compilers that don't fully support C99 (much less C11), so the adding the return 0; isn't a bad idea.

}

Here's a version of your program that corrects these issues, and should be 100% portable to all conforming hosted C implementations:

#include <stdio.h>

int array[10];

int main(void) {
    int i;

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

The expected output is:

0123456789

Try this and see if it corrects the run-time error. If it does, try starting with your original program and changing just one thing at a time (the definition of main along with the return statement, and the trailing newline) to see exactly what it is that corrects the problem.

It would be very helpful to know what compiler and operating system you're using.

(I've mentioned "hosted implementations". The other kind of C implementation is "freestanding"; it refers to implementations for embedded systems, where code may run directly on the hardware with no operating system. On such a system, most of the standard library is optional, and the correct definition of the program entry point is entirely implementation-defined; it might not even be called main. You're probably not using such an implementation.)

Upvotes: 0

Vineet1982
Vineet1982

Reputation: 7918

I dont know which compiler you are using. Since most the cases it depends on the compiler and OS behaviour. Different compiler provide different behaviour

Miscrosoft Compiler

In case using Microsoft Compiler then it will not show any Warning Message while compiling. Thus some times developer doesn't know what the reason of things. In your case the same thing is happening you either don't use return statement or if you want to use return then use change the things as provided by Vlad. Full function would be like this

Gcc Compiler

In case you are using GCC it will generate Warning Message as the default valid type under GCC is int main and return 0; in the end.

using void

#include <stdio.h>

int array[10];

void main(void) {
    int i;

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

using int

#include <stdio.h>

int array[10];

int main(void) {
    int i;

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

Another Example of using void with return

#include <stdio.h>

int array[10];

void main(void) {
    int i;

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

The above example will not generate error as the function doesn't reached the loop ending and you have returned before. Thus, here no memory access voilation would not be generated.

Upvotes: 1

Vlad from Moscow
Vlad from Moscow

Reputation: 310910

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.

Upvotes: 3

Related Questions