What are String Conversion Functions?
In C programming, string conversion functions are used to convert strings to numeric types (like integers and floats) and vice versa. These functions are part of the standard C library and are declared in headers such as <stdlib.h> and <stdio.h>.​
Why Use String Conversion Functions?
Sometimes, data comes in the form of a string (like “456”), but you need to work with it as a number (like 456). You can’t do arithmetic with strings directly, so string conversion functions are needed to convert them into usable numeric types.
For example:
char str[] = "456"; int num = atoi(str); // Converts the string "456" to the integer 456
Commonly Used String Conversion Functions
Function | Purpose | Syntax |
---|---|---|
atoi() | Converts string to int | int atoi(const char *str); |
atol() | Converts string to long int | long int atol(const char *str); |
atof() | Converts string to double | double atof(const char *str); |
strtol() | Converts string to long int (base specified) | long int strtol(const char *str, char **endptr, int base); |
strtoul() | Converts string to unsigned long | unsigned long strtoul(const char *str, char **endptr, int base); |
strtod() | Converts string to double with more control | double strtod(const char *str, char **endptr); |
atoi() – ASCII to Integer
Converts a string to an int. It stops parsing when a non-digit character is encountered.
Syntax:
int atoi(const char *str);
Example:
#include <stdio.h> #include <stdlib.h> int main() { char quiz[] = "1234"; int score = atoi(quiz); printf("Quiz Score: %d\n", score); return 0; }
Output:
Quiz Score: 1234
atol() – ASCII to Long
Converts a string to a long int.
Syntax:
long int atol(const char *str);
Example:
#include <stdio.h> #include <stdlib.h> int main() { char cert[] = "987654321"; long int certID = atol(cert); printf("Certification ID: %ld\n", certID); return 0; }
Output:
Certification ID: 987654321
atof() – ASCII to Float (Double)
Converts a string to a double.
Syntax:
double atof(const char *str);
Example:
#include <stdio.h> #include <stdlib.h> int main() { char test[] = "99.75"; double marks = atof(test); printf("Test Marks: %.2f\n", marks); return 0; }
Output:
Test Marks: 99.75
strtol() – String to Long (Base-Specific)
Converts a string to a long int, allowing base specification and error checking.
Syntax:
long int strtol(const char *str, char **endptr, int base);
Example:
#include <stdio.h> #include <stdlib.h> int main() { char mcqs[] = "1010abc"; char *end; long result = strtol(mcqs, &end, 2); // base 2 printf("Binary to decimal: %ld\n", result); printf("Stopped at: %s\n", end); return 0; }
Output:
Binary to decimal: 10 Stopped at: abc
strtoul() – String to Unsigned Long
Converts a string to an unsigned long int with base and error checking.
Syntax:
unsigned long strtoul(const char *str, char **endptr, int base);
Example:
#include <stdio.h> #include <stdlib.h> int main() { char data[] = "200"; char *end; unsigned long val = strtoul(data, &end, 10); printf("Unsigned Value: %lu\n", val); return 0; }
Output:
Unsigned Value: 200
strtod() – String to Double
Converts a string to double with error tracking.
Syntax:
double strtod(const char *str, char **endptr);
Example:
#include <stdio.h> #include <stdlib.h> int main() { char cert[] = "123.45sanfoundry"; char *end; double val = strtod(cert, &end); printf("Parsed double: %.2f\n", val); printf("Stopped at: %s\n", end); return 0; }
Output:
Parsed double: 123.45 Stopped at: sanfoundry
Practical Example of String Conversion Functions
#include <stdio.h> #include <stdlib.h> int main() { // Mock data from a certification report (as strings) char totalMarksStr[] = "85"; char passPercentageStr[] = "75.5"; char rollNumberStr[] = "202504ABCD"; char gpaStr[] = "8.75GPA"; // Convert string to int using atoi int totalMarks = atoi(totalMarksStr); printf("Total Marks: %d\n", totalMarks); // Convert string to double using atof double passPercentage = atof(passPercentageStr); printf("Pass Percentage: %.2f\n", passPercentage); // Use strtol for robust parsing with error detection char *rollEnd; long rollNumber = strtol(rollNumberStr, &rollEnd, 10); printf("Parsed Roll Number: %ld\n", rollNumber); printf("Roll suffix: %s\n", rollEnd); // Use strtod to extract GPA char *gpaEnd; double gpa = strtod(gpaStr, &gpaEnd); printf("GPA: %.2f\n", gpa); printf("GPA Suffix: %s\n", gpaEnd); // Logic decision if (totalMarks >= 75 && gpa >= 8.0) { printf("Result: Sanfoundry Certification Passed!\n"); } else { printf("Result: Certification Not Cleared.\n"); } return 0; }
Output:
Total Marks: 85 Pass Percentage: 75.50 Parsed Roll Number: 202504 Roll suffix: ABCD GPA: 8.75 GPA Suffix: GPA Result: Sanfoundry Certification Passed!
This C program shows how to convert strings into numbers using functions like atoi(), atof(), strtol(), and strtod(). It simulates a certification report where data like marks, percentage, roll number, and GPA are stored as strings. Each function extracts the numeric part from these strings. It also separates non-numeric parts using pointers. The program then checks if the student passed based on marks and GPA, and prints the result.
Sanfoundry Global Education & Learning Series – 1000 C Tutorials.
- Apply for Computer Science Internship
- Check Computer Science Books
- Watch Advanced C Programming Videos
- Check C Books
- Practice BCA MCQs