Take the 2-minute tour ×

I was given an assignment to create a procedure that scans a float, called getfloat.

for some reason, I am getting random values. If I enter "1" it prints 49.Why does this happen? And also, when i input values, I can't see them on the screen? when I use scanf for example i see what i hit, on the little black screen. but now the screen is just blank, and when i click enter it shows a bad output:

Example - input: -1. Output: 499.00000 Here is my code:

#include <stdio.h>
#include <conio.h>
#include <math.h>
#include <ctype.h>
void getfloat(float* num);
void main()
    float num=0;
    printf("Enter the float\n");
    printf("\nThe number is %lf\n",num);
void getfloat(float* num)
    float c,sign=1,exponent=10;
    if((!isdigit(c))&&(c!='+')&&(c!='-')) //if it doesnt start with a number a + or a -, its not a valid input
        printf("Not a number\n");
    if(c=='-') //if it starts with a minus, make sign negative one, later multiply our number by sign
        *num=(*num*10)+c; //scan the whole part of the number
    if(c!='.') //if after scanning whole part, c isnt a dot, we finished
    do //if it is a dot, scan fraction part
share|improve this question
add comment

3 Answers

up vote 1 down vote accepted

There are a number of issues.

1) Your posted code does not match your example "input: -1. Output: 499.00000", I get 0 due the lack of a getch() after finding a '-'. See #6.

1) 'c' is a character. When you enter '1', c took on a code for the letter 1, which in your case being ASCII coding, is 49. To convert a digit from its ASCII value to a number value, subtract 48 (the ASCII code for the letter '0', often done as c - '0'



*num = (*num*10) + (c-'0');
*num += (c-'0')/exponent;

2) Although you declare c as a float, recommend you declare it as an int. int is the return type from getch().

3) Function getch() is "used to get a character from console but does not echo to the screen". That is why you do not see them. Consider getchar() instead.

4) [Edit: delete Avoid =-. Thank-you @Daniel Fischer]

5) Your exponential calculation needs rework. Note: your exponent could receive a sign character.

6) When you test if(c=='-'), you do not then fetch another c. You also might want to test for else if(c=='+') and consume that c.

Good luck in your C journey.

share|improve this answer
The =- etc. form of compound assignment operators has been abolished long before the first standard was ratified. sign=-1; has been an unambiguous sign = -1; (only less readable) in all C compilers written in the last (at least) 30 years. "In B and early C, the operator was spelled =+ instead of += ; this mistake, repaired in 1976, ..." Pedantic note: "c takes on the ASCII code for the letter" need not be true, it could also be EBCDIC (or a different encoding, but ASCII-compatible and EBCDIC are the only ones I know to be used). –  Daniel Fischer Jun 12 '13 at 11:16
@Daniel Fischer I so rarely see a non-spaced =- that I, like you, was confident the it was obsolete. My test of the code, appeared though to do the -= function. I'll can review that later. True about "c takes on the ASCII ... need not be true" in general, but OP's "if I enter "1" it prints 49" certainly points to ASCII. BTW, in ASCII or EBCDIC, (c - '0') does covert c to its numeric value. –  chux Jun 12 '13 at 12:37
My debugging of the code was faulty when I printed out sign after the sign=-1; (I had assume it was an int type vs. float. Saw a 0 and then assumed ancient (though now obsolete) C syntax. mea culpa) –  chux Jun 12 '13 at 12:55
That c - '0' works for decimal digits is required by the standard (pity that it doesn't work for hexadecimal digits, would be super convenient if 'A' == '0' + 10 etc.). Yes, it's a pretty safe bet that the OP is working with an ASCII-compatible encoding, it was just a pedantic note ;) –  Daniel Fischer Jun 12 '13 at 13:19
add comment

A small hint: 49 is the ASCII for the character 1. You are using getch(), which gives you the return value char.

share|improve this answer
add comment

49 is the Ascii code for the number 1. So when (0'<=c && c <='9') you need to subtract '0' to get the number itself.

share|improve this answer
add comment

Your Answer


