🎓 Top 15 Udemy Courses (80-90% Discount): My Udemy Courses - Ramesh Fadatare — All my Udemy courses are real-time and project oriented courses.
▶️ Subscribe to My YouTube Channel (178K+ subscribers): Java Guides on YouTube
▶️ For AI, ChatGPT, Web, Tech, and Generative AI, subscribe to another channel: Ramesh Fadatare on YouTube
Introduction
atol() function in C is a standard library function that converts a string to a long integer. It is part of the C standard library (stdlib.h). It is commonly used to convert string representations of long integers into their corresponding long integer values.atol() Function Syntax
The syntax for the atol() function is as follows:
long int atol(const char *str);
Parameters:
str: A C string that contains the representation of a long integer.
Returns:
- The function returns the converted long integer value. If no valid conversion could be performed, it returns 0.
Examples
Converting a Simple String to Long Integer
To demonstrate how to use atol() to convert a string to a long integer, we will write a simple program.
Example
#include <stdio.h>
#include <stdlib.h>
int main() {
const char *str = "1234567890";
long int num;
// Convert string to long integer
num = atol(str);
// Print the converted value
printf("The converted value is: %ld\n", num);
return 0;
}
Output:
The converted value is: 1234567890
Handling Invalid Input
This example shows how atol() behaves with invalid input.
Example
#include <stdio.h>
#include <stdlib.h>
int main() {
const char *str = "abc123";
long int num;
// Convert string to long integer
num = atol(str);
// Print the converted value
printf("The converted value is: %ld\n", num);
return 0;
}
Output:
The converted value is: 0
Real-World Use Case
Converting User Input to Long Integer
In real-world applications, the atol() function can be used to convert user input, provided as a string, into a long integer for further numerical processing.
Example
#include <stdio.h>
#include <stdlib.h>
int main() {
char input[100];
long int value;
// Prompt the user for input
printf("Enter a long integer: ");
fgets(input, sizeof(input), stdin);
// Convert input to long integer
value = atol(input);
// Print the converted value
printf("You entered: %ld\n", value);
return 0;
}
Output (example user input "9876543210"):
Enter a long integer: 9876543210
You entered: 9876543210
Conclusion
The atol() function is used to convert strings to long integer values in C. By understanding and using this function, you can effectively manage and process numerical data stored as strings in your C programs. Always handle invalid input scenarios to ensure robust applications.
My Top and Bestseller Udemy Courses. The sale is going on with a 70 - 80% discount. The discount coupon has been added to each course below:
Build REST APIs with Spring Boot 4, Spring Security 7, and JWT
[NEW] Learn Apache Maven with IntelliJ IDEA and Java 25
ChatGPT + Generative AI + Prompt Engineering for Beginners
Spring 7 and Spring Boot 4 for Beginners (Includes 8 Projects)
Available in Udemy for Business
Building Real-Time REST APIs with Spring Boot - Blog App
Available in Udemy for Business
Building Microservices with Spring Boot and Spring Cloud
Available in Udemy for Business
Java Full-Stack Developer Course with Spring Boot and React JS
Available in Udemy for Business
Build 5 Spring Boot Projects with Java: Line-by-Line Coding
Testing Spring Boot Application with JUnit and Mockito
Available in Udemy for Business
Spring Boot Thymeleaf Real-Time Web Application - Blog App
Available in Udemy for Business
Master Spring Data JPA with Hibernate
Available in Udemy for Business
Spring Boot + Apache Kafka Course - The Practical Guide
Available in Udemy for Business
Comments
Post a Comment
Leave Comment