below two are same program except i interchange to accept input value ....in the first program i accept first using scanf and the by getchar()...which didnt allow to accept character value . but in the second program i first accept getchar()value and then scanf(). so it works fine but i dont understand why frist value doesnt work. so please help me out

#include<stdio.h>
#include<string.h>

int main()
        {
        char ch;
        char name[20];
        printf("enter your name\n");
        scanf("%s",&name);

        printf("enter single value\n");
        ch=getchar();

}

output
enter your name
lobsang
enter single value       //not allowing to enter the value here
lobsiphone:cprogram gyurmey$ 

........................................................................................................
#include<stdio.h>
#include<string.h>

int main()
        {
        char ch;
        char name[20];

         //  printf("enter your name\n");
        //  scanf("%s",&name);

        printf("enter single value\n");
        ch=getchar();

        printf("enter your name\n");
        scanf("%s",&name);

}

output

enter single value
a
enter your name
lobsang
lobsiphone:cprogram gyurmey$

Edited 6 Years Ago by Nick Evan: Added code-tags

In the first one, you respond [I]name[/I]\n (where the \n is the result of pressing the [enter] key. The %s with scanf stops at whitespace, which \n is, so that is leftover in the input buffer. This will immediately satisfy the subsequent call to getchar .

Since getchar returns an int , ch should be an int .

User Input: Strings and Numbers [C] may be of interest to you. Also, look into code tags.

Edited 6 Years Ago by Dave Sinkula: n/a

BTW
Drop the & in scanf(), it is either scanf("%s", name) or scanf("%s", &name[0]) , but not scanf("%s", &name)

it didnt solve my problem yet after your suggestion... in my program if i read using getchar() function before scanf function...my program works fine but if i read scanf before getchar().. it doesnt allow me to input value for getchar.

Comments
Answered in #2.

Right away you shouldn't use:

char name[20];
scanf("%s",&name);

It should be:

char name[20];
scanf("%s",name);

Wasn't this just posted?

Also main should return a value.

Edited 6 Years Ago by gerard4143: n/a

Here's a little hack that'll work

#include<stdio.h>
#include <stdlib.h>
#include<string.h>

int main()
{
	char ch;
	char name[20];
	printf("enter your name\n");
	scanf("%s%c",name, &ch);

	printf("enter single value\n");
	ch=getchar();

	fprintf(stdout, "name->%s, char->%c\n", name, ch);

	exit(EXIT_SUCCESS);
}

That said you probably should write your porgam something like below

#include<stdio.h>
#include <stdlib.h>
#include<string.h>

int main()
{
	char ch;
	char name[20];
	printf("enter your name\n");
	fgets(name, 19, stdin);

	printf("enter single value\n");
	ch=getchar();

	fprintf(stdout, "name->%s, char->%c\n", name, ch);

	exit(EXIT_SUCCESS);
}

Edited 6 Years Ago by gerard4143: n/a

Right away you shouldn't use:

char name[20];
scanf("%s",&name);

It should be:

char name[20];
scanf("%s",name);

Actually, no it shouldn't. See this.

every time we receive input using scanf it leaves a "\n"(enter key) and if we use getchar() to receive next input..... before getchar() takes input from user..it takes \n left by scanf as input. so user will not be able to write input to getchar() as it already received input release by scanf and it terminates without a value send to getchar().

fflush(stdin) doesnt work since c compiler doesnt flush stdin ...it support only stdout.

best solution is receive character using getchar() twice right after scanf() function. so that the first getchar() recived \n but the second time getchar()will over write the first one with user input.

simple
scanf("%s",name)
c=getchar();
c=getchar();// this will receive user input

This question has already been answered. Start a new discussion instead.