README.md

December 4, 2022 ยท View on GitHub

string to integer to double

Description

Converts a numeric string to integer and then to double. Demonstrates the use of atoi() and atof().

More Info

Basic but nifty beginner stuff. Hope it is useful to someone. Please vote!!

Submitted On
ByTom Gerhard
LevelBeginner
User Rating5.0 (10 globes from 2 users)
CompatibilityC
CategoryStrings
WorldC / C++
Archive File

API Declarations

#include<stdio.h>
#include<stdlib.h>

Source Code

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
int main()
{
  char word[80];
  puts("\nEnter an integer string: ");
  gets(word);
  /* atoi() : converts its argument, a series of numbers used
   * as a string, in to an integer value and returns it. The
   * statement after the conversion statement shows how unsigned
   * long i has been converted and can now be used.
  */
  unsigned long i = atoi(word);
  printf("\nThe string '%s' converted to integer is %u.\n", word, i);
  printf("The converted string squared is %u.\n", i * i);
  /* atol(): converts its string argument to a double value. The
   * statement following the conversion shows how it can be used.
  */
  double f = atol(word);
  printf("\nThe string '%s' converted to double is %.2f.\n", word, f);
  printf("The converted string divided by 3 is %.3f.\n\n", f / 3);
  /* play and enjoy! */
  system("PAUSE");
  return 0;
}