Sizeof in C and C++ [duplicate] - c++

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.

Related

Why sizeof() method is giving different results? [duplicate]

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.

Why is int a[0] allowed in c++? [duplicate]

This question already has answers here:
What happens if I define a 0-size array in C/C++?
(8 answers)
Closed 6 years ago.
I find that in the declaration of an array, we cannot specify the size of it like this
int a[0];
I know, empty size array illegal in C++, but In my code, empty size array compiler allowed and give the output.
My code is here :
#include <iostream>
using namespace std;
int main()
{
int a[0];
a[0] = 10;
a[1] = 20;
cout<<a[0]<<endl;
cout<<a[1]<<endl;
return 0;
}
Output:
10
20
Online compiler link : http://code.geeksforgeeks.org/TteOmO
So, My question is, Why is int a[0] allowed GCC compiler?
It issues a warning, for example clang outputs:
warning: zero size arrays are an extension [-Wzero-length-array]
this is undefined behaviour:
a[0] = 10;
a[1] = 20;
Zero length arrays are extensions for gcc, why - you can read on it here:
https://gcc.gnu.org/onlinedocs/gcc/Zero-Length.html
They are very useful as the last element of a structure that is really a header for a variable-length object:
This is actually C extension but it looks like it also is used in C++, probably to make it easier to use existing structures from C that uses this extension.
Please Look Into This
What happens if I define a 0-size array in C/C++?
If It's a good compiler it will catch that and give a warning.
but with pedantic option compiler can catch it.

How does C++ process this [duplicate]

This question already has answers here:
Undefined behavior and sequence points
(5 answers)
Closed 7 years ago.
I have here an equation i can't understand how c++ process this. Can someone explain this operation?
code:
#include <stdio.h>
main(){
int a[10] = {0,1,2,3,4,5,6,7,8,9};
int i = 0;
int num = a[i+++a[++i]]+a[++i+i++];
printf("\nnum1: %d i: %d,num,i);
}
why is the answer num = 9 while index i is just equal to 4;
Using ++ twice in the same expression on the same variable is explicitly undefined by all versions of both the C and C++ standards, and so i does not necessarily equal 4. It could be anything at the whim of the compiler writer.
Never do this. Never use ++ and -- twice in the same expression. There is no way to make any statement about what the resultant value will be, and no experience with what it does with one compiler will mean anything with respect to what another compiler does.

Why this C++ program complies and runs in CodeBlocks [duplicate]

This question already has answers here:
Declaring an array of negative length
(3 answers)
Closed 8 years ago.
#include <iostream>
using namespace std;
int main()
{
cout<<"started "<<endl;
int n= -2;
int array[n];
array[0]=100;
array[1]=200;
cout<<array[0]<<endl;
cout<<array[1]<<endl;
cout<<"over"<<endl;
return 0;
}
Why does this compile and run? I expected a compilation error because value of n is negative.
Variable-length arrays are not a C++ feature, so this line will cause a compilation error in a compiler that is ordered to strictly adhere to the C++ specification, regardless of the value of n (unless n is const and can be determined at compile-time):
int array[n];
Likely you are using a compiler that supports variable-length arrays as an extension. Therefore, the rules regarding what is valid depend entirely on the specific compiler and compiler options, and any code that you write using this extension will not be portable to compilers that don't support this non-standard feature.

Array of non-constant size: Why does this even work? [duplicate]

This question already has answers here:
C++: Why does int array[size] work?
(3 answers)
Closed 8 years ago.
#include <iostream>
using namespace std;
int main(){
int n;
cout<<"Enter the size :";
cin>>n;
int array[n]; // I've worked some outputs and it works
return 0;
}
Is this some kind of dynamic allocation?
Why doesn't it even gives an error for 'n' to be a "const"?
Also, writing cout << array[n+5]; doesn't result in an compile time or runtime error.
I'm using Dev-C++.
Apparently one can declare variable length arrays in C99, and it seems GCC accepts then for C++ also.
Variable-length automatic arrays are allowed in ISO C99, and as an
extension GCC accepts them in C90 mode and in C++. These arrays are
declared like any other automatic arrays, but with a length that is
not a constant expression.
You learn something every day .. I hadn't seen that before.