String to number conversion utilities interface.
More...
#include <stdint.h>
Go to the source code of this file.
|
int32_t | str_to_int (const char *s) |
| Convert a string to a 32-bit signed integer.
|
float | str_to_float (const char *s) |
| Convert a string to a floating-point number.
|
String to number conversion utilities interface.
This header declares functions for converting string representations of numbers to their corresponding integer and floating-point values. The implementations handle:
- Leading/trailing whitespace
- Optional sign indicators (+/-)
- Decimal points for floating-point numbers
- Partial number parsing (stops at first invalid character)
- Note
- These functions do not handle scientific notation or hexadecimal formats
-
No overflow/underflow checking is performed
- Author
- Ashutosh Vishwakarma
- Date
- 2025-07-23
◆ str_to_float()
float str_to_float |
( |
const char * | s | ) |
|
Convert a string to a floating-point number.
- Parameters
-
s | Null-terminated string to convert (may contain leading whitespace) |
- Returns
- Converted floating-point value
- Warning
- Limited precision for fractional components
-
Stops parsing at first non-digit/non-dot character
Convert a string to a floating-point number.
Parses the input string to construct a float value:
- Skips leading whitespace
- Processes optional sign
- Handles both integer and fractional parts
- Properly tracks decimal point position
- Stops at first invalid character
- Parameters
-
s | Pointer to null-terminated input string |
- Returns
- Converted floating-point value
- Warning
- Limited precision for fractional components
-
No overflow/underflow protection
Example usage:
float str_to_float(const char *s)
Convert a string to a floating-point number.
Definition conversion.c:96
◆ str_to_int()
int32_t str_to_int |
( |
const char * | s | ) |
|
Convert a string to a 32-bit signed integer.
- Parameters
-
s | Null-terminated string to convert (may contain leading whitespace) |
- Returns
- Converted 32-bit integer value
- Warning
- Potential integer overflow with large numbers
-
Stops parsing at first non-digit character
Convert a string to a 32-bit signed integer.
Parses the input string character by character to construct the integer value:
- Skips any leading whitespace (spaces/tabs)
- Processes optional sign character (+/-)
- Converts subsequent digit characters to integer value
- Stops at first non-digit character
- Parameters
-
s | Pointer to null-terminated input string |
- Returns
- Converted 32-bit integer value
- Warning
- Potential for overflow with large numbers
-
No error reporting for invalid inputs
Example usage:
int32_t str_to_int(const char *s)
Convert a string to a 32-bit signed integer.
Definition conversion.c:44