Clear my doubt

Discussion in 'C' started by priyabc, Aug 11, 2007.

  1. priyabc

    priyabc New Member

    Joined:
    Feb 9, 2007
    Messages:
    5
    Likes Received:
    0
    Trophy Points:
    0
    Occupation:
    Student
    Code:
    #define square(x) x*x
    
    main()
    {
     int i;
     i=64/square(4);
     printf("%d",i);
    }
    the result obtained for this
    64

    how it is so??

    i thought 64/square(4) will be 64/4*4 = 64/16 which will be 4... but the result is not so why.. what happens
     
    Last edited by a moderator: Aug 12, 2007
  2. DaWei

    DaWei New Member

    Joined:
    Dec 6, 2006
    Messages:
    835
    Likes Received:
    5
    Trophy Points:
    0
    Occupation:
    Semi-retired EE
    Location:
    Texan now in Central NY
    Home Page:
    http://www.daweidesigns.com
    64/4*4

    64/4 = 16

    16*4 = 64

    If you're going to use macros, you better learn that you need to parenthesize the hell out of them. You would be much wiser to write the function.
     
  3. kaustubh

    kaustubh New Member

    Joined:
    Aug 13, 2007
    Messages:
    25
    Likes Received:
    0
    Trophy Points:
    0
    hi priyabc,
    you should try something like this

    Code:
    #define square(x) x*x
    
    main()
    {
     int i;
     i=(64)/(square(4));
     printf("%d",i);
    }
    
    this mean (64) / (4 * 4) = (64)/(16) = 4
     
  4. DaWei

    DaWei New Member

    Joined:
    Dec 6, 2006
    Messages:
    835
    Likes Received:
    5
    Trophy Points:
    0
    Occupation:
    Semi-retired EE
    Location:
    Texan now in Central NY
    Home Page:
    http://www.daweidesigns.com
    You guys don't listen well. If you must use a macro, do it right. Also, kaustub, the C standard defines main as returning an int. Please don't teach bad habits.
    Code:
    #include <stdio.h>
    
    #define square(x) (x*x)
    
    int main ()
    {
        int i = 64 / square(4);
        printf ("%d", i);
        return 0;
    }
    
     
  5. kaustubh

    kaustubh New Member

    Joined:
    Aug 13, 2007
    Messages:
    25
    Likes Received:
    0
    Trophy Points:
    0
    hi dawei,
    she must be beginner. Most books of beginners written about 5 to 10 years ago didn't contain standard c or c++ . I will try to use known c /c++ standards in posts in future.
     
  6. DaWei

    DaWei New Member

    Joined:
    Dec 6, 2006
    Messages:
    835
    Likes Received:
    5
    Trophy Points:
    0
    Occupation:
    Semi-retired EE
    Location:
    Texan now in Central NY
    Home Page:
    http://www.daweidesigns.com
    The C standards were issued in 1989/90 and 1999. There are lots of compilers that don't completely conform and lots of books and tutorials (including by MS) that teach faulty C.
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice