This question already has answers here:
Using the equality operator == to compare two strings for equality in C [duplicate]
(9 answers)
Closed 4 years ago.
Whenever I run this in the compiler I get 1 returned to me. However, I've been told that this shouldn't return 1 and that my compiler is wrong and can't be trusted.
Is my friend right in that the compiler's I use are giving me the incorrect answer, or is this supposed to return 1?
#include <iostream>
#include <string>
int main()
{
bool lol = "abc" < "abcd";
std::cout << lol;
return 0;
}
The code has Undefined Behaviour, because it's using an oredering comparison operator on two pointers (the address of the string literal "abc" and that of the string literal "abcd") which are not part of the same array. It can therefore do absolutely anything.
Related
This question already has answers here:
g++ "calling" a function without parenthesis (not f() but f; ). Why does it always return 1?
(2 answers)
Closed 2 years ago.
#include <iostream>
int returnFive()
{
return 5;
}
int main()
{
std::cout << returnFive << '\n';
return 0;
}
Since this compiles without error, how does the system determine what value is actually sent and printed to console?
Imagine if the code written is something like
if(returnFive)
returnFive();
Here the expectation is that the compiler checks if the function pointer for returnFive is nullptr or not.
The compiler here is evaluating the function pointer as a boolean expression of whether it is NULL or not and printing the output.
https://godbolt.org/z/Psdc69. You can check that the cout is being passed a (bool).
This question already has answers here:
Equality-test std::string against char*, is operator==() always safe?
(3 answers)
Closed 2 years ago.
Saw several examples that doesn't define type name for Y side and I used to to think C++ is type-strict. Is it safe to use a code snippet like below one in production?
I also curious to hear why it works..
#include <string>
int main() {
std::string X = "car";
if (X == "nothing") {
return 1
}
else if (X == "car") {
return 0
}
}
Thanks to operator== overload, std::string can compare to char* as well as to other std::string.
This is perfectly OK.
This question already has answers here:
Size of character ('a') in C/C++
(4 answers)
Closed 2 years ago.
When I run the program below in C, I get the output result to be 4.
#include <stdio.h>
//using namespace std;
int main()
{
printf("%d", sizeof('a'));
return 0;
}
But when I run the code below in C++, I get the output result to be 1.
#include <iostream>
using namespace std;
int main()
{
printf("%d", sizeof('a'));
return 0;
}
Could you please explain why do I get different output for the same code as if 'a' is the way we define characters in both the languages ?
In C, a character representation (like 'a') has type int. So, sizeof operator returns the size of an integer.
In C++, it's of a character type.
This question already has answers here:
"" + something in C++
(3 answers)
Closed 5 years ago.
I am having trouble , in understanding this program please help:
#include <iostream>
using namespace std;
int main(){
const char* s = 5+"hellow world";
cout<<s;
return 0;
}
It is correct and gives following output
In third line of your code, an anonymous character array is created by the compiler. When you add 5 to the c-string, it performs pointer arithmetic and moves the pointer 5 ahead to the string. Hence, it skips the 5 character from the c-string and only stores other characters from the array into the s.
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Why is address of char data not displayed?
I was experimenting with ampersand operator and got stuck at this program :
#include<iostream>
using namespace std;
int main() {
char i='a';
cout<<&i;
return 1;
}
I was expecting the address of variable i as the output but instead the output came as the value of variable i itself.
Can anybody explain what just happened? Thanx in advance.
That's because cout::operator<< has an overload for const char*. You'll need an explicit cast to print the address:
cout<<static_cast<void*>(&i);
This will call the overload with void* as parameter, which is the one used to print addresses.
Also note that your code runs into undefined behavior. You only have a single char there, and the overload expects a null-terminated C-string.