Like Tree4Likes
  • 1 Post By tabstop
  • 1 Post By AndrewHunter
  • 2 Post By AndrewHunter

c tricky code

This is a discussion on c tricky code within the C Programming forums, part of the General Programming Boards category; Code: #include<stdio.h> void main() { float a=0.7; if(a<0.7) printf("c"); else printf("a"); } here 'a' should get printed according to me ...

  1. #1
    Registered User
    Join Date
    Aug 2011
    Posts
    91

    Exclamation c tricky code

    Code:
    #include<stdio.h>
    void main()
    {
    float a=0.7;
    if(a<0.7)
    printf("c");
    else
    printf("a");
    }
    here 'a' should get printed according to me as the condition (0.7<0.7) is false but 'c'
    is printed instead can somebody please explain?

  2. #2
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    That's because floating point values cannot always be represented exactly...

    Try this...
    Code:
    #include<stdio.h>
    int main( void )   // the correct form of main
    {
         float a = 0.7;
         
         prinf("a = %f\n", a);
    
         if(a < 0.7)
           printf("c");
         else
           printf("a");
    
          return 0;  // error level returned to OS.
    }
    What did it print for the actual value of a?

  3. #3
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    And even better you've got two different approximations running around -- 0.7 is a double, while a is a float, hence is rounded to a different number of bits.
    Salem likes this.

  4. #4
    Registered User
    Join Date
    May 2011
    Location
    Around 8.3 light-minutes from the Sun
    Posts
    1,866
    Quote Originally Posted by theju112 View Post
    Code:
    #include<stdio.h>
    void main()
    {
    float a=0.7;
    if(a<0.7)
    printf("c");
    else
    printf("a");
    }
    here 'a' should get printed according to me as the condition (0.7<0.7) is false but 'c'
    is printed instead can somebody please explain?
    The default data type for floating point numbers in C is double, so if you don't specify when your double gets demoted to a float for comparison you loose accuracy. Try this instead:
    Code:
    int main(void){
    
    	float a = 0.7;
    
    	if(a < 0.7f)
    		printf("boo");
    	else
    		printf("yeah");
    
    	getchar();
    	return (0);
    }
    theju112 likes this.
    Quote Originally Posted by anduril462 View Post
    Now, please, for the love of all things good and holy, think about what you're doing! Don't just run around willy-nilly, coding like a drunk two-year-old....
    Quote Originally Posted by quzah View Post
    ..... Just don't be surprised when I say you aren't using standard C anymore, and as such,are off in your own little universe that I will completely disregard.
    Warning: Some or all of my posted code may be non-standard and as such should not be used and in no case looked at.

  5. #5
    Registered User
    Join Date
    Aug 2011
    Posts
    91
    andrew hunters code is working..yeah!!

  6. #6
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by theju112 View Post
    andrew hunters code is working..yeah!!
    I was as surprised as you were at the news.


    Quzah.
    Hope is the first step on the road to disappointment.

  7. #7
    Registered User
    Join Date
    May 2011
    Location
    Around 8.3 light-minutes from the Sun
    Posts
    1,866
    Quote Originally Posted by quzah View Post
    I was as surprised as you were at the news.
    Quzah.
    LOL.....every once in awhile the random keys I press actually produces something....
    dennis.cpp and theju112 like this.
    Quote Originally Posted by anduril462 View Post
    Now, please, for the love of all things good and holy, think about what you're doing! Don't just run around willy-nilly, coding like a drunk two-year-old....
    Quote Originally Posted by quzah View Post
    ..... Just don't be surprised when I say you aren't using standard C anymore, and as such,are off in your own little universe that I will completely disregard.
    Warning: Some or all of my posted code may be non-standard and as such should not be used and in no case looked at.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Tricky C
    By ronrardin in forum C Programming
    Replies: 2
    Last Post: 09-29-2010, 02:01 PM
  2. explain me this tricky qs
    By ppanda04 in forum C Programming
    Replies: 9
    Last Post: 09-24-2010, 09:19 AM
  3. tricky line of code
    By dayalsoap in forum C Programming
    Replies: 5
    Last Post: 09-10-2010, 03:29 PM
  4. Tricky C Question
    By ganesh bala in forum C Programming
    Replies: 11
    Last Post: 01-28-2009, 12:58 PM
  5. Tricky const ;)
    By Carlos in forum C++ Programming
    Replies: 10
    Last Post: 01-11-2002, 12:35 PM

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