473,397 Members | 2,068 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,397 software developers and data experts.

output of a c program and the reason behind it

2
tell me the output of the following c program and the reason behind it

main()
{
int a=400;
int b;
b=a*a/a;
printf("%d",b);
}
Dec 8 '06 #1
3 2185
horace1
1,510 Expert 1GB
tell me the output of the following c program and the reason behind it

main()
{
int a=400;
int b;
b=a*a/a;
printf("%d",b);
}
the output is 400
a equals 400, the result of the expression a*a/a equals a which is assigned to b, b then equals 400
Dec 8 '06 #2
tell me the output of the following c program and the reason behind it

main()
{
int a=400;
int b;
b=a*a/a;
printf("%d",b);
}


hi there,

in precedence of operators when writing complex expressions with several operands, we may have some doubts about which operand is evaluated first and which later. in your expression
b = a*a/a
we may doubt if it really means:

a = (400*400)/400 // with a result of 400, or a = 400 *(400/400) // with a result of 400 , although you can get the same result this is very critical, what if the values are changed?

The correct answer is the first of the two expressions, with a result of 400. There is an established order with the priority of each operator, and not only the arithmetic ones (those whose preference come from mathematics) but for all the operators which can appear in C++. From greatest to lowest priority,in reading data it always finds the which always comes first.


ok

regards,
mike
Dec 8 '06 #3
Banfa
9,065 Expert Mod 8TB
because * and / have left to right associativity

b=a*a/a;

is evaluated as

b=(a*a)/a;

However I not that a and b are ints so the result of this is dependent on the size of int, typically int is either 2 or 4 bytes. If int is 4 bytes then the result is

b = (400*400) / 400
b = (160000) / 400
b = 400

However if int is 2 bytes then the multiplicaltion 400 * 400 = 160000 is behond the bounds of an int. What you actually get is undefined behaviour, i.e. anything could happen. However probably the most likely result is that the extra bits are droped therefore 400 * 400 = 28928 so

b = (400*400) / 400
b = (28928) / 400 (note this is not a whole number but fractions are dropped)
b = 72



This is a feature of integer arithmatic, when doing sums with big numbers you always need to check that you will not run into wrapping errors.
Dec 8 '06 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

4
by: Kevin Mansel via .NET 247 | last post by:
Ok, basically this is my problem. I'm building a console app tocall a dos program. So i'm using the Shell command to call theprogram, now depending on what happens, I want to read theoutput that...
11
by: Russ | last post by:
I'd like to get output formatting for my own classes that mimics the built-in output formatting. For example, >>> x = 4.54 >>> print "%4.2f" % x 4.54 In other words, if I substitute a...
17
by: Nikhil Bokare | last post by:
#include<stdio.h> int main() { int a; printf("%d %d %d %d",a,*a,**a,&a); } I tried the above code and got the same value for a, *a , **a and &a. Can anyone please tell me the reason behind...
87
by: pereges | last post by:
I have a C program which I created on Windows machine. I have compiled and executed the program on windows machine and it gives me the consistent output every time i run it. for eg. input a = 2,...
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
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
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
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.