floating point - Convert char to float in c -


i need convert char float. know can of atof() function. dont want create variable hold float. want converted float go in same variable.

operand = atof(operand)

here operand of type char. tried casting this

(float)operand = atof(operand)

but no use.

here entire code :

#include <stdio.h> #include <stdlib.h>  void main() {     float operand = 0.0f ;     char operator = '0' ;     printf("\nfollowing operators supported : + - * / s e\n") ;     float acc = 0.0f ;     while((operand = getchar()) !=0 && getchar()==' ' && (operator = getchar()) != 'e') {             (float)operand = atof(operand) ;             switch (operator) {                     case '+' : printf("\nadd %f accumulator.\tresult : %f\n", operand , operand + acc);                                acc+= operand ;                     break ;                     case '-' : printf("\nsub %f accumulator.\tresult : %f\n", operand, acc - operand);                                acc-= operand ;                     break ;                     case '*' : printf("\nmultiply accumulator %f.\t result : %f\n", operand, operand * acc);                                acc = acc * operand ;                     break ;                     case '/' : printf("\ndivide accumulator %f.\tresult : %f\n", operand, acc / operand);                                acc = acc / operand ;                     break ;                     case 's' : printf("\nset accumulator %f\n",operand) ;                                acc = operand ;                     break ;                     default : printf("\ninvalid syntax\n") ;             }     } 

}

any welcome.

although it's not same "converting char float", various hints in question think want this:

operand = operand - '0'; 

this converts (usually) ascii value in operand value represents, 0 - 9.

typically, getchar returns character code of character typed. so, example, ascii code digit '0' 48 (and '1' 49 , on). if user types '0' getchar return 48, character code digit 0. now, if subtract '0' (which 48) - 0. works digits 0 through 9 (i.e. '1' - '0' = 1, '2' - '0' = 2 , on).


Comments

Popular posts from this blog

c# - Validate object ID from GET to POST -

node.js - Custom Model Validator SailsJS -

php - Find a regex to take part of Email -