forked from portfoliocourses/c-example-code
-
Notifications
You must be signed in to change notification settings - Fork 0
/
convert_string.c
34 lines (30 loc) · 1.05 KB
/
convert_string.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
/*******************************************************************************
*
* Program: Convert String To Double, Int, Long Int
*
* Description: Example of how to convert strings to doubles, ints and long ints
* in C using the atof(), atoi() and atol() functions available in stdlib.h.
*
* YouTube Lesson: https://www.youtube.com/watch?v=UQOlTdArCRo
*
* Author: Kevin Browne @ https://portfoliocourses.com
*
*******************************************************************************/
#include <stdio.h>
#include <stdlib.h>
int main(void)
{
// setup test strings
char *double_test = "98.45";
char *int_test = "94";
char *long_int_test = "1231231213213234";
// use the functions to convert each string to their respective types
double double_result = atof(double_test);
int int_result = atoi(int_test);
long int long_int_result = atol(long_int_test);
// output the converted values to check for correctness
printf("double: %f\n", double_result);
printf("int: %d\n", int_result);
printf("long int: %ld\n", long_int_result);
return 0;
}