#include<stdio.h>
#include<conio.h>
void main()
{
int x=01234;
printf("%d",x);
} the above c code will produce output 668(value of x) why?
#include<stdio.h>
#include<conio.h>
void main()
{
int x=01234;
printf("%d",x);
} the above c code will produce output 668(value of x) why?
Short answer: the token beginning with 0 is an octal literal in C. As explained and and demonstrated with the 080 case, digits in an octal literal can only be 0–7; an 8 produces a compile error because it is not a valid octal digit.
Step-by-step conversion for clarity: the octal number 01234 represents 1×8^3 + 2×8^2 + 3×8^1 + 4×8^0 = 512 + 128 + 24 + 4 = 668 (decimal). That arithmetic is why the program prints 668 when the literal is written with a leading zero.
Practical notes and gotchas: avoid accidental octal literals by not prefixing decimal constants with 0 (this is a common source of subtle bugs). The include of conio.h and void main() in the original post are nonstandard; prefer int main(void) and standard headers only. Also remember that leading-zero octal literals are intentional in some contexts (for example, UNIX file modes like 0755), so be explicit about intent.
If input strings may contain leading zeros but should be treated as decimal, parse them with a routine that forces base 10, for example strtol with a base argument of 10. That avoids the lexical rules that make a numeric token starting with 0 octal and prevents unexpected compile-time/parse-time errors.
Jump to Post— Adak 419Read up in your help files, on how int's with assigned values that begin with a zero, are interpreted by your compiler.
What would an assignment of 080 print up?
Read up in your help files, on how int's with assigned values that begin with a zero, are interpreted by your compiler.
What would an assignment of 080 print up?
Read up in your help files, on how int's with assigned values that begin with a zero, are interpreted by your compiler.
What would an assignment of 080 print up?
the int x=080;
shows illegal octal digit error.
Exactly. The digit "8" does not exist in octal. The digits "1", "2", "3" and "4" do exist in octal.
Compiler treats number starting with 0 as octal number and 01234 is an octal number whose decimal value is 668 as you are printing it with %d specifier so it outputs 668.
check output for this instead:-
printf("%o",x); We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.