Total members 11894 |It is currently Sat Nov 23, 2024 5:28 pm Login / Join Codemiles

Java

C/C++

PHP

C#

HTML

CSS

ASP

Javascript

JQuery

AJAX

XSD

Python

Matlab

R Scripts

Weka





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...




Author:
Newbie
User avatar Posts: 2
Have thanks: 0 time

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

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


_________________
M. S. Rakha, Ph.D.
Queen's University
Canada


Author:
Mastermind
User avatar Posts: 2715
Have thanks: 74 time

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");
}


Author:
Newbie
User avatar Posts: 1
Have thanks: 1 time

For this message the author Shikshakanwar has received gratitude : jestino23

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


Author:
Newbie
User avatar Posts: 2
Have thanks: 0 time
Post new topic Reply to topic  [ 4 posts ] 

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



cron





Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group
All copyrights reserved to codemiles.com 2007-2011
mileX v1.0 designed by codemiles team
Codemiles.com is a participant in the Amazon Services LLC Associates Program, an affiliate advertising program designed to provide a means for sites to earn advertising fees by advertising and linking to Amazon.com