Back to: C Tutorials For Beginners and Professionals
Built-in Typecast Functions in C Language
In this article, I will discuss Built-in Typecast Functions in C Language with Examples. Please read our previous article discussing Type Casting in C Language. Many built-in typecasting functions are available in C language that perform data type conversion from one type to another. They are as follows:
- atof() Converts a string to float
- atoi() Converts a string to int
- atol() Converts a string to long
- itoa() Converts int to string
- ltoa() Converts long to string
atof() function in C Language
The atof() function in C is part of the C standard library, and it is used to convert a string into a floating-point number (double). The name atof stands for ASCII to float. Here’s a brief overview:
Function Prototype: double atof(const char *str);
- Parameters: The function takes a single argument: str: A pointer to a null-terminated string containing the representation of a floating-point number.
- Return Value: On success, it returns the converted floating-point number as a double. Zero is returned if the string does not contain a valid representation of a floating-point number.
- Header File: To use atof(), you need to include the header file stdlib.h.
Example to Understand atof() function in C Language
#include <stdio.h> #include <stdlib.h> int main() { const char *str = "123.45"; double num = atof(str); printf("The number is %f\n", num); return 0; }
In this example, the string “123.45” is converted to the double value 123.45.
Important Notes
- atof() does not detect errors. If the string cannot be converted to a double, it simply returns zero without any indication of an error.
- It’s often recommended to use strtod() instead of atof(), as strtod() provides error-handling capabilities.
- The behavior of atof() can be affected by the current locale, particularly for number formatting details like the decimal point character.
atoi() function in C Language
The atoi() function in C converts a string into an integer. The name atoi stands for ASCII to an integer. It’s part of the C standard library, included in the header <stdlib.h>. Here’s a basic outline of how atoi() works:
Function Prototype: int atoi(const char *str);
- Parameters: The function takes a single argument, str, which is a pointer to a null-terminated string.
- Return Value: It returns the converted integral number as an int value. If no valid conversion could be performed, it returns zero.
Key Points:
- Non-numeric Characters: The function stops reading the string as soon as a non-numeric character is found. For example, atoi(“123abc”) will return 123.
- Leading Whitespace: It automatically ignores any whitespace characters at the beginning of the string, like spaces or tabs.
- Sign Handling: It also handles negative numbers. A string starting with – will result in a negative integer.
- Error Handling: There’s no standard error-handling mechanism in atoi. If the input cannot be converted to an integer (like in the case of a purely alphabetical string), the function returns zero.
- Overflow and Underflow: The behavior of atoi() is undefined if the converted value is out of the range of an int.
Example To Understand atoi() function in C Language:
#include <stdio.h> #include <stdlib.h> int main() { char str[] = "12345"; int value = atoi(str); printf("The string \"%s\" converted to integer is %d\n", str, value); return 0; }
Alternatives:
- strtol() Function: For better error handling and to avoid undefined behavior in case of overflows, it’s often recommended to use strtol() or similar functions in the standard library.
atol() function in C Language
The atol() function in C is used to convert a string to a long integer. It stands for “ASCII to long”. The function is part of the C standard library, and its prototype is found in the <stdlib.h> header file. Here’s a brief overview of how it works:
Syntax: long int atol(const char *str);
- Parameters: The atol function takes a single argument, str, which is a pointer to a null-terminated string that is to be converted into a long integer.
- Return Value: The function returns the converted integral number as a long int value. If no valid conversion can be performed, it returns zero.
Important Points:
- The function skips all white-space characters at the beginning of the string.
- It then reads consecutive characters that are valid as part of a number and interprets them as a numerical value.
- The conversion stops when the first non-numerical character (or the end of the string) is reached.
- This function does not detect errors such as overflow, so if the converted value goes beyond the range of representable values by a long int, the result is undefined.
Here’s a simple example of its usage in C code:
#include <stdio.h> #include <stdlib.h> int main() { const char *str = "12345"; long int value; value = atol(str); printf("The string '%s' converted to long int is %ld\n", str, value); return 0; }
In this example, the string “12345” is converted to a long integer. The program would output: The string ‘12345’ converted to long int is 12345.
itoa() function in C Language
The itoa() function in C is used to convert an integer to a string. The name it stands for “integer to ASCII”. This function is not part of the ANSI C/C++ standard library, so it may not be available in standard library implementations. However, it is commonly found in many compiler environments.
The prototype of the itoa function is typically as follows:
char* itoa(int value, char* str, int base);
Parameters:
- value: The integer value you want to convert.
- str: The character array (string) where the resulting null-terminated string is stored.
- base: The numeric base for the conversion, which can be 10 for decimals, 2 for binary, 16 for hexadecimals, etc.
Here’s a basic example of using itoa():
#include <stdio.h> #include <stdlib.h> // Some implementations require this header for itoa int main() { int num = 123; char buffer[10]; itoa(num, buffer, 10); // Convert num to string with base 10 printf("The string is %s\n", buffer); return 0; }
In this example, the integer 123 is converted to a string “123” and stored in the buffer.
Remember, since itoa() is not standard, you should check your compiler’s documentation to see if it’s supported or consider using standard functions like sprintf() for portability. Alternatively, you can write your own implementation of it ().
ltoa() function in C Language
The ltoa() function in C is used to convert a long integer value to a string. This function is not part of the standard C library (ANSI C), but it is often found in various C standard library implementations as a non-standard extension. Here’s a basic overview of how ltoa() works:
Function Signature: char *ltoa(long value, char *str, int base);
Parameters:
- value: The long integer to be converted.
- str: The array in which the resulting null-terminated string is stored.
- base: The base for the conversion, which can vary from 2 (binary) to 36.
Return Value: ltoa() returns a pointer to the string str containing the converted value.
Usage: You must provide a large array for str to hold the resulting string. The function does not allocate memory for the string; it’s the caller’s responsibility to ensure that enough space is available.
Error Handling: Since ltoa() is not a standard function, its behavior can vary between different library implementations. Some implementations might not handle errors consistently.
Alternatives in Standard C: In standard C, the sprintf() or snprintf() functions can be used to convert numbers to strings in various bases.
Example of using ltoa():
#include <stdlib.h> #include <stdio.h> int main() { long number = 12345; char buffer[20]; ltoa(number, buffer, 10); // Converts number to string in base 10 printf("The string is: %s\n", buffer); return 0; }
Remember, since ltoa() is not part of the standard C library, it’s often better to use standard functions like sprintf() for portability.
In the next article, I will discuss Control Statements in C with Examples. In this article, I explain Built-in Typecast Functions in C Language with Examples. I hope you enjoy this article on Built-in Typecast Functions in C Language. I would like to have your feedback. Please post your feedback, questions, or comments about this article.