Switch to full style
For C/C++ coders discussions and solutions
Post a reply

using of scanf and printf functions

Mon Oct 11, 2010 6:10 pm

pls can anyone help me ?? i have a problem with the following code...The code is ..
Code:
main()
{
   int a,b;
   scanf("%d%d",&a,&b);
   char x;
   scanf("%c",&c);
   switch(x);
   {
     case'A':
     printf("good");
     break:
     default:
     printf("bad");
}

The output of following code in C is "bad" but problem is that during execution,it only asks for the values of a &b.It doesnt ask for the value of x whose datatype is char.Why So?
The Same program if executed in C++ using cout and cin works fine n even asks for the value of x.Why So that the above code works for C++ and not in C?

pls help me...



Re: code problem

Mon Oct 11, 2010 10:33 pm

Code:
scanf("%c",&c);

is think it should be
Code:
scanf("%c",&x);


Re: code problem

Wed Oct 13, 2010 12:53 pm

use \n before














use \n before %c in scanf statement as shown below......
main()
{
int a,b;
scanf("%d%d",&a,&b);
char x;
scanf("\n%c",&c);
switch(x);
{
case'A':
printf("good");
break:
default:
printf("bad");
}

Re: code problem

Sat Oct 16, 2010 8:28 am

ya thank u but can u pls tell me whats the significance of '\n' here in the program....pls

Post a reply
  Related Posts  to : using of scanf and printf functions
 why to use scanf in c programming     -  
 PHP Array Functions     -  
 What are Inline functions?     -  
 What are Virtual Functions?     -  
 Functions and References     -  
 Calling Functions Dynamically     -  
 Lets Learn C++----->(Lesson 5) Functions     -  
 program to run shell command line functions     -  
 character running automatically / jump /duck/ functions     -