×

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

Format Specifier in C

Format Specifier in C Format specifiers can be described as an operator that is used to print the data referred by any object or variable in combination with the printf ()...

3 minutes read.

C Program for Extended Euclidean algorithms

The Euclidean method simply computes the GCD (greatest common divisor) of two numbers, p and q. (Let's assume) The GCD of two integers is the greatest number that divides them both....

3 minutes read.

Classification of Programming language

 Classification of Programming language  The programming language represents a set of instructions compiled along with each other to perform a specific task provided by the CPU (Central Processing Unit). A programming...

3 minutes read.

Structure in C

Why Structure came into the picture? In the actual world, we deal with entities that are collections of objects rather than tiny bits of data like integers, characters, floats, etc. So,...

4 minutes read.

Free() Function in C

Free() function is a built-in function which is define in the stdlib.h header file. If we want to use this function in our program, we must include the stdlib.h header...

4 minutes read.

Types of Array in C

What is an Array? One value can be stored in a variable at once. How many variables will you need if you have 100 values? Well, the answer isn't 100. It...

14 minutes read.

strtok() and strtok_r() functions in C with examples

strtok() and strtok_r() functions in C with examples C offer various strtok() and strtok r() methods for a string to be separated by a certain delimiter. Dividing a string is a...

2 minutes read.

Top Array Keywords in C

Sorting array in C Types of array in C Array of structure in C How do you initialize an array in C Array declaration in C Array of strings in C Multidimensional array in C Reverse array...

1 minute read.

getc() function in C

Getc is one of the file handling technique in C. The Getc() is a C library function gets the next character or new characters  from the specific stream and supports...

4 minutes read.

Variable in C

Variable is an identifier that holds data in memory. It is used to identify input data in a program. The value of the variable can change at the time of...

2 minutes read.

Advantages of Dynamic Memory Allocation in C

Dynamic Memory Allocation Dynamic memory allocation is a process of assigning or allocating memory to a variable during the execution of a program. Dynamic memory allocation provides storage according to the...

3 minutes read.

Quick sort in C

Quick-sort, in the same way, like a merge sort, follows the principle of the divide and conquer algorithm. It then picks an element as pivot and partitions, and the given...

4 minutes read.

What is the main in C?

In the C programming language, "main" is a predefined term or function. It is the primary function in every C program and is in charge of beginning and terminating the...

6 minutes read.

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.

Array Example in C

An array is a collection of similar types of data elements arranged in such a way that any number of values can be assigned to it. It can store values that...

4 minutes read.

Function in C

Function is a group of statements that are used to perform any task. In other words, we can say that a function is a self- contained a block of programs...

3 minutes read.

Infinite loop in C

Definition of infinite loop The infinite loop is a loop that does not get dismissed because of its looping construct. A continuous output or no output are the two possible outcomes...

3 minutes read.

Pure Virtual Function in C

Before knowing about the pure virtual function some of the important points about the virtual function in C++ are given below. In c++ the member function that is defined in the...

4 minutes read.

Associativity of Operators in C

What is an Associativity operator? Two operators with equal priority are connected by employing their association when they are present in an expression. There are two different types of associativity: left to rightright...

4 minutes read.

C Function Argument and Return Values

Functions in the C programming language are declared to avoid repeatedly writing a performable block of code; it is the same in any programming language. When it comes to the...

3 minutes read.