+ Reply to Thread
Page 2 of 3
FirstFirst 1 2 3 LastLast
Results 11 to 20 of 21

Thread: Confused about expression evaluation

  1. #11
    Learning Programmer R3.RyozKidz is an unknown quantity at this point R3.RyozKidz's Avatar
    Join Date
    Oct 2009
    Posts
    96

    Re: Confused about expression evaluation

    i havent learn boolean now~ T_T..
    i wan to buy a book and learn now..~.. blek..`

  2. #12

  3. #13
    Newbie n00bcoder is an unknown quantity at this point
    Join Date
    Oct 2009
    Posts
    8

    Re: Confused about expression evaluation

    another one of my n00b questions : { I have this book which I'm trying to do exercises from and some of the questions I have no clue about!}


    Code:
    flota a = 0.7;
    if(a < 0.7)
      printf("A");
    else
      printf("B");
    The book says the o.p is A. Can someone please explain why that's the o.p? I'm completely confused

  4. #14
    dcs
    dcs is offline
    Guru dcs is just really nice dcs is just really nice dcs is just really nice dcs is just really nice
    Join Date
    Mar 2008
    Posts
    769

    Re: Confused about expression evaluation

    Loss of precision and data types.
    Code:
    #include<stdio.h>
    
    int main()
    {
       float a = 0.7;
       /*
        * The value 0.7 a double; when 'a' is initialized, it loses precision when
        * being stored as a float. This is done silently.
        * 
        * In the comparison below, the float value 'a' is compared to the double
        * value 0.7. In the process 'a' is promoted to a double, but it doesn't gain
        * back any precision already lost in the initialization.
        */
       if ( a < 0.7 )
       {
          puts("A"); /* the answer I get */
       }
       else
       {
          puts("B");
       }
       /*
        * If we make the comparison of 'a' with the float value 0.7, both will have
        * lost the same amount of precision and the expected answer is what we get.
        */
       if ( a < 0.7F )
       {
          puts("C");
       }
       else
       {
          puts("D"); /* the answer I get */
       }
       return 0;
    }
    
    /* my output
    A
    D
    */

  5. #15
    Newbie n00bcoder is an unknown quantity at this point
    Join Date
    Oct 2009
    Posts
    8

    Re: Confused about expression evaluation

    thanks a lot

    btw, is there any specific "must read" book/website where a lot of such things are very clearly mentioned or taken up as examples etc. so that I don't have to come across such problems only in tests?

    BTW, I'm currently referring to Kernighan&Ritchie.

  6. #16
    Learning Programmer R3.RyozKidz is an unknown quantity at this point R3.RyozKidz's Avatar
    Join Date
    Oct 2009
    Posts
    96

    Re: Confused about expression evaluation

    if i wan to know more about thing kind of stuff , wat kind of reference u guys suggest? any suggestion ?

  7. #17
    Super Moderator WingedPanther has much to be proud of WingedPanther has much to be proud of WingedPanther has much to be proud of WingedPanther has much to be proud of WingedPanther has much to be proud of WingedPanther has much to be proud of WingedPanther has much to be proud of WingedPanther has much to be proud of WingedPanther has much to be proud of WingedPanther's Avatar
    Join Date
    Jul 2006
    Age
    36
    Posts
    11,693
    Blog Entries
    57

    Re: Confused about expression evaluation

    K&R is a good resource. The ANSI standard is another (not user-friendly) option.
    CodeCall Blog | CodeCall Wiki | Shareware
    Programming is a branch of mathematics.
    My CodeCall Blog | My Personal Blog

  8. #18
    Newbie n00bcoder is an unknown quantity at this point
    Join Date
    Oct 2009
    Posts
    8

    Re: Confused about expression evaluation

    Code:
    char p[] = "%d\n";
    p[1] = 'c';
    printf(p,65);
    The program prints A

    can someone please explain the logic in this one?

  9. #19
    Super Moderator WingedPanther has much to be proud of WingedPanther has much to be proud of WingedPanther has much to be proud of WingedPanther has much to be proud of WingedPanther has much to be proud of WingedPanther has much to be proud of WingedPanther has much to be proud of WingedPanther has much to be proud of WingedPanther has much to be proud of WingedPanther's Avatar
    Join Date
    Jul 2006
    Age
    36
    Posts
    11,693
    Blog Entries
    57

    Re: Confused about expression evaluation

    65 is the ascii code for A.
    CodeCall Blog | CodeCall Wiki | Shareware
    Programming is a branch of mathematics.
    My CodeCall Blog | My Personal Blog

  10. #20
    Newbie n00bcoder is an unknown quantity at this point
    Join Date
    Oct 2009
    Posts
    8

    Re: Confused about expression evaluation

    please take time to explain the logic of the program. I just can't seem to figure it out myself


    Code:
    char *x = "Alice";  // this means that x is a char pointer which points to the starting location of string "Alice"
    n = strlen(x); 
    *x = x[n]; // but what is this? x was a pointer so what does this statement mean? 
    for(i = 0; i<=n;i++)
    {
      printf("%s", x);
      x++;
    }

+ Reply to Thread
Page 2 of 3
FirstFirst 1 2 3 LastLast

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

     

Similar Threads

  1. How to start? Confused!!!!
    By harpreetsingh2k in forum General Programming
    Replies: 7
    Last Post: 12-01-2008, 08:49 AM
  2. Convert a generic language expression to excel formula?
    By arunsinbox in forum General Programming
    Replies: 3
    Last Post: 09-24-2008, 10:33 PM
  3. Count words using Regular Expression in c#
    By lartzi in forum C# Programming
    Replies: 1
    Last Post: 06-29-2008, 09:56 AM
  4. Replies: 2
    Last Post: 02-27-2008, 03:04 PM
  5. c# regular expression
    By moonrise in forum C# Programming
    Replies: 3
    Last Post: 05-22-2006, 04:54 PM

Bookmarks

Bookmarks

     
        Algorithms and Data Structures

        Java tutorials

        Algorithms Forum

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts