What does the following code snippet do (in C)?

What does the following code snippet do (in C)?

int a = 033;
printf("%d", a + 1);
+3
source share
4 answers

033is an octal integer literal , and its meaning 8*3+3 = 27. Your code prints 28.

Integer literal starting with 0is octal. If it starts at 0x, it is hexadecimal.

By the way, for the sake example

int x = 08; //error

is a compile-time error, since it is 8not an octal digit.

+6
source

I would venture to guess and say 28:)

0
source

28.

033 C, "0", , 27 .

, 27 + 1 = 28

0

cue:

  • 3- - .
  • 2- "0x" - hex.

Try looking at this example:

 #include<stdio.h>
 main()
 {
 int a = 033;
 printf("\nin decimal: %d", a+1);
 printf("\nin hex: %x", a+1);
 printf("\nin octal: %o", a+1);
 }

it leads to:

in decimal: 28
in hex: 1c
in octal: 34
0
source

Source: https://habr.com/ru/post/1778969/


All Articles