atoi() Function in C

Learn via video course
FREE
View all courses
C++ Course: Learn the Essentials
C++ Course: Learn the Essentials
by Prateek Narang
1000
5
Start Learning
C++ Course: Learn the Essentials
C++ Course: Learn the Essentials
by Prateek Narang
1000
5
Start Learning
Topics Covered

Overview

The atoi function in C converts a string of characters to an integer value. The input is a character string and the function stops reading the input when it first encounters a character that is not a number. The atoi function in C can not convert string in decimal and exponent notation. The function skips all the white spaces encountered at the start of the string. The function is specified in stdlib.h header file.

Transform Your Career

Choose from our industry-leading programs designed for career success

NSDC Certified

Modern Software and AI Engineering Program

Master full-stack development with AI integration

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program
NSDC Certified

Modern Data Science and ML with specialisation in AI

Advanced data science techniques with AI specialization

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program
NSDC Certified

Advanced AIML with Specialisation in Agentic AI

Deep dive into AIML with focus on Agentic systems

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program
NSDC Certified

DevOps, Cloud & AI Platform Engineering

Build and manage AI-powered cloud infrastructure

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program
NSDC Certified

AI Engineering Advanced Certification by IIT-Roorkee

Premier AI engineering certification from IIT-Roorkee

3 MonthsDuration
AI-LedCurriculum
Career SupportSupport
Program highlights
Go to Program

Syntax of Atoi() Function in C

The syntax of the atoi() function is:

Scaler Placement Report and Statistics

₹23L
AVG CTC
SCALER PLACEMENT PROOF

Scaler learners achieved 2.5x salary growth with average post-Scaler CTC reaching ₹23L.

11,000+placements
650+companies
Verified data

Parameters of Atoi() Function in C

The atoi function in C accepts only one parameter as an argument which is a pointer to a string. The string is passed as const, which ensures the function doesn't change the value of the string and only returns the integer value from the string.

Return Value of Atoi() Function in C

For valid inputs, the atoi function in C returns an integer value equal to the passed string. If the passed string has non-valid input, the function returns 0. Because the atoi() function works incrementally (converts string to number by reading characters one by one), the function breaks if it encounters a non-ASCII value and skips all whitespace at the starting of the string. atoi() function can convert a string to a valid integer if the string contains the following parameter:

  1. Strings made of ASCII digits i.e. '0', '1', '2', '3', '4', '5', '6', '7', '8' and '9'.
  2. Strings beginning with characters '+' or '-' and made entirely of ASCII digits.

If the function call breaks in between, it returns either of two possible values.

  1. If the function is called at the start and no valid input is encountered so far, the function returns the value 0.
  2. Otherwise, the atoi() function returns the number encountered before the first non-digit character.

Turn Learning into Career Growth

1200+Hiring Partners
89%Placement Rate
11,000+Placements
147%Avg Salary Increment
2.5XCareer Growth
₹23 LPAAvg Post-Scaler Salary
1200+Hiring Partners
89%Placement Rate
11,000+Placements
147%Avg Salary Increment
2.5XCareer Growth
₹23 LPAAvg Post-Scaler Salary

Required Header

C language requires the following header file for atoi function:

Example

Let's see an example of converting a number in a string to an integer value using the atoi() function in C.

Output

Here, we use the atoi function in C to convert the string value stored in the character array str to an integer. The atoi function returns an integer value that is assigned to the variable value.

More Examples

Now that we have seen how the atoi function works in C. Let us see different cases when an invalid or partially valid string is passed as an argument to the function.

Output

In the above example, we can make the following observations from the output of the program:

  • If an invalid string is passed to the atoi function, it returns the integer value 0.
  • If the atoi function in C encounters a non-digit character, it returns an integer value it can transform before the first non-digit character.
  • If the string starts with the character '+' or '-' followed by ASCII digits, it is a valid input.

Scaler Placement Report and Statistics

₹23L
AVG CTC
SCALER PLACEMENT PROOF

Scaler learners achieved 2.5x salary growth with average post-Scaler CTC reaching ₹23L.

11,000+placements
650+companies
Verified data

Conclusion

  • The atoi function in C converts a string of characters to an integer value.
  • To use atoi function in C we have to include stdlib.h header file in our program.
  • The function reads the input string character by character and breaks when a non-digit ASCII character is encountered. It also skips the all whitespaces present at the start of the string
  • The atoi() function accepts only one string parameter as input and returns its converted integer value. The input is of constant value that ensures the function does not change the value of the input string.
  • If an invalid string is passed as an input, the function returns 0.
Hiring Partners:
GoogleGoogleAmazonAmazonMicrosoftMicrosoftFlipkartFlipkartAdobeAdobe1200+ more