-4

I have the code:

#include <stdio.h>
int main(void) {
    int c;
    c = getchar();
    putchar(c);

    return 0;
}

and after compiling and running, when I input k for example, it prints out k%. Why is it printing out %?

Edit: I tested a few things out and realized that it's the shell (I'm using zsh with oh-my-zsh configuration which is pretty awesome) that's doing that in order to get to a new line. I attached putchar('\n') at the end of the main() function and it doesn't print that out. Thanks for the helpful comments.

(Please let me know the reasons for the downvotes so I could improve my further questions in the future)

hwkd
  • 2,411
  • 3
  • 18
  • 27
  • 3
    Are you sure it's not some other program printing the `%`? – David Schwartz Feb 19 '16 at 09:34
  • Nitpick: `getchar` returns and `putchar` expects `int` datatype. – haccks Feb 19 '16 at 09:36
  • 7
    shell prompt .. – BLUEPIXY Feb 19 '16 at 09:36
  • Add `putchar('\n')` at the end of `main` and see what happens. – Jabberwocky Feb 19 '16 at 09:38
  • Your code is not the reason why % is being shown. It's your ide or something else that is printing %. I have tested your programme in ubuntu terminal. It's showing correct. Tell me where are you running the code? – Kevin Pandya Feb 19 '16 at 09:39
  • Nitpick: `main()` should be `int main (void)`, and you should `return 0;` at the end. Technically, this code is non-standard C, so your code invokes undefined behaviour – Elias Van Ootegem Feb 19 '16 at 09:41
  • @EliasVanOotegem I love how perfectly good code becomes "undefined behaviour" overnight when a new version of the standard gets ratified by ISO. Or does it happen when your national standards body votes to accept it? There must be a period of time when the same code is undefined in the USA but is perfectly good in Brazil then? I'm confused. – n. m. could be an AI Feb 19 '16 at 09:56
  • Change your prompt to something recognisable in your login dot file. – Martin James Feb 19 '16 at 10:07
  • @n.m.: This could could be considered valid C89 code. But since then, C99 and C11 have been published, and they removed the implicit `int` return type from the standard. This code is, and will always be standard for a C89 compiler, its behaviour is undefined if you run it past a C99 (or C11) compliant compiler though. Geography doesn't matter. Given that C89 is still fairly common (default compiler mode of gcc for example is gnu89), many compilers will be happy to churn out a working binary, but you'll hit a snag once you run `gcc -std=c99 code.c -Wall`, because of the UB according to C99 – Elias Van Ootegem Feb 19 '16 at 10:16
  • Once a new C standard is released, it's _"the"_ standard. Same in Brazil, UK, US, or South-Pole. It just doesn't affect you unless you decide to use a different compiler that implemented the latest version of the language. TL;TR: code that was fully standard compliant yesterday, and followed the C89 standard will always be free of undefined behaviour, provided you compile it as C89 code. OP did not specify what standard he's using, but he did say he's new/learning C. I figured he might as well adopt the latest standards from the start – Elias Van Ootegem Feb 19 '16 at 10:17

1 Answers1

1

A couple of things could be causing that % sign to appear:

Your program outputs k without a new line, and your shell prompt just looks like this:

% 

Meaning that you run the program like so:

% ./a.out
k //getchar
k% //putchar + exit + shell prompt

So in short: the % isn't part of the output.

There is of course the problem with your code triggering UB: the implicit int return type is no longer part of the C standard since C99 and up, and your main function is not quite right, some standard compliant main functions are:

int main(void);
int main (int argc, char **argv);
int main (int argc, char *argv[]);

using () is not the same thing.

Lastly, you're not returning anything from main, which you should do, just add return 0 at the end.

Elias Van Ootegem
  • 74,482
  • 9
  • 111
  • 149