LinuxQuestions.org
Visit Jeremy's Blog.
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 12-01-2009, 06:45 AM   #1
eeepc4gsurf
LQ Newbie
 
Registered: Nov 2008
Posts: 10

Rep: Reputation: 0
Extra "10" printed after every conversion from ASCII to int.


Hello everyone. I have a very basic program which I wrote, to print the integer equivalents of an ASCII character. The code is below:
Code:
#include<stdio.h>

int main(void)
{
    char c;
    while (1)
    {
        scanf("%c", &c);
        printf("%d\n", c);
    }
}
The code is supposed to take a character as input and print the integer equivalent of that character. But the problem is that, after printing the integer equivalent, it prints an extra '10', every time.

Code:
f
102
10
g
103
10
My question is: Why does this extra '10' always come? When the code is just a simple:

Code:
#include<stdio.h>

int main(void)
{
    char c;
    scanf("%c", &c);
    printf("%d\n", c);
}
The code works just fine. There is no extra '10' displayed. I am using Ubuntu 9.10 with gcc-4.4.1.

Thanks in advance
 
Old 12-01-2009, 06:53 AM   #2
Guttorm
Senior Member
 
Registered: Dec 2003
Location: Trondheim, Norway
Distribution: Debian and Ubuntu
Posts: 1,453

Rep: Reputation: 448Reputation: 448Reputation: 448Reputation: 448Reputation: 448
The reason is that you hit enter after the character, and its ascii value is 10. Stdin is line buffered so when you type a character, the character is put in the buffer. The scanf don't get the character until a newline is met. It then reads the character + the newline next time in the loop.
 
Old 12-01-2009, 07:03 AM   #3
eeepc4gsurf
LQ Newbie
 
Registered: Nov 2008
Posts: 10

Original Poster
Rep: Reputation: 0
So what can I do to stop this behaviour?
 
Old 12-01-2009, 07:31 AM   #4
Hko
Senior Member
 
Registered: Aug 2002
Location: Groningen, The Netherlands
Distribution: Debian
Posts: 2,536

Rep: Reputation: 111Reputation: 111
Except for some exceptions, I would recommend against scanf().
Code:
#include<stdio.h>

int main(void)
{
    int c;

    while (1) {
        c = getchar();
        if (c == EOF) { /* Stop on end of file (or ctrl-d) */
            break;
        }
        getchar();  /* read but discard next char (newline) */
        printf("%d\n", c);
    }
    return 0;
}
 
Old 12-01-2009, 07:32 AM   #5
eeepc4gsurf
LQ Newbie
 
Registered: Nov 2008
Posts: 10

Original Poster
Rep: Reputation: 0
Thanks.
 
  


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
Save "xinput set-int-prop" properties to xorg.conf or elsewhere? slacky Linux - Hardware 3 05-20-2009 11:44 AM
a2ps "Printed by" header empty rtra Linux - Software 1 04-15-2007 09:03 AM
Segmentation fault with asm("int 0x0f") justthething Programming 1 08-02-2004 01:43 PM
"Extra" moderators in the "Distro-forums" : Just curious... Megamieuwsel LQ Suggestions & Feedback 3 07-10-2004 04:18 AM
Java does "age" or "Age" matter when declaring an Int?? Laptop2250 Programming 3 10-13-2003 12:34 PM

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

All times are GMT -5. The time now is 11:31 PM.

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