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.
Related
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.
This question already has answers here:
Why does cout print char arrays differently from other arrays?
(4 answers)
Closed 5 years ago.
I am novice in programming. I have written a program and confused in concepts of pointers.
#include <bits/stdc++.h>
using namespace std;
int main()
{
char c[]="hello";
char *a=c;
cout<<a<<endl;
int arr[]={1,2,3,5};
int *p=arr;
cout<<p<<endl;
return 0;
}
When I print a, it prints hello but when I print p it print the address. Why?
std::ostream has overload for const char* to display C-string.
int* would use the void* one which print the address.
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:
How is a variable at the same address producing 2 different values? [duplicate]
(4 answers)
Closed 8 years ago.
I have a piece of program as shown below:
#include <iostream>
#include <cstdlib>
using namespace std;
int main()
{
const int i = 100;
cout<<"const i::"<<i<<endl;
const_cast<int&>(i) = 200;
cout<<"const i after cast::"<<i<<endl;
return EXIT_SUCCESS;
}
But the value of i is still 100. Aren't const_cast supposed to change the value of i?
Constant data is, by its very definition, constant, and attempting to change constant data leads to undefined behavior.
This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
Size of character ('a') in C/C++
#include <stdio.h>
int main()
{
printf("%d" , sizeof('a'));
return 0;
}
#include <iostream>
using namespace std;
int main(){
cout << sizeof('a');
return 0;
}
For C, it gives 4 as answer whereas for C++ it gives 1 ?
My question is why the languages interpret the character constant differently ?
Actually sizeof('a') == sizeof(int) in C (so you should get 4). I'm not entirely sure about C++ but I believe sizeof('a') == sizeof(char).
The C part is explained in the C FAQ.
Perhaps surprisingly, character
constants in C are of type int, so
sizeof('a') is sizeof(int) (though
this is another area where C++
differs)
You have managed to find one of the few places where the C and C++ standards differ significantly. They are different answers because the specs say they must be.