Go4Expert

Go4Expert (http://www.go4expert.com/)
-   C (http://www.go4expert.com/forums/c/)
-   -   The scanf() function (http://www.go4expert.com/forums/the-scanf-function-t5643/)

Player 3Aug2007 17:42

The scanf() function
 
Hi all, i'm new on this site and have some what i think are simple questions. I have had a good look on here and i feel a bit silly asking these basic questions. I am a complete beginner and would like to know a few things to help me on my way. If i need to go to another site which deals with novices then please point me in the right direction. I think this site id for experts only?

Why use the gets() function when theres the scanf() function?

Also when using the gets() function, if you want to have numbers inputed you have to use the atoi() and atof() to convert it to an int number. Is this because the input from the keyboard is text, even the numbers?

Hope this makes sense, like i say i am a complete novice so please excuse my dumb questions. I want to understand all of what i'm learning fully.

Thanks

shabbir 4Aug2007 12:25

Re: The scanf() function
 
Moved from Help n Support + Announcements to C-C++ forum

shabbir 4Aug2007 12:30

Re: The scanf() function
 
Quote:

Originally Posted by Player
I think this site id for experts only?

Not really. At least I learn from here some of the things which I have never thought of. So its all about learning.

Quote:

Originally Posted by Player
Why use the gets() function when theres the scanf() function?

First of all any thing in a programming language can be done in more than one way and scanf provide the general functionality but gets provide some special functionality for string.

Quote:

Originally Posted by Player
Also when using the gets() function, if you want to have numbers inputed you have to use the atoi() and atof() to convert it to an int number. Is this because the input from the keyboard is text, even the numbers?

Because gets takes in strings and so when you need the input to be converted to some other data type you need to make the conversion using the functions.

Player 4Aug2007 14:14

Re: The scanf() function
 
Quote:

Originally Posted by shabbir
Not really. At least I learn from here some of the things which I have never thought of. So its all about learning.

First of all any thing in a programming language can be done in more than one way and scanf provide the general functionality but gets provide some special functionality for string.

Because gets takes in strings and so when you need the input to be converted to some other data type you need to make the conversion using the functions.

Thanks for the reply, i was starting to think i had asked a really dumb question.

I understand about the conversion when using the gets function but if the scanf function automatically converts it why bother with gets? I have also read that the gets function can receive more input than you tell it to making the excess input being dumped into memory which can cause problems, crashing etc. So again, why use gets?

Thanks in advance

P.S. Please tell me if i'm asking really dumb questions :)

shabbir 4Aug2007 16:35

Re: The scanf() function
 
Scanf function does not do it automatically. You need to tell the scanf what you are inputting using the % symbol.

No questions is dumb and if you ask such question it brush ups our knowledge as well and helps in taking some interview with the questions. I am definitely going to add this to my list of questions in the interview.

Player 4Aug2007 17:17

Re: The scanf() function
 
Quote:

Originally Posted by shabbir
Scanf function does not do it automatically. You need to tell the scanf what you are inputting using the % symbol.

No questions is dumb and if you ask such question it brush ups our knowledge as well and helps in taking some interview with the questions. I am definitely going to add this to my list of questions in the interview.

I think i understand now. I have read that input from the keyboard is text. Does punching a number in come as text too. Is this why the % symbol is used for. To convert text to int for example? Could you have a look at the following and tell me if these do the same things please?

char name
gets(name) is the same as

scanf("%s",&name)

and

char input
int result
gets(input)
result=atoi(input) is the same as

scanf("%d",&result)

Thanks for your patience :)

shabbir 5Aug2007 21:12

Re: The scanf() function
 
As far as the user of the functions are considered its the same but the internal implementations may be different or same depending on the compiler.

DaWei 5Aug2007 23:05

Re: The scanf() function
 
First, and very important, do not use gets. If you point gets at a string and the user enters more information than the string can hold, you are looking at a buffer overflow. This can cause complete failure of your program. Use fgets. With fgets you can set a maximum on the number of characters that can be accepted.

Secondly, input devices are entirely implementation dependent. They are hardware. Consider a machine running Windows. Each key produces a code which has no independent meaning, other than to distinguish one key position from another. Windows will take those codes and do things with them, one of which is to interpret them as Virtual Key Codes (this pair of codes represents an ENTER key, that represents the F12 key, etc.). The language library for C (in this instance) will apply further transformations and organizational measures and present the information to you as described in the language documentation.

As you can see, the difference between a 'number' and a 'character' (or a 'function') is strictly one of interpretation. If you mess with your C, you will discover that 1 and '1' and "1" are not the same thing.


All times are GMT +5.5. The time now is 15:00.