Here is the second lesser know fact
scanf() return type:
scanf() will return number of variables it succesfully stored new values.i.e,
where a,b,c are integer variables
if we input 12 b
then the value of a will be 1
if we input 12 1
then the value of a will be 2
that means it can be used to verify whether the user has given the values according to the specified data type or not.
here is the problem that was asked in 2nd C puzzle
Code:
#include<stdio.h>
#include<conio.h>
void main()
{
char a;
int b,c;
clrscr();
printf("Enter a character and 2 integer values\n");
scanf("%c%b%c",&a,&b,&c);
getch();
}
This is an program which accepts a character followed by 2 integer values.
NOW ALL I WANT TO DO IN THIS PROGRAM IS TO CHECK WHETHER THE USER HAS ENTERED CORRECT INPUT(FIRST CHARACTER AND FOLLOWED BY THE 2 INTEGER VALUES.
It should display "That's Good" if the user has inputted in correct sequence else it should display "That's Bad".
Here are some sample ouput's
Code:
Output:
Enter a character and 2 integer values
a 23 45.96
That's good
^^ decimal values are rounded off.Hence it is valid input
Code:
Output:
Enter a character and 2 integer values
2 23 45.96
That's good
^^'2' is also a character
Code:
Output:
Enter a character and 2 integer values
2 ad 45.96
That's bad
^^ ad is not a decimal value.
The following are the Rules:
1.The keyword if or operator '?' should come only once in a program
2.No logical operator's are allowed (that means && || ! should not be used)
3.No new variabels must be used
4.No header file other than the existing one should be used
Here is the solution
Code:
#include<stdio.h>
#include<conio.h>
void main()
{
char a;
int b,c;
clrscr();
printf("Enter a character and 2 integer values\n");
if (scanf("%c %d %d",&a,&b,&c)==3)
printf("\n thats good");
else
printf("\n thats bad");
getch();
}
Find more of the C puzzles at *www.thinkdigit.com/forum/showthread.php?t=70697
no contributions

Code:
#include<stdio.h>
#include<conio.h>
void main()
{
char a;
int b,c;
clrscr();
printf("Enter a character and 2 integer values\n");
if (scanf("%c %d %d",&a,&b,&c)==3)
printf("\n thats good\n");
else
printf("\n thats bad\n");
printf("The values are %c %d %d\n",a,b,c)
getch();
}
the above program is going to work correctly.I earlier stated that the output will be some garbage values but it is not so.
Sorry for the mistake
I just wonder how come no one recognised this mistake.That means no one is trying the facts specified here

