473,399 Members | 2,278 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,399 software developers and data experts.

puzzle in C

Hi all,

main()
{
int a=12,b=10;
printf("%d , a+b");
}

the output is
is
10,a+b

why it is so?
I am using Turbo C compiler on Windows

plz give me reply soon
regs
sreelal
Nov 14 '05 #1
4 1067
sreelal wrote:
Hi all,

main()
{
int a=12,b=10;
printf("%d , a+b");
}

the output is
is
10,a+b

why it is so?


Random luck. You gave too few arguments to printf which, upon seeing
the specifier "%d" looks for a value. It happens to find 10; it might
have found anything, or gotten violently ill.

More dumb luck allowed you to get away with the required prototype for
the variadic printf, which including <stdio.h> would give you. It also
allowed you to get away with either the implicit int for the return type
for main (required to be explicit in C99) or leaving off 'return 0;' or
equivalent (needed for C89), and leaving off the end-of-line character
on the last line of output (needed for predictable portable behavior).

The program

#include <stdio.h>

int main(void)
{
int a = 12, b = 10;
printf("%d , a+b\n");
return 0;
}

gives for output my system (this time):

826102610 , a+b

Where it got that number, it only knows. At least it didn't segfault.
Nov 14 '05 #2
sreelal wrote:
Hi all,

main()
{
int a=12,b=10;
printf("%d , a+b");
}

the output is
is
10,a+b

why it is so?
I am using Turbo C compiler on Windows

plz give me reply soon
regs
sreelal


First of all, was that the program you intended to write? Did you
perhaps intend to write:

printf("%d", a+b);

Which would print:

22
To answer the question that you asked literally, in short, you gave
printf() the format string "%d , a+b" but you didn't give it a value to
print in place of %d; hence your use of printf is wrong and its
behaviour is undefined. That it printed 10 is coincidence.

Long answer. Since your format string includes %d the printf() function
assumes that it has been called with a second argument, a signed
integer. Arguments to functions are conventionally passed on the stack.
In the case of your program only one argument is placed on the stack -
the memory location of the string literal "%d , a+b". However the
printf function will try to read two values from the stack - the memory
location of the format string, and a signed integer to print in place of
%d. Since nothing was placed on the stack for the latter argument the
value printed could be anything, depending on whatever value happens to
be just above the stack in your computer's memory at the time. For you
it was the number 10, which may or may not be related to the previous
statement, the assignment of 10 to b. If so this was a coincidence.
Basically your program as printed is wrong and it could behave
differently depending on your compiler, operating system and platform.

Hope this helps,
Sam
Nov 14 '05 #3
Sam Jervis wrote:
sreelal wrote:
Hi all,

main()
{
int a=12,b=10;
printf("%d , a+b");
}

the output is
is
10,a+b

why it is so?
I am using Turbo C compiler on Windows

plz give me reply soon
regs
sreelal


First of all, was that the program you intended to write? Did you
perhaps intend to write:

printf("%d", a+b);

Which would print:

22
To answer the question that you asked literally, in short, you gave
printf() the format string "%d , a+b" but you didn't give it a value to
print in place of %d; hence your use of printf is wrong and its
behaviour is undefined. That it printed 10 is coincidence.

Long answer. Since your format string includes %d the printf() function
assumes that it has been called with a second argument, a signed
integer. Arguments to functions are conventionally passed on the stack.
In the case of your program only one argument is placed on the stack -
the memory location of the string literal "%d , a+b". However the
printf function will try to read two values from the stack - the memory
location of the format string, and a signed integer to print in place of
%d. Since nothing was placed on the stack for the latter argument the
value printed could be anything, depending on whatever value happens to
be just above the stack in your computer's memory at the time. For you
it was the number 10, which may or may not be related to the previous
statement, the assignment of 10 to b. If so this was a coincidence.
Basically your program as printed is wrong and it could behave
differently depending on your compiler, operating system and platform.

Hope this helps,
Sam

Guessing:
probably a and b are stack variables for his compiler and since 10 is the
last one on the stack it got scoffed up by printf looking for an arg
The other guy who answered below and got a total different answer - well,
maybe his compiler used register vars for a and b and that would leave the
stack in a different state from the first guy. Who knows, as you said,
its undefined.
Eric

Nov 14 '05 #4
In <HusFc.17164$Oq2.13579@attbi_s52> Eric <no******@comcast.net> writes:
The other guy who answered below and got a total different answer - well,
maybe his compiler used register vars for a and b and that would leave the
stack in a different state from the first guy. Who knows, as you said,
its undefined.


And, therefore, there is little point in speculating about why printf
picked one value or another.

Dan
--
Dan Pop
DESY Zeuthen, RZ group
Email: Da*****@ifh.de
Nov 14 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Developwebsites | last post by:
Hi all, I've made a sliding puzzle game in shockwave which works just fine, except I dont know how to have it solve itself. the URL is: http://members.aol.com/rglukov/games/selfsolve.htm ...
42
by: Frank Buss | last post by:
I've setup a challenge, mainly for C++, Java and Lisp, but every other language is welcome: http://www.frank-buss.de/challenge/index.html There is nothing to win, but I hope there will be some...
1
by: xavier vazquez | last post by:
I have a problem with a program that does not working properly...when the program run is suppose to generate a cross word puzzle , when the outcome show the letter of the words overlap one intop of...
0
by: xavier vazquez | last post by:
have a problem with a program that does not working properly...when the program run is suppose to generate a cross word puzzle , when the outcome show the letter of the words overlap one intop of the...
5
by: ashish0799 | last post by:
HI I M ASHISH I WANT ALGORYTHMUS OF THIS PROBLEM Jigsaw puzzles. You would have solved many in your childhood and many people still like it in their old ages also. Now what you have got to do...
3
by: oncue01 | last post by:
Word Puzzle Task You are going to search M words in an N × N puzzle. The words may have been placed in one of the four directions as from (i) left to right (E), (ii) right to left (W), (iii) up...
6
by: Phoe6 | last post by:
Hi All, I would like to request a code and design review of one of my program. n-puzzle.py http://sarovar.org/snippet/detail.php?type=snippet&id=83 Its a N-puzzle problem solver ( Wikipedia page...
2
by: Gio | last post by:
I'm getting K&R (it's on the way), should I also get the Answer Book? And while I'm there, should I get the Puzzle Book? Or should I save the Puzzle Book for when I'm more advanced? - Gio ...
4
by: honey777 | last post by:
Problem: 15 Puzzle This is a common puzzle with a 4x4 playing space with 15 tiles, numbered 1 through 15. One "spot" is always left blank. Here is an example of the puzzle: The goal is to...
5
by: dmf1207 | last post by:
Hi All! I'm new to javascript and need a little help with a simple puzzle im trying to design. I have a 600x100 pixel picture that I have sliced into 6 100x100 rectangles making a table of of 6...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.