Quick question

This is a discussion on Quick question within the C Programming forums, part of the General Programming Boards category; Im relatively new to C. On Tuesday we have a small test on simple things. On the sample test I ...

  1. #1
    Registered User
    Join Date
    Oct 2005
    Posts
    12

    Quick question

    Im relatively new to C. On Tuesday we have a small test on simple things. On the sample test I have managed to answer many of the questions however I cant seem to solve a simple expression having to do with double and int. The question is:

    1. What is the value of the following expression in C? Assume a=5, b=7, c=3.2, d=5.0.

    a,b=int
    c,d,e=double

    e=b/a*c+c*d

    From what my friend says, the answer is 19.2; however I do not know how he came to that conclusion. I know the book definitions of int and double, but I cant seem to apply it to this expression. I would appreciate it if someone could show me the process to solving it.

  2. #2
    ---
    Join Date
    May 2004
    Posts
    1,379
    Why don;t you put it into a compiler and find out the answer for yourself?
    Code:
    #include <stdio.h>
    
    int main(void)
    {
    	int a=5, b=7;
    	double c = 3.2, d = 5.0, e;
    	e = b / a * c + c * d;
    	printf("%lf",e);
    	return 0;
    }
    Last edited by sand_man; 10-09-2005 at 10:47 PM.

  3. #3
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,505
    Looks like an attempt to make you understand the difference between integer and floating point division.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  4. #4
    Registered User
    Join Date
    Oct 2005
    Posts
    12
    Well I could put it into pico (we use Unix) but its a written thing so putting it in a compiler wont help. I have to solve everything by hand.
    Last edited by Stiks; 10-10-2005 at 05:05 PM.

  5. #5
    cwr
    cwr is offline
    Registered Luser cwr's Avatar
    Join Date
    Jul 2005
    Location
    Sydney, Australia
    Posts
    868
    Look up order of precedence and associativity in your C book, then work through it according to those rules.

  6. #6
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  7. #7
    cwr
    cwr is offline
    Registered Luser cwr's Avatar
    Join Date
    Jul 2005
    Location
    Sydney, Australia
    Posts
    868
    Quote Originally Posted by Dave_Sinkula
    That's an interesting read, but it does also point out that you can rely on operator precedence rules in C as long as you don't think it has anything to do with order of evaluation. Since we are dealing with simple numeric constants and not anything with side effects, this is no problem.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Very quick math question
    By jverkoey in forum A Brief History of Cprogramming.com
    Replies: 8
    Last Post: 10-26-2005, 11:05 PM
  2. very quick question.
    By Unregistered in forum C++ Programming
    Replies: 7
    Last Post: 07-24-2002, 03:48 AM
  3. quick question
    By Unregistered in forum C++ Programming
    Replies: 5
    Last Post: 07-22-2002, 04:44 AM
  4. Quick Question Regarding Pointers
    By charash in forum C++ Programming
    Replies: 4
    Last Post: 05-04-2002, 11:04 AM
  5. Quick question: exit();
    By Cheeze-It in forum C Programming
    Replies: 6
    Last Post: 08-15-2001, 05:46 PM

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