LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 08-22-2005, 06:08 AM   #1
sateeshgalla
LQ Newbie
 
Registered: Apr 2005
Location: Bangalore
Posts: 16

Rep: Reputation: 0
Angry scanf(), ......see the fun...whats the reason?


Hi,

I am Sateesh. I wrote a simple C code. it compiles well. but when run, it goes errotically.
please check out the code and give the reason behind it why its work like that and how to correct it.

the error is it asks for character only twice. and gives output as

$ a:a b: c:b
$

Here is the code.

int main()
{

char a,b,c;
scanf("%c", &a);
scanf("%c", &b);
scanf("%c", &c);
printf("a:%c\t b:%c\tc:%c\n",a,b,c);
return 0;
}

if add \n before %c , it works fine. why?

Thanks in advance.
 
Old 08-22-2005, 06:20 AM   #2
bigearsbilly
Senior Member
 
Registered: Mar 2004
Location: england
Distribution: Mint, Armbian, NetBSD, Puppy, Raspbian
Posts: 3,515

Rep: Reputation: 239Reputation: 239Reputation: 239
It is working.
scanf is picking up the <RETURN> as a character.
it just picks up the character, YOU have to parse it!

It's often best to get a whole line and parse the input yourself.
Interpreting user input is always the trickiest part of any task


you'll notice if you type three characters *the* press return it works.
 
Old 08-22-2005, 06:37 AM   #3
sateeshgalla
LQ Newbie
 
Registered: Apr 2005
Location: Bangalore
Posts: 16

Original Poster
Rep: Reputation: 0
hi billy,
thak you very much. i understood.
when you scan a character, it takes any character \n or blank..everything is a character.
when you scan a integer, it doesnot take \n or blank character and when press enter also it doesnot accept \n and waits for integer value.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
How to use the scanf( ) int C ? naihe2010 Programming 7 11-30-2005 02:35 AM
scanf question jnusa Programming 4 01-07-2005 09:04 AM
scanf() problem. LUB997 Programming 3 12-19-2004 08:03 PM
scanf blackzone Programming 1 08-04-2004 01:50 AM
scanf help homerz Programming 2 11-11-2003 05:48 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 12:03 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration