Converting Strings To Numbers - C String

C examples for String:String Function

Introduction

  • atof-Converts a string to a floating-point number
  • atoi-Converts a string to an integer

Demo Code

#include <stdio.h> 
#include <stdlib.h> 
int main()/*  w  ww  .j a v a 2  s .c om*/
{ 
   char *str1 = "123.45"; 
   char *str2 = "12"; 
  
   float x; 
   int y; 
  
   printf("\nString 1 is \"%s\"\n", str1); 
   printf("String 2 is \"%s\"\n", str2); 
  
   x = atof(str1); 
   y = atoi(str2); 
  
   printf("\nString 1 converted to a float is %.2f\n", x); 
   printf("String 2 converted to an integer is %d\n", y); 
}

Result


Related Tutorials