Get Even More Visitors To Your Blog, Upgrade To A Business Listing >>

C++ atoi

Introduction to C++ atoi

In C++, Atoi is defined as the default Function it will be used to declare and return the value as the integer data type and passing the string as the arguments, so it converts the string into integer arguments; also, the function will accept only one argument at the time so we will pass the data type with continuously on the function like string to integer if the string type as the valid type of user inputs the function also returns the same valid number of integer values for the passed string values else if no value is passed on the data type the return value also zero.

Syntax

In C++ each object, variables, keywords, and functions have their own syntax and attributes for declaring in the programming codes. Based on the requirement, we will utilize the special keywords and the functions used from the programming library. For example, the atoi is one of the built-in functions, and it will be used for converting the string data type values into the integer data type by using the function atoi().

#include
#include
data type main()
{
data type variable name;
data type var name;
variable name=atoi(var name);
---some c++ code logics---
}

The above codes are the basic syntax for the atoi() function, which it will use for the two different types, also called with another function if it’s required.

How the atoi() method works in C++?

The C++ has n number of methods that will provide some abstractions from the actual parent method and new child methods, which is already being used for the programmers. It also used to allow it with more focus on the coding concepts. It makes it easier to write the programming codes and clean them using some methods like destroy() or any other default methods belonging to the garbage collections, and it’s the main area for destroying the unwanted codes and clean up the memory space areas. It depends upon the data types, and the object creation sizes must be calculated, and it allocates the memory space for both the big storage data type variables as well as small amount storage variables. Normally the C++ declarations the directives are used with the different types of memory spaces like heap and stack memory.

The objects are stored in heap memory, and the methods are stored in stack memory; we can also create default memory functions like calloc(), malloc(), etc. But here, the atoi() function will convert the string, char data type values into the integer data type, the function which executes and first omit the unwanted whitespaces with characters until it needs to find the non-white character characters. It can also search operators like +, -, * followed with 0 to 9 digits as much as possible for interpreting the numerical set of values. The string also contains some more additional characters; after that, they must be ignored for the integer numbers, which are already ignored and have not affected the functional behavior of the C++.

If suppose the function atoi() has passed arguments, it must be the non-whitespace characters it will be a valid integer it will return the valid integers else the sequence is not occurred at that time either the string is empty, or it must contain only for the whitespace characters. So there are no conversions that occurred or processed in the function, and also there is no return value is found. Also, the converted integer value is out of range means the represented value is in int data type, and it causes some undefined behavior; it throws the exceptions.

Examples of C++ atoi

Given below are the examples of C++ atoi:

Example #1

Code:

#include
#include
struct demo {
virtual void one(int) { std::cout void two(char)        { std::cout void three(int)         { std::cout protected:
int a;
typedef int val;
};
struct demo1 : demo {
using demo::a;
using demo::val;
using demo::one;
void one(int) { std::cout using demo::two;
void two(int) { std::cout using demo::three;
void three(int) { std::cout };
int main()
{
demo1 i;
demo& d = i;
i.a = 3;
i.one(3);
i.one(3);
i.two(2);
i.two('k');
i.three(3);
i.three(3);
int u,v;
char ch[] = "siva";
u = atoi(ch);
std::cout char ch1[] = "raman";
v = atoi(ch1);
std::cout return (0);
}

Output:

Example #2

Code:

#include
#include
#include
using namespace std;
bool demo(char a)
{
return (a >= '0' && a ? true
: false;
}
int demo1(char* s)
{
if (*s == '\0')
return 0;
int b = 0;
int s1 = 1;
int p = 0;
if (s[0] == '-') {
s1 = -1;
p++;
}
for (; s[p] != '\0'; ++p) {
if (demo(s[p]) == false)
return 0;
b = b * 10 + s[p] - '0';
}
return s1 * b;
}
int main()
{
char c[] = "-987654";
int output = demo1(c);
printf("%d ", output);
return 0;
}

Output:

Example #3

Code:

#include
#include
int main()
{
const char *s = "5637";
const char *s1 = "1.657";
const char *s2 = "53647527 Welcome To My Domain";
const char *s3 = "Have a Nice Day 67";
const char *s4 = "Hihg0.67";
int a = std::atoi(s);
int b = std::atoi(s1);
int c = std::atoi(s2);
int d = std::atoi(s3);
int e = std::atoi(s4);
std::cout std::cout std::cout std::cout std::cout }

Output:

Conclusion

C++ has a lot of predefined and user-defined functions; it includes a lot of characteristics and the special keywords in the libraries. The functions that will be implementing the logic depends upon the project requirement. Here we used these functions for reducing the lines of codes and the memory areas; also, the function will be called due to the data type value conversion in the main method.

Recommended Articles

This is a guide to C++ atoi. Here we discuss How the atoi() method works in C++ and Examples along with codes and outputs. You may also have a look at the following articles to learn more –

  1. C++ find_if()
  2. C++ Stream
  3. C++ Multiset
  4. C++ shuffle()

The post C++ atoi appeared first on EDUCBA.



This post first appeared on Free Online CFA Calculator Training Course | EduCB, please read the originial post: here

Share the post

C++ atoi

×

Subscribe to Free Online Cfa Calculator Training Course | Educb

Get updates delivered right to your inbox!

Thank you for your subscription

×