include <stdio.h>
int main() {
int var = 0120;
printf(“%d”,var);
return 0;
}
In this above code I am getting 80 as output I am not getting that can anyone help me with that
include <stdio.h>
int main() {
int var = 0120;
printf(“%d”,var);
return 0;
}
In this above code I am getting 80 as output I am not getting that can anyone help me with that
In C, when you prefix a number with a leading zero (0), it is treated as an octal (base-8) number rather than a decimal (base-10) number.
So, when you write:
int var = 0120;
You are actually initializing var with the octal value 120, which is equivalent to the decimal value 80. In octal notation, the digit 8 is not valid, so it stops at 7, and 0120 in octal is equal to 80 in decimal.
That’s why when you print var using
printf("%d", var);
it prints 80.
Thanks
when we put 0 in starting in C program it treats it as Octal number. to print correct output use %o instead of %d.
include <stdio.h>
int main() {
int var = 0120;
printf(“%o”,var);
return 0;
}
Well, there is a simple syntax error with your code.
Try this code:
#include <stdio.h>
int main() {
int var = 120;
printf("%d", var);
return 0;
}
Run the above code, and you will get what you are looking for.
Thanks