operators

This is a discussion on operators within the C Programming forums, part of the General Programming Boards category; i am a beginner in programming and i have a doubt: what is wrong with the following code? the result ...

  1. #1
    Registered User
    Join Date
    Aug 2007
    Posts
    28

    operators

    i am a beginner in programming and i have a doubt:
    what is wrong with the following code? the result is always 0...

    Code:
    #include <stdio.h>
    
    float pi;
    float raio;
    float volume;
    char line[100];
    
    int main() {
    	pi = 3.14159265;
    	printf("qual o raio da esfera? ");
    	fgets(line, sizeof(line), stdin);
    	sscanf(line, "%f", &raio);
    	volume = (3/4) * pi * (raio*raio*raio); /* 0.75 instead of 3/4 does the job... */
    	printf("volume e %f\n", volume);
    	return (0);
    }

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,743
    As it is integer division, 3/4 = 0
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Aug 2007
    Posts
    28
    Quote Originally Posted by laserlight View Post
    As it is integer division, 3/4 = 0
    thanks for the reply.
    what do i need to change to be able to use "3/4" then?

  4. #4
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,801
    3.0/4.0 or just 0.75 instead.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  5. #5
    Registered User
    Join Date
    Aug 2007
    Posts
    28
    Quote Originally Posted by hk_mp5kpdw View Post
    3.0/4.0 or just 0.75 instead.
    I can believe that i made this mistake...
    Thanks for the quick answers!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Logical Operators in C++
    By Flecto in forum C++ Programming
    Replies: 4
    Last Post: 05-15-2009, 07:17 AM
  2. Bolean Operators hurt my head. (Trouble understanding) :(
    By Funcoot in forum C++ Programming
    Replies: 3
    Last Post: 01-20-2008, 06:42 PM
  3. operators???
    By arjunajay in forum C++ Programming
    Replies: 11
    Last Post: 06-25-2005, 04:37 AM
  4. floating point operators
    By DavidP in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 10-22-2003, 07:53 PM
  5. Operators
    By George in forum C++ Programming
    Replies: 3
    Last Post: 04-02-2003, 06:35 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21