-1

I have a small C program here that runs till you enter the ! character. It works as expected when you enter the exclamation mark but when you enter any other character some additional hellos that I can't explain are output.

int main() {
    char in;
    while (in != '!') {
        printf("\nENTER:\n");
        scanf("%c", &in);
        for (int i = 0; i < 5; i++) {
            printf("hello %c ", in);
        }
    }
}

Sample session:

C:\Users\hp\Desktop>gcc just.c && a.exe

ENTER:
a
hello a hello a hello a hello a hello a 
ENTER:
hello
 hello
 hello
 hello
 hello

ENTER:
!
hello ! hello ! hello ! hello ! hello !
chqrlie
  • 131,814
  • 10
  • 121
  • 189
  • Uninitialized local variables in C (like your `in` variable) really are *uninitialized*. They will have an *indeterminate* value (which you should look at as a "garbage" value). Don't use such variables in your code, always remember to initialize your variables. – Some programmer dude May 22 '22 at 07:38
  • 3
    As for your problem, the `%c` format doesn't skip leading white-space (which many other formats does). Like for example the newline that was left over from the last input. Change the format from `"%c"` to `" %c"` (note the leading space in the format string). – Some programmer dude May 22 '22 at 07:39
  • @Someprogrammerdude even after initializing the variable i am getting those extra hellos with "in" still having an indeterminate value. – Ajaykrishnan R May 22 '22 at 07:42
  • While the solution might not have been intuitive (unless you read more about [`scanf`](https://en.cppreference.com/w/cpp/io/c/fscanf) and its formats strings) a quick step-through in a [*debugger*](https://stackoverflow.com/questions/25385173/what-is-a-debugger-and-how-can-it-help-me-diagnose-problems) should have made it clear that the newline from the previous input is read. – Some programmer dude May 22 '22 at 07:42
  • 1
    Yes, you entred an `'a'` and a newline. So two sets of 5 hellos. Notice the newline was output with the second set. – Weather Vane May 22 '22 at 07:42
  • In addition to the comments from @Someprogrammerdude, when you access a variable with immediate storage duration (like your `in`) while its value is *indeterminate*, you invoke *Undefined Behavior* and all bets are off with your program. It can appear to run correctly or crash or anything in between. – David C. Rankin May 22 '22 at 07:44
  • I'll close this as one of the (thousands) of duplicates. – Weather Vane May 22 '22 at 07:45
  • Notice how the "Related" questions don't even mention `scanf()`, much less anything about an uninitialized value (which would be impossible), but at least if the "Related" could pick up `scanf()` that would help with finding a duplicate. (good luck finding a good uninitialized variable dupe -- that's a needle in a haystack search) – David C. Rankin May 22 '22 at 07:51

1 Answers1

0

Your code has undefined behavior because in is uninitialized so the first test in != '!' may or may not be true and could eventually cause other side effects on mischievous systems.

The output you observe is consistent with the user typing the keys a, Enter, ! and Enter again.

The Enter key produces a newline character \n in stdin, which is read by scanf() in the second iteration and output in the loop. The second and subsequent hello has a space before it because there is a space after %c in the printf format string.

You should use scanf(" %c", &in); to ignore whitespace and you should test the return value of scanf() to avoid undefined behavior at end of file.

Here is a modified version:

#include <stdio.h>

int main() {
    char in = 0;
    while (in != '!') {
        printf("ENTER:\n");
        if (scanf(" %c", &in) != 1)
            break;
        for (int i = 0; i < 5; i++) {
            printf("hello %c ", in);
        }
        printf("\n");
    }
    return 0;
}
chqrlie
  • 131,814
  • 10
  • 121
  • 189