Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why in the code "456"+1, output is "56" [duplicate]

Tags:

c++

#include <iostream>
int main()
{
    std::cout << "25"+1;
    return 0;
}

I am getting "5" as output. when I use "5"+1,output is blank;"456"+1 output is "56". confused what is going behind the scenes.

like image 781
user4291320 Avatar asked Mar 10 '15 07:03

user4291320


People also ask

What will be the output of the C code?

Output is dependent on the compiler. For 32 bit compiler it would be fffffffe and for 16 bit it would be fffe.

WHAT IS &N in C?

*&n is equivalent to n . Thus the value of n is printed out. The value of n is the address of the variable p that is a pointer to int .

Are data types in C and C++ same?

Built-in data types is supported in C. Built-in & user-defined data types is supported in C++. C is a function driven language because C is a procedural programming language. C++ is an object driven language because it is an object oriented programming.


3 Answers

The string literal "25" is really a char array of type const char[3] with values {'2', '5', '\0'} (the two characters you see and a null-terminator.) In C and C++, arrays can easily decay to pointers to their first element. This is what happens in this expression:

"25" + 1

where "25" decays to &"25"[0], or a pointer to the first character. Adding 1 to that gives you a pointer to 5.

On top of that, std::ostream, of which std::cout is an instance, prints a const char* (note that char* would also work) by assuming it is a null-terminated string. So in this case, it prints only 5.

like image 56
juanchopanza Avatar answered Oct 24 '22 03:10

juanchopanza


Behind the scenes, "25" is an array of three characters: two representing '2' and '5', and a terminator with the value of zero to mark the end.

An array is a slightly strange creature, with a tendency to change into a pointer (to the first element) if you do anything with it. That's what's happening here: adding one to an array doesn't make sense, so it turns into a pointer. Adding one to that gives a pointer to the second character.

When given a pointer to a character, << assumes it points to a terminated string, and keeps printing characters until it finds a terminator (or explodes in some way, if there isn't one). So giving it a pointer to the second character of a string will print all the characters from the second onwards, as you observed.

If you're new to C and C++, you should first decide which language to learn first, since they're very different. If you choose C++, you'd be wise to become familiar with its friendly, high-level library (such as std::string for dealing with strings without this kind of weirdness) before plunging into the low-level madness of arrays and pointers.

like image 14
Mike Seymour Avatar answered Oct 24 '22 04:10

Mike Seymour


When you write a "", the compiler understands you are putting a string inside. In this case, you are using the function cout, so it prints on screen this string. You are operating with strings when you use the '+' operator, so you are doing a displacement operation before send it to the cout.

like image 2
SeLKoT Avatar answered Oct 24 '22 03:10

SeLKoT