You are not logged in. Please login at www.codechef.com 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

3★king_of_hacker
204312
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.

Reference:

c99 Standard: 7.19.6.1:

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.

link

answered 19 Dec '14, 15:03

acodebreaker2's gravatar image

1★acodebreaker2
1.2k12
accept rate: 18%

1

http://ideone.com/5FvU0d see this code , the format specifier of printf donot match with the data type still it gives correct answer ,

now see this http://ideone.com/nKavO8 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.

link

answered 19 Dec '14, 13:57

the65bit's gravatar image

4★the65bit
1.1k101328
accept rate: 13%

I too use gcc only

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

Have a look here https://ideone.com/P2da7u

(19 Dec '14, 14:56) the65bit4★
1
(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

link

answered 19 Dec '14, 15:39

king_of_hacker's gravatar image

3★king_of_hacker
204312
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
Preview

Follow this question

By Email:

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

By RSS:

Answers

Answers and Comments

Markdown Basics

  • *italic* or _italic_
  • **bold** or __bold__
  • link:[text](http://url.com/ "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:

×2,738
×2,475
×139
×32
×8
×6

question asked: 19 Dec '14, 13:47

question was seen: 5,506 times

last updated: 19 Dec '14, 20:56