LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 01-02-2006, 02:08 AM   #1
duffmckagan
Senior Member
 
Registered: Feb 2005
Distribution: Cent OS 6.4
Posts: 1,163

Rep: Reputation: 49
Program runs on Win32 platform, but not on Linux.


I will keep it simple.

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.
 
Old 01-02-2006, 03:40 AM   #2
scuzzman
Senior Member
 
Registered: May 2004
Location: Hilliard, Ohio, USA
Distribution: Slackware, Kubuntu
Posts: 1,851

Rep: Reputation: 47
So what is the compiler output? What happens when you type:
Code:
gcc -o exe_name src_name.c
Of course, replace exe_name with what you want to call the executable file, and src_name with the name of the source code.

Edit: Just tested it on my box. It compiled and ran fine, but I think there may be a mathematical error or two:
Code:
[scuzzy@slackdell /home/scuzzy]$ gcc -o test_prog test_prog.c
[scuzzy@slackdell /home/scuzzy]$ ./test_prog

Please enter your Basic Salary: 20000

Richard's gross salary is 20000
[scuzzy@slackdell /home/scuzzy]$

Last edited by scuzzman; 01-02-2006 at 03:43 AM.
 
Old 01-02-2006, 07:47 AM   #3
duffmckagan
Senior Member
 
Registered: Feb 2005
Distribution: Cent OS 6.4
Posts: 1,163

Original Poster
Rep: Reputation: 49
Quote:
[scuzzy@slackdell /home/scuzzy]$ gcc -o test_prog test_prog.c
[scuzzy@slackdell /home/scuzzy]$ ./test_prog

Please enter your Basic Salary: 20000

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.

Code:
amit@copperskull$gcc -c prog.c
amit@copperskullgcc prog.o -o prog
 
Old 01-02-2006, 09:16 AM   #4
slantoflight
Member
 
Registered: Aug 2005
Distribution: Smoothwall
Posts: 283
Blog Entries: 3

Rep: Reputation: 35
I could be wrong here, but I think you forgot a parenthesis.
Code:
 basic_sal+(40/100)*basic_sal+(20/100)*basic_sal
should be

Code:
basic_sal+((40/100)*basic_sal)+((20/100)*basic_sal))
Normally PEMDAS rules would apply here, but I think the compiler is following PEMDAS from left to right rather than seeking

Last edited by slantoflight; 01-02-2006 at 09:20 AM.
 
Old 01-02-2006, 09:22 AM   #5
Nylex
LQ Addict
 
Registered: Jul 2003
Location: London, UK
Distribution: Slackware
Posts: 7,464

Rep: Reputation: Disabled
I don't think you need to be explicit with the brackets there, as * has a higher precedence than +.
 
Old 01-02-2006, 09:36 AM   #6
dmail
Member
 
Registered: Oct 2005
Posts: 970

Rep: Reputation: Disabled
I think this is the answer, but I can not explain how you got the correct answer in windows.
ok lets look at this line
Code:
gross_sal=basic_sal+(40/100)*basic_sal+(20/100)*basic_sal;
where:
gross_sal is an int
basic_sal is an int
and int/int = int

int = int + (int)* int + (int) * int;
40/100 as an int = 0;
20/100 as an int = 0;
so whats being worked out

gross_sal = basic_sal;
logical errors

float/int = float
int/float = float
40.0/100 = 0.4;
40/100.0 = 0.4;
20.0/100 = 0.2;
20/100.0 = 0.2;

Last edited by dmail; 01-02-2006 at 09:38 AM.
 
Old 01-02-2006, 09:56 AM   #7
duffmckagan
Senior Member
 
Registered: Feb 2005
Distribution: Cent OS 6.4
Posts: 1,163

Original Poster
Rep: Reputation: 49
Quote:
Originally Posted by dmail
I think this is the answer, but I can not explain how you got the correct answer in windows.
ok lets look at this line
Code:
gross_sal=basic_sal+(40/100)*basic_sal+(20/100)*basic_sal;
where:
gross_sal is an int
basic_sal is an int
and int/int = int

int = int + (int)* int + (int) * int;
40/100 as an int = 0;
20/100 as an int = 0;
so whats being worked out

gross_sal = basic_sal;
logical errors

float/int = float
int/float = float
40.0/100 = 0.4;
40/100.0 = 0.4;
20.0/100 = 0.2;
20/100.0 = 0.2;

Wonderful explanation there!

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.
 
Old 01-02-2006, 10:02 AM   #8
duffmckagan
Senior Member
 
Registered: Feb 2005
Distribution: Cent OS 6.4
Posts: 1,163

Original Poster
Rep: Reputation: 49
NOT WORKING!!

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!
 
Old 01-02-2006, 10:08 AM   #9
dmail
Member
 
Registered: Oct 2005
Posts: 970

Rep: Reputation: Disabled
Quote:
Originally Posted by duffmckagan
NOT WORKING!!

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 ;(

instead of
Code:
gross_sal=basic_sal+(40/100)*basic_sal+(20/100)*basic_sal;
try
Code:
gross_sal=basic_sal+(40/100.0)*basic_sal+(20/100.0)*basic_sal;
this will cause of few casts for float to int tho!

which you could change to
Code:
gross_sal=basic_sal+ int( (40/100.0)*basic_sal+(20/100.0)*basic_sal );
so this is:
Code:
int/float = (40/100.0) = float
int/float = (20/100.0) = float
float * int = (20/100.0) * basic_sal = float
float * int = (40/100.0) * basic_sal = float
float + float = float =( (40/100.0)*basic_sal + (20/100.0)*basic_sal )
does that make sense?

I dont think shouting in capitals, using bold or underlining really helps, does it?

Last edited by dmail; 01-02-2006 at 10:38 AM.
 
Old 01-02-2006, 11:47 AM   #10
duffmckagan
Senior Member
 
Registered: Feb 2005
Distribution: Cent OS 6.4
Posts: 1,163

Original Poster
Rep: Reputation: 49
Quote:
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
This was quite clear.

Quote:
float/int = float
int/float = float
40.0/100 = 0.4;
40/100.0 = 0.4;
20.0/100 = 0.2;
20/100.0 = 0.2;
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.

So, this was not to work.

Code:
gross_sal=basic_sal+(40/100)*basic_sal+(20/100)*basic_sal;

Well..thanks a lot dmail. I'm grateful for your help.
 
Old 01-02-2006, 11:50 AM   #11
duffmckagan
Senior Member
 
Registered: Feb 2005
Distribution: Cent OS 6.4
Posts: 1,163

Original Poster
Rep: Reputation: 49
Also, I checked the code that I ran on Windows.


It worked fine with the following line.
Code:
gross_sal=basic_sal+(40/100)*basic_sal+(20/100)*basic_sal;
It had float gross_sal.
But seems like the type-conversion of the Visual Studio C++ Compiler is better.
So, I got the proper out.
 
Old 01-02-2006, 12:10 PM   #12
dmail
Member
 
Registered: Oct 2005
Posts: 970

Rep: Reputation: Disabled
Quote:
Originally Posted by duffmckagan
Also, I checked the code that I ran on Windows.


It worked fine with the following line.
Code:
gross_sal=basic_sal+(40/100)*basic_sal+(20/100)*basic_sal;
It had float gross_sal.
But seems like the type-conversion of the Visual Studio C++ Compiler is better.
So, I got the proper out.
What? Ok this is my last post on the matter, I don't want to be rude but please take note ;(

this is your orignal code
Code:
#include <stdio.h>
int main(int argc, char *argv[])
{
      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);
      return 0;
}


compiled with visual studio 2003.
input = 100;
output = 100; hmmm something wrong here

this is the code with one line changed
Code:
#include <stdio.h>
int main(int argc, char *argv[])
{


	int basic_sal,gross_sal;
	printf("\nPlease enter your Basic Salary: ");
	scanf("%d",&basic_sal);
	gross_sal=basic_sal+int( (40/100.0)*basic_sal+(20/100.0)*basic_sal);
	printf ("\nRichard's gross salary is %d\n", gross_sal);
        return 0;
}
compiled with visual studio 2003.
input = 100;
output = 160; woot this is correct as it shows the whole $/'s

you keep saying you get it, but you don't!

int / int = int
an int has no decimal place.
10/50 if both are ints gives 0
0 * any_number = 0
so what you are adding to the basic_sal is 0 and 0

Last edited by dmail; 01-02-2006 at 12:17 PM.
 
Old 01-02-2006, 01:35 PM   #13
paulsm4
LQ Guru
 
Registered: Mar 2004
Distribution: SusE 8.2
Posts: 5,863
Blog Entries: 1

Rep: Reputation: Disabled
Dmail - I can appreciate your frustration ;-)

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...

Last edited by paulsm4; 01-02-2006 at 01:37 PM.
 
Old 01-02-2006, 10:00 PM   #14
sundialsvcs
LQ Guru
 
Registered: Feb 2004
Location: SE Tennessee, USA
Distribution: Gentoo, LFS
Posts: 9,078
Blog Entries: 4

Rep: Reputation: 3175Reputation: 3175Reputation: 3175Reputation: 3175Reputation: 3175Reputation: 3175Reputation: 3175Reputation: 3175Reputation: 3175Reputation: 3175Reputation: 3175
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.
 
Old 01-02-2006, 10:43 PM   #15
duffmckagan
Senior Member
 
Registered: Feb 2005
Distribution: Cent OS 6.4
Posts: 1,163

Original Poster
Rep: Reputation: 49
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.

Dmail, thanks again, you have been of great help.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
cross platform C or C++ API for linux and Win32 tcma Linux - Software 1 12-09-2004 11:05 PM
How to program under Linux/Unix platform Igor007 Programming 2 08-19-2004 04:54 PM
How to program under Linux/Unix platform Igor007 Linux - Newbie 3 08-19-2004 02:18 PM
How to program under Linux/Unix platform Igor007 Solaris / OpenSolaris 2 08-19-2004 02:17 PM
How to program under Linux/Unix platform Igor007 *BSD 1 08-19-2004 02:14 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 08:14 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration