Silly problem in C

9 views
Skip to first unread message

Owais Lone

unread,
May 12, 2009, 3:18:47 PM5/12/09
to Barcamp Kashmir
So here I am trying to print 1 for a, 2 for b, 3 for c etc...

here is the code


Quote:
#include<stdio.h>
#include<stdlib.h>

int main()
{
char a;

while(1)
{
fflush(stdin);
scanf("%c",&a);
fflush(stdin);
printf("%d\n",a-96);
}
return 0;
}
It works fine but prints 10 - 96 after every iteration.
I also tried flushing the input stream but no gain.

Actually it prints 10 but since I'm subtracting 96, it prints 10-96.

And the most important thing. This happens only when I put it inside a
loop. Without a loop it works fine.

Athar Raouf

unread,
May 13, 2009, 12:59:41 AM5/13/09
to Barcamp Kashmir
I Don't have much knowledge of your "C"
But It appears to me that you are reading the number into a char
variable at: scanf("%c",&a) , as %c is for char.........
It should better be %d or %f.............

Check out this link...
http://www.geocities.com/learnprogramming123/Clesson4Beginner.htm

Mir Nazim

unread,
May 13, 2009, 3:17:49 AM5/13/09
to barcamp...@googlegroups.com
try type coersion.

printf("%d\n",((int)a)-96);
--
Regards
Mir Nazim
Phone: +91 9469071855
Skype/GTalk: mirnazim

Owais Lone

unread,
May 13, 2009, 4:39:30 AM5/13/09
to Barcamp Kashmir
What I'm doing is 100% correct. I know using (int) is the official way
in C. But this is supposed to work too. as the char 'a' == int 97
Anyways I've found a workaround,

here is the modified code


#include<stdio.h>


int main()
{
char a;

while(1)
{
scanf("%c",&a);

if(a!='\n')
printf("%d\n",a-96);
}
return 0;
}


The problem here is that 'return' ascii value 10 remains in the input
buffer stream. So in the next iteration the loop just skips scanf() as
it takes input from the stream.
So the user get to input again in the third iteration. Sound good at
the face of it but it doesn't work if I use fflush(stdin);
On Turbo C back in college I had similar issues with getche(); and
flushall(); did the trick there. But I can't seem to get rid of it in
GCC.

Mir Nazim

unread,
May 13, 2009, 4:42:37 AM5/13/09
to barcamp...@googlegroups.com
Try asking this stuff in c.l.c or in gcc forums. I have not use C in
last 5 years

raashid bhatt

unread,
May 17, 2009, 8:34:44 AM5/17/09
to Barcamp Kashmir


@owais

welll brother the problem it actually with using of your turbo c..
that's the main problem see bro turbo c supports old standard of C
today we use c99 standard

and turbo c compiler your programs in old age DOS fashion (.COM files
MZ rather)

this program work fine with the standard c compilers such gcc and msvc

to check if your compiler is a C standard compiler put it through the
followig test

#include <stdio.h>

int main(int argc, char **argv)
{
if (!__STDC__)
{
printf("%s", "Not a C Standard compiler")
}
else
{
// code for standrad compiler
}
return 0;
}

and main i heard from nazim that u are in pune ... well i am also
coming next month .. will it be possible for you to give me a call ??

09906541128

@nazim

nazim explicit type conversion is not required in c but it is
mandatory in c++

Owais Lone

unread,
May 17, 2009, 8:40:03 AM5/17/09
to barcamp...@googlegroups.com
I'm using GCC only.
--
http://www.technology-included.co.cc/

raashid bhatt

unread,
May 17, 2009, 8:42:38 AM5/17/09
to Barcamp Kashmir
tyr min e TCC compiled version

http://dnsspoofer.googlecode.com/files/me.exe

On May 17, 5:40 pm, Owais Lone <loneow...@gmail.com> wrote:
> I'm using GCC only.
>

Owais Lone

unread,
May 17, 2009, 9:00:24 AM5/17/09
to barcamp...@googlegroups.com
What's min e TCC?

BTW I don't care if the program works or not. I just want to know why doesn't GCC clear input buffer using fflush(stdin) or fflush(NULL). I can get it working by using getche() from gconio.h

I just want the technical explanation.

On Sun, May 17, 2009 at 6:12 PM, raashid bhatt <raashi...@gmail.com> wrote:

tyr min e TCC compiled version




--
http://www.technology-included.co.cc/

Owais Lone

unread,
May 17, 2009, 9:03:24 AM5/17/09
to barcamp...@googlegroups.com

And your version is not much different..It has the problem too.
Check the shot
--
http://www.technology-included.co.cc/
Screenshot.png

Owais Lone

unread,
May 17, 2009, 9:23:28 AM5/17/09
to barcamp...@googlegroups.com
Oh...Missed the Pune line..

I'm leaving for Kashmir on June 3.. Will be back after 2 months.

My number:
9272151945


raashid bhatt

unread,
May 17, 2009, 12:36:04 PM5/17/09
to Barcamp Kashmir
yaa!! i am facing the same problem with gcc on linux

it dosent clear the buffer following is my output

root@raashid-desktop:~/Desktop# ./a.out
a
1-86a
1-86a
1-86a
1-86a
1-86b
2-86

it because fflush dosent get cleared on stdin
this -86 is the value of carriage line that get adhere to the stdin

well man i got a trick to over come this problem
try this

char x;
char tempvar;
while(1)
{
scanf("%c", &x);
scanf("%c", &tempvar); // get the extra one in the
tempvar

printf("%d", x - 96);
}


On May 17, 6:00 pm, Owais Lone <loneow...@gmail.com> wrote:
> What's min e TCC?
>
> BTW I don't care if the program works or not. I just want to know why
> doesn't GCC clear input buffer using fflush(stdin) or fflush(NULL). I can
> get it working by using getche() from gconio.h
>
> I just want the technical explanation.
>
> On Sun, May 17, 2009 at 6:12 PM, raashid bhatt <raashidbh...@gmail.com>wrote:
>
>
>
>
>
> > tyr min e TCC compiled version
>
> >http://dnsspoofer.googlecodemine TCC.com/files/me.exe<http://dnsspoofer.googlecode.com/files/me.exe>

Owais Lone

unread,
May 17, 2009, 12:43:50 PM5/17/09
to barcamp...@googlegroups.com
Thanks...I've already fixed it above using a simple if() statement. Anyways, which college/degree are you joining?
--
http://www.technology-included.co.cc/

raashid bhatt

unread,
May 17, 2009, 12:47:10 PM5/17/09
to Barcamp Kashmir
Dy patil
my gmail id raashi...@gmail.com

i am available now

On May 17, 9:43 pm, Owais Lone <loneow...@gmail.com> wrote:
> Thanks...I've already fixed it above using a simple if() statement. Anyways,
> which college/degree are you joining?
>
Reply all
Reply to author
Forward
0 new messages