ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
I am learning C programming. My Program runs fine on Windows, but not on Linux. I am using Debian Sarge.
My GCC version is -> gcc version 3.3.5 (Debian 1:3.3.5-13)
Here is a sample program that runs fine on Windows, but not on Linux.
Code:
/*Richard's basic salary is input to the keyboard.
His dearness allowance is 40% of basic salary, and house rent allowance is 20% of basic salary.
Write a program to calculate his gross salary.
*/
/* Gross salary = Basic salary + Dearness allowance + House rent allowance
= Basic salary + (40/100)*Basic salary + (20/100)*Basic salary
*/
#include<stdio.h>
main()
{
int basic_sal,gross_sal;
printf("\nPlease enter your Basic Salary: ");
scanf("%d",&basic_sal);
gross_sal=basic_sal+(40/100)*basic_sal+(20/100)*basic_sal;
printf ("\nRichard's gross salary is %d\n", gross_sal);
}
This is an example proram. Very simple though, it doesn't run on Linux
What is wrong?
I know that there are differences between programming on Different platforms, but I tried to search google and found nothing.
Please link me to information that would be relevant.
Thank you.
Last edited by duffmckagan; 01-02-2006 at 02:10 AM.
Richard's gross salary is 20000
[scuzzy@slackdell /home/scuzzy]$
Exactly. That is where the problem lies!
There is no mathematical error in the program.
I got the proper output in Windows. But I get the same output as you get.
The gross salary (output) is simply equal to the basic salary!
So thats what I ask. Are there any differences I should look for while programming on the Win32 and the Linux platforms. If yes, what are they / please link to it.
I compile and run programs in the following manner.
I don't know how was I able to get it done in Windows.
I was using the Visual Studio C++ compiler.
Maybe, I changed the int to float for a while, and got it running.
I will recheck and find out.
But thanks man. Thats a Wonderful explanation. I will post back with int made float. Should work.
Changed both to float and inputted value in decimals (for basic salary)-- to get rid of type conversion errors.
Still gross salary=basic salary!
Changed only gross_sal to float, but still no luck at all.
Well..please make it clear that are there any obvious changes to be considered while programming on different platforms? Win32 and Linux in this case. This goes unattended every time!
Changed both to float and inputted value in decimals (for basic salary)-- to get rid of type conversion errors.
Still gross salary=basic salary!
Changed only gross_sal to float, but still no luck at all.
Well..please make it clear that are there any obvious changes to be considered while programming on different platforms? Win32 and Linux in this case. This goes unattended every time!
Quote:
Wonderful explanation there...
maybe so, but I don't think you got what i was saying ;(
I dont think shouting in capitals, using bold or underlining really helps, does it?
I know all the rules about posting threads ...blah...blah..blah!
I also have been found complaining about posting in Capital letters. But this time, its me! LOL
Quote:
int = int + (int)* int + (int) * int;
40/100 as an int = 0;
20/100 as an int = 0;
so whats being worked out
Yes. True Indeed.
I was thinking about it as to what is the big deal with this one.
I think I got your point now. It seems like a type conversion problem with the GCC compiler.
float gross_sal couldn't take a value for an int value being calculated, it seems.
Duffmckagan - As you know, some things are "compiler dependent", and some things are "standard". The rules of precedence ("*" binds before "+"; parenthesis binds before either of them) are standard. The rules of conversion (if either operand is a "float", the other is converted to "float") are also standard. By these rules, your program should run exactly the same under both Windows and Gcc.
And by my tests, they *do* run exactly the same:
Code:
/*
* Richard's basic salary is input to the keyboard.
* Gross salary = Basic salary + Dearness allowance + House rent allowance
* = Basic salary + (40/100)*Basic salary + (20/100)*Basic salary
*
* SAMPLE BUILD:
* cl x.c # Visual Studio 6.0
* cc -g -o x x.c $ Gcc 3.3.4
*
* SAMPLE OUTPUT:
* Windows: GCC:
* A) Gross salary is 20000 A) Gross salary is 20000
* B) Gross salary is 32000 B) Gross salary is 32000
* C) Gross salary is 32000 C) Gross salary is 32000
* <= Results *identical*
*/
#include <stdio.h>
int
main()
{
int basic_sal,gross_sal;
#if 0
printf("\nPlease enter your Basic Salary: ");
scanf("%d",&basic_sal);
#else
basic_sal = 20000;
#endif
/* A: Integer arithmetic */
gross_sal = basic_sal + (40/100) * basic_sal + (20/100) * basic_sal;
printf ("A) Gross salary is %d\n", gross_sal);
/* B: mixed arithmetic (minimal parenthesis) */
gross_sal = basic_sal + (40/100.0) * basic_sal + (20/100.0) * basic_sal;
printf ("B) Gross salary is %d\n", gross_sal);
/* C: mixed arithmetic (explicit casting) */
gross_sal = basic_sal + (double)(40/100.0) * basic_sal + (double)(20/100.0) * basic_sal;
printf ("C) Gross salary is %d\n", gross_sal);
return 0;
}
So will you please acknowledge DMail's assertion that the expression "(40/100.0)" is a non-zero float, whereas the expression "(40/100)" is a zero int, therein lies the problem ... and NONE of this has ANYTHING to do with Visual Studio vs Gcc?
You can also verify the underlying behavior by compiling with "/Fa" or "-S" to generate assembly output...
The fundamental issue here is that you are doing the arithmetic using integer types, of unspecified length, to perform calculations that you have clearly designed in terms of floating-point operations.
In circumstances like these you need to be very explicit about how you want the compiler to produce the code.... what precision to carry the result in, when to convert from integer to float and back, and so on.
(1) Declare and use a floating-point variable to hold the initial result of your calculation.
(2) As a further clue to the compiler, say "(40.0/100.0)" or simply "0.40".
(3) I might even cast the integers to floating-point in the expression.
When you have calculated the result and stored it in the temporary variable, then assign it to the integer final-result.
When you are very explicit like this, you leave no uncertainty as to what you'll get. The compiler will still produce very efficient code, which might not closely resemble what you wrote in source.
Thanks a lot dmail.
I think I was in a bit of a hurry when I posted my last post.
You have been of great help here and I appreciate that. You have not been rude or anything like that, and I agree that your point is absolutely correct.
I can't really challenge you on such things, cuz I am just a beginner at C Programming. The only thing that I can do is learn from you guys.
paulsm4 & sundialsvcs..I agree with what both of you've said.
No more doubts here. I think I got your point.
I think I made a mistake while posting about the Visual Studio Compiler.
I will take a detailed look at the code, and this time, I am in no hurry.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.