The following program should take 4 numbers but it takes 5 numbers and prints first 4 number entered. How to correct the problem???

#include<stdio.h>
#include<conio.h>

void main()
{ 
	int a1,a2,a3,a4;
	printf("Enter 4 numbers \n");
	scanf_s("%d %d %d %d ", &a1,&a2, &a3, &a4);
	printf("The 4 numbers entered are %d %d %d and %d",a1,a2, a3, a4);
	_getch();
}

Output

Enter 4 numbers
6
7
8
9
1
The four numbers entered are 6 7 8 and 9

Recommended Answers

All 4 Replies

Hi,
When I used scanf instead of scanf_s, it printed the output properly... I am not aware of scanf_s... googled it and found it out its some secure version of scanf or something... i use unix and this is available only for MS C I guess...

scanf("%d %d %d %d ", &a1,&a2, &a3, &a4);

Hi,
When I used scanf instead of scanf_s, it printed the output properly... I am not aware of scanf_s... googled it and found it out its some secure version of scanf or something... i use unix and this is available only for MS C I guess...

scanf("%d %d %d %d ", &a1,&a2, &a3, &a4);

I doubt that IT printed out properly. Maybe you retyped it, because on my compiler (gcc) problem is with blank space after last %d that exists in his code, and so scanf asks for continuation of input... Or something :/ pretty weird function

You should not give that space at the end...

scanf("%d %d %d %d", &a1,&a2, &a3, &a4);

Ya, u are right, The problem was because of the blank space
Many Thanks........
I was just hanged about an hour for such a simple problem

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.