Jason Yang-Jhu Chen
Jason Yang-Jhu Chen

Reputation: 37

scanf character input/output

scanf not working as expected with a normal input character

So I was just trying out the scanf() and a normal input/output of this function. I know that I had to leave a space before the input character and the operand% so my code is as below. Somehow I don't understand why whatever the input I inserted the output remains 0.

 #include<stdio.h>

 int main()
 {
  char c ;

  scanf(" %c",&c);
  printf("%c",c);

  return 0;
  }

I was expecting the output will be whatever the character I insert. For example, insert an "A" via keyboard and the output will be exactly an "A".


I'm using a vim environment to edit my code, but I found that if I run this code on codeblocks it works. What's the difference?

Upvotes: 1

Views: 567

Answers (2)

Luis Colorado
Luis Colorado

Reputation: 12635

I've tried to run your program (exactly as it appears in your question)

pru.c

 #include<stdio.h>

 int main()
 {
  char c ;

  scanf(" %c",&c);
  printf("%c",c);

  return 0;
  }

and with the input you posted in your question (just A plus Enter) and got the following result:

$ cc pru.c
$ ./a.out
A
A$ _

which is exactly the expected output. So the problem must be in another place, or you have a completely different scenario and need to provide more information.

I tested this on a PC (Intel Core Duo) with FreeBSD 12.0/CLANG compiler. (here $ is the unix prompt and _ is the cursor after the run) And of course, the program has been edited with vi(1) (this has no impact on the result).

Edit

Try to change

  printf("%c",c);

by this

  printf("0x%02x, [%c]\n",c,c);

so, you'll get an hex dump of the character just input, and also its representation as printed. The \n at the end is to ensure your shell prompt is not eating (overwriting) the last line output of your program (mostly if you have changed the prompt variable PS1) hidding the printed char.

That should produce (on your posted input) the following output:

$ cc pru.c                <--- compilation of new pru.c
$ ./a.out                 <--- default name for your program executable.
A                         <--- this is your input (followed by <return>)
0x41, [A]                 <--- this should be your program output.
$ _                       <--- prompt (and cursor) after your program execution

Upvotes: 1

hyde
hyde

Reputation: 62777

It appears you are not providing any non-whitespace (which are eaten away by th space in the format string, including newlines) characters (or perhaps nothing at all) from standard input. If this happens, scanf will fail to parse a char and leaves c uninitialized.

Using uninitialized variable is is Undefined Beheavior, so in theory anything could happen. In practice, from your description, it sounds like memory reserved for c happens to have byte value 0, which is unprintable character, so printf prints something else (maybe /0, maybe nothing). And then the environment (vim) might show you the program exit code, also 0 here (assuming the Undefined Behavior doesn' cause your program to crash).

To fix this, check return value of scanf:

 #include<stdio.h>

 int main()
 {
   char c ;

   int r = scanf(" %c",&c);
   if (r==1) {
      // Always print something and add newline
      // to be sure we see some output always.
      printf("c='%c'\n",c);
   } else {
      printf("scanf error: %d\n", r);
      // If r==-1, errno variable tells what error was
   }
   return 0;
 }

Practical hint: To provide standard input when there is no terminal (so you can't type the input), you can pipe something:

echo A | ./thisprogram

Upvotes: 4

Related Questions