-2
#include<stdio.h>

void main() {
   int a[3];
   a[0]=1;
   a[1]=2;
   a[2]=3;
   printf("%d", a[2]);
}

It isn't showing any errors or warnings. But it isn't running

Jabberwocky
  • 48,281
  • 17
  • 65
  • 115
chayan
  • 5
  • 4

1 Answers1

1
 void main(){

is non-standard. main() function should return an int. Some IDEs/platforms check the return the value of the process. So this might be a problem. Change it to:

 int main(void){

If you are using C89 then you should also have a return statement from main(). This is not required since C99. In C99 and later, main() will implicitly return success if the control reaches the end of main as if you had: return 0; at the end of main() function.

In C89/C90, you must have a return 0; or return EXIT_SUCCESS; at the end of main(). Otherwise, it leads to undefined behaviour. But this is not required in C99 and C11. There's obviously no other problem in your code except this. So if you still have issues, you need to provide more details about your environment/compiler.

P.P
  • 117,907
  • 20
  • 175
  • 238
  • You forgot to mention the `return 0;` at the end. – Jabberwocky Jan 28 '16 at 15:14
  • @MichaelWalz: This is not required for `main`! http://port70.net/~nsz/c/c11/n1570.html#5.1.2.2.3p1 – too honest for this site Jan 28 '16 at 15:15
  • @MichaelWalz Since C99, it's not required to have explicit `return` from C99. Nonetheless, worth adding. – P.P Jan 28 '16 at 15:17
  • Thanks I ignored that. – Jabberwocky Jan 28 '16 at 15:17
  • Tried adding int to main and return 0. But it still won't run – chayan Jan 28 '16 at 15:25
  • 1
    @ChayanKathuria "won't run" isn't giving any useful information about the failure. What *exactly* happens? It doesn't give output/crashes? There's no problem with your code. So it's your environment/compiler or malware not allowing you to run executables, etc be a problem. – P.P Jan 28 '16 at 15:30
  • @I3x as soon as I press Ctrl+f9, it flashes something for a micro second and that's it. It doesn't take me to the output window. – chayan Jan 28 '16 at 15:33
  • 1
    @ChayanKathuria Probably it outputs and exits the windows, add `system("pause");` at the end of main() before "return 0;`. – P.P Jan 28 '16 at 15:36