Author Topic: Simple C question. <solved>  (Read 1134 times)

Offline docnascar

  • Sr. Member
  • ****
  • Posts: 465
Simple C question. <solved>
« on: January 29, 2011, 03:37:00 PM »
Hi. I'm learning the basics of C and I can't figure this out.

Why does my program give me an answer of 2 if I use 4/2 (which is good). But if I use 2/4 my answer shows "0" and not "0.5" What am I missing in my code?

This works:
Code: [Select]
#include <stdio.h>
#include <math.h>

int main()
{
int answr;

answr = 4 / 2;
printf("The answer is : %d\n",answr);
getchar ();
return (0);
}


This shows "0"... Why  ??? Thanks!!!

Code: [Select]
#include <stdio.h>
#include <math.h>

int main()
{
int answr;

answr = 2 / 4;
printf("The answer is : %d\n",answr);
getchar ();
return (0);
}
« Last Edit: January 29, 2011, 07:45:01 PM by docnascar »
My main PCLINUXOS PC:
KDE Mini
AMD FX-6300 (3.5G / 6 core)
MSI 970A-G46 AM3+ MOBO
G.SKILL Sniper Series 8GB (2 x 4GB) 1866 (PC3 14900)
ECS GeForce GT 440 (Fermi) 512MB 128-bit GDDR5
Seagate Barracuda ST1000DM003 1TB 7200 RPM SATA
SAMSUNG DVD Burner SATA Model SH-224BB
POWERUP PU-550 (550W) p

Offline Village Idiot

  • Hero Member
  • *****
  • Posts: 2345
  • Have A Nice Day.
Re: Simple C question.
« Reply #1 on: January 29, 2011, 03:40:47 PM »
Code: [Select]
int answr;
Change int to float   ;)

An Integer is just that, an integer. A float carries the integer value -and- the numbers on the right hand side of the decimal point. For even more precision, use double.

 :)
$ fortune
No Microsoft products were used in any way for the creation of this message.
If you are using a Microsoft product to view it, BEWARE! - I'm not
responsible for any harm you might encounter as a result.

Online muungwana

  • Hero Member
  • *****
  • Posts: 6254
Re: Simple C question.
« Reply #2 on: January 29, 2011, 03:55:07 PM »

This is a classic example of why C language is seeing as being too easy to shoot yourself with and hence dangerous.

The compiler knows that information is going to be lost when a decimal number is assigned to an interger data type but it doesnt even show a warning. Other language will refuse to do such an assignment and will require a programmer to do explicit casting first.
.. 3 things are certain in life : death, taxes and software bloat ..
.. tell me something i don't know, something i can use as i struggle to reason with the world around me ..

Offline docnascar

  • Sr. Member
  • ****
  • Posts: 465
Re: Simple C question.
« Reply #3 on: January 29, 2011, 06:03:49 PM »
I tried double and float, but I still get an answer of 0 (or 0.00000 for this code) if I do 2/4. How do I get it to display 0.5? Thanks!

Code: [Select]
#include <stdio.h>
#include <math.h>

int main()
{
double answr;

answr = 2 / 4;
printf("The answer is : %2.5f \n",answr);

getchar ();
return (0);
}
My main PCLINUXOS PC:
KDE Mini
AMD FX-6300 (3.5G / 6 core)
MSI 970A-G46 AM3+ MOBO
G.SKILL Sniper Series 8GB (2 x 4GB) 1866 (PC3 14900)
ECS GeForce GT 440 (Fermi) 512MB 128-bit GDDR5
Seagate Barracuda ST1000DM003 1TB 7200 RPM SATA
SAMSUNG DVD Burner SATA Model SH-224BB
POWERUP PU-550 (550W) p

Online muungwana

  • Hero Member
  • *****
  • Posts: 6254
Re: Simple C question.
« Reply #4 on: January 29, 2011, 06:09:07 PM »

try with these lines

double answr;
answr = (double) 2 / 4;
.. 3 things are certain in life : death, taxes and software bloat ..
.. tell me something i don't know, something i can use as i struggle to reason with the world around me ..

Offline docnascar

  • Sr. Member
  • ****
  • Posts: 465
Re: Simple C question.
« Reply #5 on: January 29, 2011, 06:50:21 PM »

This is a classic example of why C language is seeing as being too easy to shoot yourself with and hence dangerous.

The compiler knows that information is going to be lost when a decimal number is assigned to an interger data type but it doesnt even show a warning. Other language will refuse to do such an assignment and will require a programmer to do explicit casting first.

I agree. For beginners like me, its not so straight forward and self explanatory.


try with these lines

double answr;
answr = (double) 2 / 4;

That worked. Thanks!

Can anyone explain why you would have to use (double) with your equation? To me, "answr" was expressed as double when its first called out, so I would not think that you would have to retell it that during the math part.
« Last Edit: January 29, 2011, 06:52:56 PM by docnascar »
My main PCLINUXOS PC:
KDE Mini
AMD FX-6300 (3.5G / 6 core)
MSI 970A-G46 AM3+ MOBO
G.SKILL Sniper Series 8GB (2 x 4GB) 1866 (PC3 14900)
ECS GeForce GT 440 (Fermi) 512MB 128-bit GDDR5
Seagate Barracuda ST1000DM003 1TB 7200 RPM SATA
SAMSUNG DVD Burner SATA Model SH-224BB
POWERUP PU-550 (550W) p

Offline docnascar

  • Sr. Member
  • ****
  • Posts: 465
Re: Simple C question.
« Reply #6 on: January 29, 2011, 07:15:20 PM »
Code: [Select]

float answr;
answr = (float) 2 / 4;

float works as well.
My main PCLINUXOS PC:
KDE Mini
AMD FX-6300 (3.5G / 6 core)
MSI 970A-G46 AM3+ MOBO
G.SKILL Sniper Series 8GB (2 x 4GB) 1866 (PC3 14900)
ECS GeForce GT 440 (Fermi) 512MB 128-bit GDDR5
Seagate Barracuda ST1000DM003 1TB 7200 RPM SATA
SAMSUNG DVD Burner SATA Model SH-224BB
POWERUP PU-550 (550W) p

Online muungwana

  • Hero Member
  • *****
  • Posts: 6254
Re: Simple C question.
« Reply #7 on: January 29, 2011, 07:23:47 PM »
"casting" is converting one data type into another.

in your example, you declared an object of type "double" named "answr" and then you want to put a result of 2 / 4.

Now, both "2" and "4" are integers and hence the computer decided that both are integers and hence a result should also be of type integer and hence "2 / 4" produced "0" since integer numbers do not have a decimal part and that part is ignored. The resulting number is then assigned to your variable and that is why the result was "0"

doing (double) 2 / 4 forced the calculation to be of type double and that is why it worked because the result will then be "0.5" and then "answr" will be assigned "0.5"

your code would have worked if you had any following combination (2.0 / 4), (2 / 4.0), (2.0 / 4.0). These numbers would have forced the compiler to see your numbers as not integers and a proper calculation and assignment would have taken place.

basically, your numbers made the computer assume you were doing an integer calculation and it went all the way with integers and that is why an explicit cast was required to force the computer to do the calculation with double data type in mind.
« Last Edit: January 29, 2011, 07:26:48 PM by muungwana »
.. 3 things are certain in life : death, taxes and software bloat ..
.. tell me something i don't know, something i can use as i struggle to reason with the world around me ..

Offline docnascar

  • Sr. Member
  • ****
  • Posts: 465
Re: Simple C question.
« Reply #8 on: January 29, 2011, 07:44:40 PM »
Thanks. Great explanation. It makes sense.
My main PCLINUXOS PC:
KDE Mini
AMD FX-6300 (3.5G / 6 core)
MSI 970A-G46 AM3+ MOBO
G.SKILL Sniper Series 8GB (2 x 4GB) 1866 (PC3 14900)
ECS GeForce GT 440 (Fermi) 512MB 128-bit GDDR5
Seagate Barracuda ST1000DM003 1TB 7200 RPM SATA
SAMSUNG DVD Burner SATA Model SH-224BB
POWERUP PU-550 (550W) p

Offline critter

  • Full Member
  • ***
  • Posts: 220
Re: Simple C question. <solved>
« Reply #9 on: January 30, 2011, 01:15:34 AM »
Passing values in declared variables can help to avoid a lot of this confusion.

Code: [Select]
#include <stdio.h>
#include <math.h>

main()
{
float answr, a, b;

a=2;
b=4;
answr=(a/b);
printf ("The answer is : %2.5f\n", answr);
return (0);
}
Motherboard   Gigabyte Z68X-UD3H-B3
Hard Drives      2 x Maxtor STM350032 500GB SATA
Memory      16GB RAM
Processor      Intel core i5 3.30GHz
Video         nVidia GeForce GT430
Sound      HDA Intel PCH
PCLinuxOS          KDE

Offline docnascar

  • Sr. Member
  • ****
  • Posts: 465
Re: Simple C question. <solved>
« Reply #10 on: January 30, 2011, 08:52:34 AM »
Thanks Critter. Good example.

My main PCLINUXOS PC:
KDE Mini
AMD FX-6300 (3.5G / 6 core)
MSI 970A-G46 AM3+ MOBO
G.SKILL Sniper Series 8GB (2 x 4GB) 1866 (PC3 14900)
ECS GeForce GT 440 (Fermi) 512MB 128-bit GDDR5
Seagate Barracuda ST1000DM003 1TB 7200 RPM SATA
SAMSUNG DVD Burner SATA Model SH-224BB
POWERUP PU-550 (550W) p

Offline The Chief

  • Hero Member
  • *****
  • Posts: 2253
Re: Simple C question.
« Reply #11 on: January 30, 2011, 12:36:46 PM »

This is a classic example of why C language is seeing as being too easy to shoot yourself with and hence dangerous.


I dispute that - C assumes the user at least knows what kind of data he will be manipulating, and uses strong (well, fairly strong) typing to prevent those almost undetectable bugs that arise from calling a function with the wrong parameters.

Retired Senior Chief, Retired Software Engineer, Active GrandPa

Online muungwana

  • Hero Member
  • *****
  • Posts: 6254
Re: Simple C question.
« Reply #12 on: January 30, 2011, 01:20:01 PM »

This is a classic example of why C language is seeing as being too easy to shoot yourself with and hence dangerous.


I dispute that - C assumes the user at least knows what kind of data he will be manipulating, and uses strong (well, fairly strong) typing to prevent those almost undetectable bugs that arise from calling a function with the wrong parameters.

C complains when you call a function with a wrong parameter? i wrote this to test and this is what i got

code:
Code: [Select]
#include <stdio.h>
#include <math.h>

void output( char z )
{
    printf("The answer is : %d \n",z);   
}

int main()
{
char x = 'A' ;
int y = x ;
printf("The answer is : %d \n",y);
output( y ) ;
return (0);
}

result after compiling and running the program:

[ink@mtz ~]$ ./a.out
The answer is : 65
The answer is : 65

See the problem with C? i assigned a "char" datatype to an "int" data type and nobody complained, not even a warning. I then declared a function that took a type "char" but gave it a type "int" and again, no complain and no error from the compiler. I think this disputes your last statement.
.. 3 things are certain in life : death, taxes and software bloat ..
.. tell me something i don't know, something i can use as i struggle to reason with the world around me ..

Offline The Chief

  • Hero Member
  • *****
  • Posts: 2253
Re: Simple C question.
« Reply #13 on: January 31, 2011, 10:19:57 AM »

That worked. Thanks!

Can anyone explain why you would have to use (double) with your equation? To me, "answr" was expressed as double when its first called out, so I would not think that you would have to retell it that during the math part.

Because the divide operation was given two integers, so it provided an integer answer, without other direction.  As an alternative, you could have written either (or both) of the input numbers (the 2 and 4) with a '.0' after them.

Retired Senior Chief, Retired Software Engineer, Active GrandPa