#include<stdio.h>
main()
char a,m;
printf("1st\n");
scanf("%c",&a);
printf("2nd\n");
scanf("%c",&m);

Dani AI

Generated

Short summary and a robust fix.

As explained, the behavior comes from how %c treats input: it reads the very next byte from stdin, including whitespace left by the Enter key. That is why using %d appeared to "fix" it — numeric conversions skip leading whitespace. The practical rule: prefer line-oriented input for interactive programs and treat scanf as token-oriented, not line-oriented.

Two safe approaches that avoid undefined tricks (like fflush(stdin)):

Read a full input line and take the first character. This is simple, portable and predictable:

char buf[64];
if (fgets(buf, sizeof buf, stdin))
    a = buf[0];
/* repeat for the next prompt */

Or consume the rest of the current line explicitly with getchar() before reading the next character:

int ch;
while ((ch = getchar()) != '\n' && ch != EOF) { /* discard */ }
/* now read the next character (getchar or fgets) */

Notes and cautions: fflush(stdin) is undefined behavior in standard C; avoid it. Mixing scanf for tokens and raw getchar/fgets for lines works but be deliberate — prefer a single strategy (read lines with fgets, then parse with sscanf/strtol) for robust code. As hinted, small format tweaks can mask the symptom, but reading and parsing lines is clearer and less error-prone for real programs.

Recommended Answers

All 3 Replies

It's not skipping input, you already supplied it. Say you enter 1 for the 1st number. What you really enter is:

'1' and '\n'.

The '1' is read into 'a' and the '\n' remains in the stream. When calling scanf() a next time it reads the newline character that was left in the buffer.

There's a couple of ways to solve this, one being:

#include<stdio.h>
#include<math.h>

main()
{
    char a,m;

    printf("1st\n");
    scanf("%c",&a);

    fflush(stdin);

    printf("2nd\n");
    scanf("%c",&m);

    return 0;
}

It's generally a bit shady to flush inputs streams though. Your goal is to ensure the buffer is empty before the next scanf(). You could also do something like this:

#include<stdio.h>
#include<math.h>

char read_character (void)
{
    char result = 0, symbol = 0;

    // Read the first character.
    scanf("%c", &result);

    // Read until you encounter a newline.
    while (symbol != '\n')
    {
        scanf("%c", &symbol);
    }

    return result;
}

main()
{
    char a = 0,m = 0;

    printf("1st\n");
    a = read_character();

    printf("2nd\n");
    m = read_character();

    return 0;
}

Another option would be to read a stream using fgets() and then obtain the first character from that. The idea remains the same.

-edit-
To awnser why it does run when using int instead of char is that when using an int a newline isn't a valid value for it so it's skipped.

do you mean it is taking the input from the first or is skipping over taking in another value for the second variable?

do you mean it is taking the input from the first or is skipping over taking in another value for the second variable?

already answered by Gonbe it takes the '\n' input from the first variable

although just have to point out that the first one stated (using fflush()) might not always be the optimal solution... here's why link to article

other solutions would be to use

scanf(" %c",&m); //notice the whitespace before %c

to catch the trailing newline

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.