What does c++ constant value 1j mean? [duplicate] - c++

This question already has answers here:
What does the integer suffix J mean?
(2 answers)
Closed 6 years ago.
I am using Apple LLVM version 7.3.0 (clang-703.0.31) to compile some code and I find that 1j could be a constant value.
In the following code snippet, I assign int b with value 1j and it compiles.
But j is not any variable I have defined.
Can anybody explain what is 1j? Or it is a compiler bug?
Thanks a lot.
#include <iostream>
using namespace std;
int main() {
// What is 1j? Why this code can compile??
int b = 1j;
cout << b << endl;
return 0;
}
I have seen another post explaining this is complex number.
What does the integer suffix J mean?
And using compiler option -pedantic would raise a warning for it.

The suffix i and j is used for complex numbers.

Related

Declared int has random starting value [duplicate]

This question already has answers here:
Variable initialization in C++
(11 answers)
Closed 4 years ago.
I'm very new to C++ and have just started out.
I'm doing a simple exercise where I need to declare a variable with type int and add to it.
Essentially the integer has a starting value of 44 and I have no idea why.
The exercise comes out of a book I'm following.
Here is my code:
#include <iostream>
using namespace std;
int main()
{
int sum;
sum = sum + 1;
cout << sum;
return 0;
}
If I run the following code I get the answer of 45, which makes no sense at all to me.
I want to understand why sum has a value of 44, if no value is assigned to it.
I'm using VScode and the g++ compiler.
Thanks!
You have to initialize variable at first.
The correct code would be:
int sum = 0;
sum = sum + 1;
In your case you have undefined behavior.

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.

Declare an array based on a non-constant variable? [duplicate]

This question already has answers here:
In C++ books, array bound must be constant expression, but why the following code works?
(2 answers)
Closed 6 years ago.
void foo (int k)
{
int C[size(k)][size(k)];
C[1][2] = 4;
std::cout << C[1][2];
}
How this code is compiled correctly?
You code compiles with g++ because it supports variable length arrays as an extension; see 6.19 Arrays of Variable Length
It's not a feature of standard C++, so it won't necessarily work with other compilers.
If you want g++ to complain about this sort of thing, give it -pedantic.

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.

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.