This question already has answers here:
Why is initializing an integer in C++ to 010 different from initializing it to 10?
(4 answers)
Closed 1 year ago.
#include<bits/stdc++.h>
using namespace std;
int a[500];
int main()
{
a[21]=10;
if(a[21]==a[021])puts("Yes");
else puts("No");
return 0;
}
g++ -std=c++11
The output is No, can anyone tell me why?
In C++ a leading 0 on an integer literal means that value is in octal (similar to the way that 0x21 means that value is in hexadecimal).
Each of these values will be different. Here's a quick online demo:
http://cpp.sh/3cws4n
Note: the default output format for cout is decimal so the values you see are in decimal.
Related
This question already has an answer here:
Integer division always zero [duplicate]
(1 answer)
Closed 3 years ago.
As the title says, this code is meant to calculate the probability of 2 people having the same birthday in a group of 5 but it just outputs 1, I'm fairly new to C++ so any help would be appreciated.
#include <iostream>
using namespace std;
int main(){
float p;
p=1-(364/365)*(363/365)*(362/365)*(361/365);
cout<<p;
}
Put a .0 on each number, that way is treated as a double instead of an integer. Integer division (364/365) equals 0
p=1.0-(364.0/365.0)*(363.0/365.0)*(362.0/365.0)*(361.0/365.0);
This is because after calculation 364/365 the calculates answer is an integer which is 0.
To make it work change it like this.
p=1-(364/365.0)*(363/365.0)*(362/365.0)*(361/365.0);
You need to cast the integers to floats as / rounds to the largest integer below the result when both types are int:
p=1-(float(364)/float(365))*(float(363)/float(365))*(float(362)/float(365))*(float(361)/float(365));
This question already has answers here:
C Unsigned int providing a negative value?
(3 answers)
printf format specifiers for uint32_t and size_t
(4 answers)
Closed 5 years ago.
1.The code defines the variables as unsigned integer however the output is shown negative.
#include<stdio.h>
#include<limits.h>
#include<stdint.h>
int main(){
uint32_t a= 25,b=50;
a = a-b;
printf("\n%d\n",a);
return 0;
}
The correct format specifier would be (The macro PRIu32 is defined in inttypes.h header ).
printf("%" PRIu32 "\n",a);
%d expects the arguments passed to it would be address of an signed int - that's why the value you passed is considered as signed int. After you have used the correct format specifier the output would be UINT32_MAX+1-25.
Using the wrong format specifier is undefined behavior.
This question already has answers here:
Why does division result in zero instead of a decimal?
(5 answers)
Closed 5 years ago.
I'm still a beginner in C++ so I seek some help with the basics.
Here, in the following code, I'm using type-casting to find value of 122/65 but I'm getting only the integer part even with double data type.
#include <iostream>
using namespace std;
int main()
{
double a=(double)('z'/'A');
cout<<a;
return 0;
}
Can someone provide me a good reason for this??
Thank you.
You make an integer division and then you typecast the result to double. Basically you have:
(double) (122/65) = (double) (1) = 1.0
^ truncated -> integer division
If you want a floating point division you can do it this way:
double a = (double)'z' / (double)'A';
// a = 122.0 / 65.0
This question already has answers here:
Why pow(10,5) = 9,999 in C++
(8 answers)
Closed 8 years ago.
#include <iostream.h>
#include <math.h>
int main()
{
int j=2;
int output;
output=pow(10,j);
cout<<output;
return 0;
}
I wrote above code to gcc 12 compiler and got the output 99 instead 100. I don't get the valid reason while searching on various sites. Is there any compiler problem?
Because of integer truncation. pow() returns a floating point value, and due to floating point arithmetic, it is probably ~ 99.999...; however, due to integer truncation, even 99.999... gets truncated down to 99.
This question already has answers here:
How disastrous is integer overflow in C++?
(3 answers)
Closed 8 years ago.
when i try to add two long numbers it gives me minus result :
#include<iostream>
using namespace std;
int main ()
{
int a=1825228665;
int b=1452556585;
cout<<a+b;
return 0;
}
This gives me:
-1017182046
It's overflowing of the type. When you add two big number that the result can't be stored in chosen type it get overfloved. In most cases it will wraped the number, but it's not defined in the standard. So in some compilers the result is undefined.
For int and other numeric types when program can't store this big number in it we can see a overflow of it.
Lets say that int can store number from -10 to 10, when you do this:
int a = 10;
int b = a+1;
You will get -10 in b or some random value (it can be anything because the result is undefined)
That's because the results overflows. Since the first bit in numeric signed data types is used for the sign representation. The specific representation is called Two's complement (Wikipedia article here). Practically a 1 in this bit maps to a - while a 0 to +. The solution to this problem is using a larger data type like long. Larger it means that the memory used to store it is bigger so the range of values increases.