You are not logged in. Please login at to post your questions!


printf problem %d and %lld

I declared a variable as int ans;

And then used printf as printf("%lld\n",ans); I got wrong answer.

When I changed it to %d I got AC.

But in my computer I was getting correct output. Any explanation why??

Any help is appreciated...

asked 19 Dec '14, 13:47

king_of_hacker's gravatar image

accept rate: 7%

What can happen if printf is called with a wrong format string?

Anything can happen. It is Undefined behavior! Undefined behavior means that anything can happen. It may show you results which you expect or it may not or it may crash. Anything can happen and you can blame no one but yourself about it.


c99 Standard:

para 9:

If a conversion specification is invalid, the behavior is undefined.225) If any argument is not the correct type for the corresponding coversion specification, the behavior is undefined.


answered 19 Dec '14, 15:03

acodebreaker2's gravatar image

accept rate: 18%

1 see this code , the format specifier of printf donot match with the data type still it gives correct answer ,

now see this still the format specifier of printf donot match , it gives the wrong answer , so the behaviour is undefined .

(19 Dec '14, 15:22) acodebreaker21★

%lld is used for printing long long int and %d is used for printing integer variables. May be on your computer you were using different compiler so you were getting right answer. Codechef uses gcc compiler and hence you got wrong answer.


answered 19 Dec '14, 13:57

the65bit's gravatar image

accept rate: 13%

I too use gcc only

(19 Dec '14, 14:10) king_of_hacker3★

Have a look here

(19 Dec '14, 14:56) the65bit4★
(19 Dec '14, 14:57) the65bit4★

@the65bit but why in my computer i get the same output for both ur codes?? i am using gcc 4.8.1


answered 19 Dec '14, 15:39

king_of_hacker's gravatar image

accept rate: 7%

edited 19 Dec '14, 15:40

when the code is erroneous, different compilers may behave differently. You have to make sure that the code you write is correct from your side.

(19 Dec '14, 17:26) gvaibhav217★

Different compilers behave differently as @acodebreaker2 pointed.

(19 Dec '14, 20:56) the65bit4★
toggle preview

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here



Answers and Comments

Markdown Basics

  • *italic* or _italic_
  • **bold** or __bold__
  • link:[text]( "title")
  • image?![alt text](/path/img.jpg "title")
  • numbered list: 1. Foo 2. Bar
  • to add a line break simply add two spaces to where you would like the new line to be.
  • basic HTML tags are also supported
  • mathemetical formulas in Latex between $ symbol

Question tags:


question asked: 19 Dec '14, 13:47

question was seen: 5,337 times

last updated: 19 Dec '14, 20:56