Link to home
Start Free TrialLog in
Avatar of CBigNoobie
CBigNoobie

asked on

C Noobie Needs Help And Offer Big Points :)

Harlow Every C Seniors and Superiors here.
I got two very basic and question to ask

1st) scanf
 #include <stdio.h>
 #include <conio.h>
 main()
 {
int a;
   char b;

   printf("Enter num: ");
   scanf("%d", &a);
   printf("Enter char: ");
   scanf("%c", &b);

   printf("%d\n",a);
   printf("%c",b);

   getch();
 }

look at this simple coding, after i enter num, i canot enter char.
can some1 tell me the reason why, instead of how cos i know by flushing it, the problem would be solve, and also if i reverse the coding by entering char 1st then entering num, it would also be okay. so what's wrong? pls explain to me generally.
another thing is, if i change the char declartion to string, and i prompt to enter num 1st then enter the string, it works fine too, the reverse, string 1st then num also work fines.

so what wif scanf ?
pls pls pls help very important

 #include <stdio.h>
 #include <conio.h>
 main()
 {
      char a[10];
   char b[10];

   printf("Enter string a: ");
   scanf("%s", a);
      printf("Enter string b: ");
   gets(b);
   printf("%s\n",a);
   printf("%s",b);
   getch();
 }

how come after scanf for string and i use gets , it cant work. but if i gets 1st then scanf, it work. why ? i know flushing can solve this problem but why ? thanks alot

2nd) pointers and array
i guess i will post this later...  THANKS ALL GUYS WHO HELP ME
I REALLY CANT FIND AN ANSWER ON THE NET AND THERE IS NO ONE ARD TO HELP ME
PLS PLS PLS PLS PLS PLS PLS GOD BLESS YOU FOR DOING A KIND DEED TO HELP ME PLS
Avatar of sunnycoder
sunnycoder
Flag of India image

>look at this simple coding, after i enter num, i canot enter char.
the enter key !!! \n

>how come after scanf for string and i use gets , it cant work
gets is not meant to be used .. read the man page ... its man page recommends not to use it
when you realize, that there are two read requests and you cannot see the second one executing and flushing buffers will help you get rid of the problem .... then the obvious answer is that the second read request is being satisfied by some data from the buffer !!!!
u enter num say 10.
These characters and the terminating newline go into the keyboard buffer, scanf() skips over any leading white space and reads characters that form an integer and converts them to the internal form for an integer.It stops reading when the first non-digit is encountered. It stores the integer value, 10, in a and returns the number of items read, i.e. 1. The first integer, 10, is read correctly and printed , followed by a prompt to type in the charecter. but since buffer is not flushed this request is fulfilled already.

program does not wait for the user to type the response. A newline is printed as the next character read, and the program terminates. The reason is that when the user types the integer followed by a RETURN, the digit characters followed by the terminating newline are placed in the keyboard buffer.The function scanf() reads the integer until it reaches the newline character, but leaves the newline in the buffer. This newline character is then read as the next input character into b.
Avatar of jinumjoy
jinumjoy

I want to add to the Question itself.

Is there a problem if ASCII 13(enter, hope i am r8) is also read by the scanf() and omit it while representing the integer/variable. I mean is there some strong reason to leave the "13" in the buffer???? Can anyone give me a scenario where in the \n left behind can be of use later????

The question is if you got to flush the buffer after a scanf() then let shouldn't scanf() be doing it?

-Jinu
what if I wanted to read that \n as a char ? then you would complain that it flushes the \n ;o) ... if you are expecting a \n and do not want it to be read into a variable, you can always specify this in format string

scanf ( "%d\n%c",...);
did it give u explanation u wanted r u want more clarification.
erhmmm i needed more clarifications :/ kinda confuse.

what is whitespace ? what is the man page?

since you say the \n is left in the buffer, does that mean that the integer inputted let say 10, is remove from the buffer once it is read into the variable a leaving \n behind?

then how about the scanf for char 1st, doesn the 2nd scanf for integer takes in the \n ?

i am sorry if i am too stupid about such things, i hope i dun offend or irritate you guys.
i am very sincere in learning from you guys.. pls do help :)
and thanks a million times.
CBignoobie is my brother, just helping him to type the question
i am java noob
he is c noob
haha
>what is whitespace ?
tab, blanks, newline

>what is the man page?
help pages available on a *nix machine ... if you are using one, type man scanf on command prompt

>since you say the \n is left in the buffer, does that mean that the integer inputted let say 10, is remove from the buffer once it
>is read into the variable a leaving \n behind?
yes

>then how about the scanf for char 1st, doesn the 2nd scanf for integer takes in the \n ?
no, the data types are different, it is looking for an int ... in the first case it was looking for a char and could not tell if \n was what user wanted to input as a char

>CBignoobie is my brother, just helping him to type the question
make sure these are not dups, they can land you in trouble
whietspace is blank.
man page is manual page. it is help information for any command.
like for command scanf if u want to know more information/detailed information.
type
#man scanf
Q:since you say the \n is left in the buffer, does that mean that the integer inputted let say 10, is remove from the buffer once it is read into the variable a leaving \n behind?

A: yes.  \n is left behind.
Q: then how about the scanf for char 1st, doesn the 2nd scanf for integer takes in the \n ?
A: this time scanf is looking for char, scanf() skips over any leading white space and reads characters that form an char, It stops reading when the first non-char is encountered. that will be your number. since \n is also a charecter.
Avatar of CBigNoobie

ASKER

haha ya that my brother. thanks for all the help. i am quite clear already

as for scanf for char
let say you enter "a" and you press the enter key \n
the buffer contain a\n ?
and onli the a is put into variable? then how abt the \n?

does that means if the next scanf request is of different datatype in compare to the contents of the buffer, the buffer is clear?

:) u got clear explainatations
>does that means if the next scanf request is of different datatype in compare to the contents of the buffer, the buffer is clear?

never assume such things ... it is bad programming ... always flush buffers ...

data types can be mixed to a certain extent .... you may be having a scanf() for a float while there is an int in the buffer ... you might assume that buffer is clear since there is no float in it but in fact the int will be read into the float ...

so, dont depend on it ... you will end up pulling your hair out ;o)
scanf problem:

this must be
scanf("%s", &a);
this works fine whether you declared the variable 'a' to be a string or a char.
i also had this problem and this made the trick.
Q: then how about the scanf for char 1st, doesn the 2nd scanf for integer takes in the \n ?
A: this time scanf is looking for char, scanf() skips over any leading white space and reads characters that form an char, It stops reading when the first non-char is encountered. that will be your number. since \n is also a charecter.

i thought \n was also consider as an whitespace, then since scanf is looking for char
why do u say it skips over any leading whitespace(isnt \n a whitespace too)?
---------------------------------------------------------------------------------------
and i still dont understand
if i scanf for character 1st, then i scanf for integer, doesnt the \n after the character goes into the integer? since sunnycoder say i cant assume that if the next scanf request is of different datatype in compare to the contents of the buffer, the buffer is clear

sorry if i am slow. i am kinda getting the whole situtation clear, but still abit here and there of uncertainty thanks again for all the trouble though.
you might need an application where a space or a tab or a newline may be valid inputs "press enter to continue" ... how will you read them ? ofcourse using scanf !!! that is why, when you are trying to read in a char and there is a \n in the buffer, the \n will be assigned to that char
yea ha, but then how abt the part
whereby u scanf for char, press enter \n, then u scanf for number,why the \n is not put into the integer variable, becos of the diff datatype?
but then again you say cant assume that the buffer is clear when the next scanf request is of different datatype,

so how do i actually determine when the content in a buffer will be taken by the next scanf request or not?
>so how do i actually determine when the content in a buffer will be taken by the next scanf request or not?
depends on the contents of the buffer ... if the buffer has an int and you read a float, it will be read from the buffer as a float...
It is best to flush buffers before scanf and that is what you should be doing
> whereby u scanf for char, press enter \n, then u scanf for number,why the \n is not put into the integer variable, becos of the diff datatype?

yes, scanf looks for integar and it can not make the request fulfilled with \n.
hmm ok I AM CLEAR!!!! Hurray!!!

just two last doubts i have now ;) sorry if i am long winded,
Q1)when will the scenario happen, when you scanf for an int, then you scanf for a float
how is it possible for that int to be in the buffer? after you scanf for that int?

what i mean is, when you scanf for a int number, let say the user enter 5,
5 is inside the buffer, then it is put into a int variable, how is it possible for a int value to remain in the buffer?

Q2)
errhmm remember if i scanf for a char, then i scanf for the int
the \n after i enter the char value is inside the buffer, then i scanf for the int, then become \n doesn satisfy the int value, so it will allow me to enter an int value, what happens to the \n? is it still inside the buffer?

now i really start to understand it is realli a must to flush, so troublesome!

haha

ASKER CERTIFIED SOLUTION
Avatar of sunnycoder
sunnycoder
Flag of India image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
hurray hurray !!! finallly i got the answer so happi !!!!!

anywhere where i can know when scanf keeps scanning or stop scanning for %i %c  and %s

what I is mean is for %i %s %c, does scanf ignore whitespaces, and when does it stop scanning?
 
just hoping for some kind help since i already awarded marks :)

thanks all pple who reply to this post.
>anywhere where i can know when scanf keeps scanning or stop scanning for %i %c  and %s
None that I know of ... you will learn it as you go along coding