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

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.