0
#include<stdio.h>
void main() {
char ch;
ch=getchar();
printf("%d",ch);
}

This gives me error NZEC (non zero exit code) but when I use-

#include<stdio.h>
void main() {
char ch;
scanf("%c",&ch);
printf("%d",ch);
}

No execution error and accepts solution What's wrong with getchar() ?

I am quite new to C so don't know much I checked few answers on difference between scanf and getchar but I could not understand. Please help me in understanding this behaviour.

Solution: I did not know that my query is related to this question: What should main() return in C and C++?

Moreover both of these are working-

#include<stdio.h>
void main() {
int ch;
ch=getchar();
printf("%d",ch);
}

changing char to int and-

#include<stdio.h>
int main(void) {
char ch;
ch=getchar();
printf("%d",ch);
return 0;
}

changing void to int

bUff23
  • 163
  • 12
  • 2
    The `main` function is supposed to be declared to return an `int`. It's part of the C specification. Not doing that leads to [*undefined behavior*](https://en.wikipedia.org/wiki/Undefined_behavior). See e.g. [this `main` function reference](http://en.cppreference.com/w/c/language/main_function) for more information. – Some programmer dude Apr 15 '18 at 10:48
  • *This gives me error NZEC (non zero exit code)* Thats what you get with `void main()` – wildplasser Apr 15 '18 at 10:49
  • did you happen to get any warning from the compiler? – Antti Haapala Apr 15 '18 at 11:25
  • I ran this on online judge for hacker earth ...there I got execution error but the code compiled succesfully. – bUff23 Apr 15 '18 at 11:28

2 Answers2

2

Both are undefined behaviours because main should return int as defined by the C standard. It's especially important when you are checking its return code.

Change the definition to:

int main(void)
{
    ...
}

Note that since C99, main doesn't need to explicitly return any value. It's as if you had return 0; at the end. But if you are using C89, you are required to return a value explicitly (or call exit).

P.P
  • 106,931
  • 18
  • 154
  • 210
  • it worked by this as well as by changing `char ch` to `int ch` as suggested by @user3121023 One more question @usr since I am learning c which standard should I use and how do I actually learn it any good sources if u can recommend so that someday I could answer like you thanks! – bUff23 Apr 15 '18 at 11:09
  • Try to use the latest standard C11 if you can (but support/use of C11 is not widespread). Otherwise, I'd suggest to follow C99. – P.P Apr 15 '18 at 11:15
  • any good sources for c learning that u wud like to recommend – bUff23 Apr 15 '18 at 11:19
  • K&R 2nd edition is good for exercises (bit the book itself is outdated). I heard "C Programming: A Modern Approach" is quite good (covers c99). But depending your level, you might also like "C How to Program (8th Edition)" too. But others may different opinions. You can search and read about (the quality of) others textbooks. – P.P Apr 15 '18 at 11:32
1

Your main() should returns int instead of void As its specified in n1570 standard

It shall be defined with a return type of int and with no parameters: int main(void) { /* ... */ }

Replace error code blocks as

int  main() {
        char ch;
        ch=getchar();
        printf("%d",ch);
        return 0;
}
Achal
  • 11,629
  • 2
  • 14
  • 34