c programing

i have compiled the following code

struct book{
	char name;
	float price;
	int page;
int main(){
	printf("entr the data\n");
	scanf("%c %f %d",&b1.name,&b1.price,&b1.page);
	scanf("%c %f %d",&b2.name,&b2.price,&b2.page);
	scanf("%c %f %d",&b3.name,&b3.price,&b3.page);
	printf("%c %f %d\n",b1.name,b1.price,b1.page);
	printf("%c %f %d\n",b2.name,b2.price,b2.page);
	printf("%c %f %d\n",b3.name,b3.price,b3.page);
	return 0;

it takes only first two scanf and print all three printf statement but second one is giving garbage value.what is the problem ?thanks in advance.

1 Like

modify your code a little bit :smiley:

scanf("%c %f %d\n",&b1.name,&b1.price,&b1.page);
scanf("%c %f %d\n",&b2.name,&b2.price,&b2.page);
scanf("%c %f %d",&b3.name,&b3.price,&b3.page);

Maybe try using a getchar() at the end of each scanf. It takes the new-line as a character for b2.

1 Like

how can i use fflush() in above code.

@nitya I dont think so, I tried using fflush(stdin) to clear the input stream, but that didn’t seem to work. I am not so sure if that can be done. Just in case you get to know how to use fflush() to solve this then please do let me know as well. Thanks

When you press ENTER in c it takes it as a character and for %c you store enter. So garbage values are coming. What u need to do is flush the standard input.
Just type fflush(stdin); before each scanf() statement.

it is not working ,it gives garbage value for 2nd printf…i am using code block ide…

could u plz take the pain to explain why it started working after “\n”??

From what I understand, the modification provided by ravi works because the \n in scanf eats the white space you provide in between the two inputs which is the new line(\n) character in this case. Since you hit ENTER after the first series of inputs and the waiting place holder is %c - it reads \n from buffer and moves to the next input(correct me if I am wrong).

You can also put a space to eat white space instead of \n like

scanf("%c %f %d ",&b1.name,&b1.price,&b1.page);

scanf("%c %f %d ",&b2.name,&b2.price,&b2.page);

scanf("%c %f %d",&b3.name,&b3.price,&b3.page);

Another alternative is to clear the buffer using fflush like -

scanf("%c %f %d",&b1.name,&b1.price,&b1.page);

scanf("%c %f %d",&b2.name,&b2.price,&b2.page);

scanf("%c %f %d",&b3.name,&b3.price,&b3.page);

alt text

giving space is working but fflush(stdin) is not working.i am using code blocks.

I forgot to remove the space after using fflush. It works now. Check :slight_smile:

it’s not working even after removing space

fflush will not work in this case

It works in Code::blocks 12.11. I ll add an image.

@grvana it just eats up the enter key that you press after your first input, so that the value of your character of the second scanf doesn’t take the value \n, but the desired character.

1 Like

@thezodiac I tried this and it works in my code blocks now… But initially when this question was posted I was using Dev-C++ and it didnt work in it. Does this mean that this is not a kind of “universally accepted” solution, and might not work everywhere. Just like it doesnt work in nitya’s code blocks???

Change your scanf as below. See change in space. Live example here.

scanf(" %c%f%d",&b1.name,&b1.price,&b1.page);  /* Extra space prepended */
scanf(" %c%f%d",&b2.name,&b2.price,&b2.page);
scanf(" %c%f%d",&b3.name,&b3.price,&b3.page);

check how scanf respond for space character. It says:

Whitespace character: the function
will read and ignore any whitespace
characters encountered before the next
non-whitespace character (whitespace
characters include spaces, newline and
tab characters – see isspace). A
single whitespace in the format string
validates any quantity of whitespace
characters extracted from the stream
(including none).

Which means always use space before %c in scanf if you want to ensure that the character read is not a white space.

1 Like

@coderzz027 Never fflush(stdin) as the behaviour of flushing stdin in C is an undefined behaviour.

Never fflush(stdin) as the behaviour of flushing stdin in C is an undefined behaviour.

Never fflush(stdin) as the behaviour of flushing stdin in C is an undefined behaviour.

1 Like