-1

This code runs, even produces the expected output. However, in the build log, I always get Process terminated with status 2 (0 minute(s), 5 second(s)) or some variation of this log.

Code -

#include<stdio.h>
#include<conio.h>

void main() {

printf("Please enter a character");
char ch = getch();

ch++;
printf("\n%c", ch);


} 

Sample output (the input was 'a' without quotes)-

Please enter a character
b
Sourav Ghosh
  • 133,132
  • 16
  • 183
  • 261
IAmACoder
  • 33
  • 7
  • 4
    Why'd you use `void main()`? use `int main()` , unless explicitly mentioned something else by your compiler documentation. – Sourav Ghosh Sep 11 '18 at 13:54
  • 3
    Possible duplicate of [What are the valid signatures for C's main() function?](https://stackoverflow.com/questions/2108192/what-are-the-valid-signatures-for-cs-main-function) – hellow Sep 11 '18 at 13:55
  • 1
    If the message is output in the _build log_, then it is related the _build_ not the _execution_ of this code. Did you really mean "_build log_"? Does your build also _execute_ the code? That would explain your description, but is unusual and deserves explanation. Either way, if you are in any way relying on or testing the exit value of a program, you should probably explicitly return a value - otherwise why are you testing it? – Clifford Sep 11 '18 at 14:11

4 Answers4

1

If you declare main as int main(void) or int main(int argc, char *argv[]), then the current C standard says that reaching the } that terminates main returns a value of 0. This also holds if main is declared with a return type compatible with int but with parameters defined by the C implementation.

If you declare main differently, as with void main(), the C standard does not say what the behavior is, and it is up to your C implementation.

Eric Postpischil
  • 195,579
  • 13
  • 168
  • 312
1

First of all, I'd recommend to check What are the valid signatures for C's main() function?.

For a hosted environment, void is not a valid return type for main().

That being said, as per this function signature, the return type is void, that indicates the call to main() returns nothing to the caller (environment). Now, from the environment, try to access the returned value [i.e., the prompt you are getting] would be indeterminate, at best. C standard does not define any behavior for that.

Sourav Ghosh
  • 133,132
  • 16
  • 183
  • 261
1

Since you declared main without a valid signature (i.e. you gave it the return type void), you cannot assume anything about the value that is actually returned to the operating system. If you're compiling for the x86 architecture, then here's what's probably happening to explain the returned value of 2:

main:
    ...
    call printf
    add esp, 8
    ; return value from printf is in EAX register
    ret 

printf returns the number of characters written to standard output, 2 in your case with the format string \n%c. The return value from printf is still in the register designated for holding return values (EAX) when you exit from main, so that's what main returns.

Govind Parmar
  • 20,656
  • 7
  • 53
  • 85
0

If you don't return anything from main, any value may get returned. In your case, your program returns 2 as indicated in the message (status 2). You need to return 0 to notify other programs that your program run successfully.

int main() {
   ....
   return 0;
} 
KamilCuk
  • 120,984
  • 8
  • 59
  • 111
  • 1
    The C standard says that, if the return type of `main` is compatible with `int` and control reaches the `}` that terminates `main`, then zero is returned. This is a special specification for `main`, different from other functions. – Eric Postpischil Sep 11 '18 at 13:59
  • To complete what @EricPostpischil said, the `return 0;` in the end is anyway implicit for `main()`. – Sourav Ghosh Sep 11 '18 at 14:00