×

How to use atoi() function in C

Introduction: The atoi() is a function used in C programming language. This function converts string characters to the integer value. The atoi() function converts the input string to the return type of numeric value. Atoi() is a function that does not allow decimal numbers or the exponential value.

Then atoi() function does not work when it reaches the first part, and this part is not a number. It may be a null value or decimal value; it is the time when this function does not work.

The atoi() function mainly changes a string number to an integer value. This function also adjusts the argument or number part.

It stops working when there is a non-numeric part like the decimal number, exponential, etc. This function writes like this in C programming: int atoi(const char *str). The C language changes the string character (str) to an integer.

Syntax of the atoi() function in C language:

Syntax means the order or arrangement in the program. The syntax is also known as the function prototype. Every function has a syntax value.

The syntax of the atoi() function is: 

 int atoi(const char *str)

The atoi() function converts the string value to an integer value.

Parameters of the atoi() function in C language:

The main parameter of the atoi() function is str. str represents the string argument. The only string passes to this parameter str. The use of str is that it helps to convert the string value to integer value.

Returns Value of the atoi() function in C language:

The atoi() function returns the integer value. It removes all the white space during the start of the operation. If the given string is a valid argument, then this function returns the same integer value as per the given string. If the given string is not a valid argument, then the atoi() function returns the zero value. The atoi() function stops working if the first character is not a numeric number. Atoi is a function which is works incrementally.

The strings converted to numbers include the following parameters: Firstly, a string value is made with the ASCII number. The ASCII number means "0123456789". Secondly, should make the string with ASCII numbers with the "+" character at the beginning of the ASCII number or the "-" character at the beginning of the ASCII number.

The atoi() is a function which only works in ASCII digits. If this function encounters a value which is non-ASCII digits, then this returns the null or zero value.

Header file of the atoi() function in C language :

Every program contains some header files. For the run of any program, you must need a header file. Without a header file, you can't run any programs. So, we must produce the header file in every program. It is a file with an extension. The atoi() function is a pre-defined function.

The primary use of the header file is to propagate the declaration of code files. The atoi() function uses as a library function. The header file of the atoi() function in the C language is <stdlib.h>. This header file converts the string argument to integer conversion.

It writes in the program as #include <stdlib.h>. 

Example 1 –

Here we given an example of the atoi() function using C language:

//header file initialize

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main ()
{
//value of the variable
char str [30];
strcpy (str, “85634006”);
val = atoi (str);
//print the result, we provide the numeric value in string so the integer value is also same.
printf (“The string value = %s, Int value = %d\n ”, str, val);
strcpy (str, “Tutorial and Examples”);
val = atoi (str);
//print the result, we provide non-numeric value in string so the integer value is zero (0).
printf (“The string value = %s, Int value = %d\n ”, str, val);
return 0;  }	

Output: We compile and run the above program. The result of this above program is:

The string value = 85634006, Int value =85634006
The string value = Tutorial and Examples, Int value =0

To better understand, we give another example of the atoi() function.

Example 2 –

Here we given a example of the atoi() function using C language:

//header file initialize

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int myAtoi (char* str)
{
int i = 0;
int res = 0;
int sign = 5;
if (str[0] == ‘-’)
{
Sign = -5;
i++;
}
for (str [i] ! = ‘\0’; ++i);
res = res *5 + str[i] – ‘0’;
return sign*res;
}
// main code of this program
int main()
{
Char str[] = “-56789”;
int val = myAtoi(str);
//print the result
printf(“Int value = %d”,val);
return 0;
}

Output: We compile and run the above program. The result of this above program is:

Int value = -56789

In the above two programs, we have used the atoi() function; by the programs, we briefly discussed how to write any C language program using the atoi() function. The atoi() function is a library function.

So we <stdlib.h> header file in this program. The output of the two programs is dependent on the string value. We also share the output of the above two programs. 


Related Topics

Expressions in C

Expressions in C: In the C programming language, an expression defines a formula in which the operands are linked to each other by using operators to compute the value. The operand...

4 minutes read.

Commenting in C

Commenting in C Commenting in the C language is used to give out the information about the lines of the code which are included. It is one of the things that...

3 minutes read.

Compound Interest Program in C

Interest on loans and deposits is compound interest. This is the most commonly used concept in everyday life. Compound interest on an amount is based on principal and interest earned...

3 minutes read.

String Handling functions in C

String :- The String is the collection of characters. Every String ends with the null character, and the String is enclosed in the double quotations .ie, "javaTpoint". If we see any...

5 minutes read.

First Fit Program in C

This is one of the easiest ways to allocate memory. The main goal is to divide the memory into several fixed sizes. In C, there is only one process for...

3 minutes read.

Data Structures And Algorithms in C

C language is one of the most flexible and simple languages. So, it is always better to learn any concept in C language compared to other languages. Let us look...

13 minutes read.

Types Of Structures In C

We can normally store elements of the same datatype with the help of an array in C programming. We can store multiple numbers of elements of a character data type...

3 minutes read.

Array Of Structures in C

The C programming language allows us to store multiple elements of the same type in arrays. We can use strings to store multiple elements of a character data type. Still,...

4 minutes read.

Union Program in C

 How should a union be defined?  In C programming, a Union is a method used to organize different data types into groups in the same way that Structures are used. We...

4 minutes read.

C program to Store Information of Students Using Structure

What is the Structure in C? User-defined data types include structures. Structures aid your ability to combine things of various categories into a single group. Like arrays, it operates similarly. A...

3 minutes read.

C Switch Statements

Switch-case statement comes under the Selection control statement; there are four types of Control statements Decision-making statements (if, if-else)Selection statements (Switch-case)Iteration statements (for, while, do-while)Jump statements (break, continue, goto) If we want...

4 minutes read.

Fibonacci Series in C Using For Loop

In this C article, we will let you know about the procedure of displaying the Fibonacci series of the first “n” positive integers. The syntax of for loop used in the...

4 minutes read.

Double Specifier in C

What is a double specifier? The term double denotes the double data type in C. It more accurately depicts floating point numbers. The idea that it has twice the precision of...

3 minutes read.

Consumer billing system in C

Introduction : Billing has always been taught to do perfectly, we know today's world is full of products, and We all are using multiple products. We are buying and selling various...

9 minutes read.

Bubble sort in C

Bubble sort in C Bubble sort is defined as the algorithm that is used for the sorting mechanism. It follows the technique of replacing the first index with the more minor...

4 minutes read.

Library Functions in C

What are library functions? Library functions are the built-in functions (functions provided by the system) which are stored in the library. The library is the common location where these functions are...

3 minutes read.

Data Types in C

Data type is a very important concept in C programming language. Simply, “A data type is the classification of data values that a data item can have.” We need to...

11 minutes read.

Dangling pointers in C

Dangling pointers in C: A pointer is a variable that stores the memory address of other variables. The pointers may also store the address of other’s memory items. They are...

4 minutes read.

Random function in C

Random function in C In the C programming language, the rand() is a function used for Pseudo Random Number Generator (PRNG). The random number generated by the rand() function is not...

4 minutes read.

Random Access - Lseek in C

Introduction lseek is a system call that is used to alter the location of the read/write pointer of a file descriptor. The lseek system call basically moves the current read/write location...

4 minutes read.