PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Thursday, August 11, 2022

[FIXED] Why output of int 070 is 56 in C program?

 August 11, 2022     c, decimal, int, octal     No comments   

Issue

Can you explain it? Why it given 56 value as output?

#include <stdio.h> 
#include <conio.h>

void main()
{
    int x = 070;
    printf("%d", x);
    getch();
}

Solution

Any integer literal (integer constant) starting with 0 is an octal representation.

Quoting C11, chapter §6.4.4.1, Integer constants

octal-constant:

 0
 octal-constant octal-digit

and

octal-digit: one of

  0 1 2 3 4 5 6 7

and, as per chapter §7.21.6.1, for %d format specifier with printf(), (emphasis mine)

d,i The int argument is converted to signed decimal [...]

Thereby, octal 70 == decimal 56.



Answered By - Sourav Ghosh
Answer Checked By - Timothy Miller (PHPFixing Admin)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing