7 Replies - 15426 Views - Last Post: 24 February 2010 - 05:32 AM Rate Topic: -----

#1 Guest_Laser Gunz*


Reputation:

Declare true/false/if in C

Posted 23 February 2010 - 11:59 PM

I just began working with C, and am stuck at learning how to declare true/false. I feel as if this may be confusing so I will just give you some examples.

Write an expression that evaluates to true if the integer variable x contains an even value, and false if it contains an odd value.

#include<stdio.h>
intmain()
{
int = x;
if (x=even);
{
printf("\x is true!");
}
else
{
if (x=odd);
printf("\x is false!");
}
}


Another example: Write a literal representing C's false value.

I feel like this is an easy answer, but I have been trying different versions. I say some stuff like

literal bool c = false; - but I believe this in C++ and not C.


Any help in this general topic would be greatly appreciated.

Sorry for the newbish mistakes.

Is This A Good Question/Topic? 0

Replies To: Declare true/false/if in C

#2 Martyn.Rae  Icon User is offline

  • The programming dinosaur
  • member icon

Reputation: 540
  • View blog
  • Posts: 1,406
  • Joined: 22-August 09

Re: Declare true/false/if in C

Posted 24 February 2010 - 12:26 AM

Quote

Sorry for the newbish mistakes


No need to apologise, we were all newbies once upon a time!

You are absolutely correct when you state that the keywords true and false are C++.

Traditionally, in C two definitions were used #define TRUE 1 and #define FALSE 0. You do have to be very careful though because if FALSE is defined as 0, then surely TRUE must be everything else ... so surely -2315 or 34826 are TRUE if 0 is FALSE. Do they represent MAYBE or INDETERMINATE? This is the reason why the C++ standard introduce the bool type. It can only be allocated a value of true or false.

You can achieve this in C by saying

typedef enum { false=0, true } bool;



but I would suggest you only did that if you were compiling with a pure C compiler or a C++ compiler where you had the ability to tell it that it must compile using strict C standards.

Coming back to the code you posted there are several mistakes.

#include<stdio.h> 
int main() 
{ 
    int = x;                   // I think you meant to say int x;
    if (x=even);               // even does not exist in the C language 
                               // also = means assign not equals
                               // == means equals
                               // The semicolon should not be there
                               // The way to test x for even is to check
                               // if the last bit is zero or one
                               // To do this, we say if ( x&1 ) which
                               // effectively clears all of the bits to
                               // zero except the last bit. So we are now
                               // effectively checking if ( x == 0 ). In
                               // other words in English if the expression
                               // x&1 equals 0 then x is an even number.
    if ( x&1 == 0 )
    { 
        printf("\x is true!"); 
    } 
    else 
    { 
        if (x=odd);            // Don't need this because if the last bit
                               // of x is not zero, then it must be odd!
        printf("\x is false!"); 
    } 
}



Hope this helps you.
Was This Post Helpful? 1
  • +
  • -

#3 Guest_Guest*


Reputation:

Re: Declare true/false/if in C

Posted 24 February 2010 - 01:02 AM

Another quick question.


Write an expression that evaluates to true

how do I do those?

Say I want to write that x is greater than y?


I really have no clue what 'true' means in C.
Was This Post Helpful? 1

#4 Martyn.Rae  Icon User is offline

  • The programming dinosaur
  • member icon

Reputation: 540
  • View blog
  • Posts: 1,406
  • Joined: 22-August 09

Re: Declare true/false/if in C

Posted 24 February 2010 - 01:25 AM

View PostGuest, on 24 February 2010 - 07:02 AM, said:

Another quick question.


Write an expression that evaluates to true

how do I do those?

Say I want to write that x is greater than y?


I really have no clue what 'true' means in C.


Taking my code from above:

#include<stdio.h>  
int main()  
{  
    int x = 10;
    int y = 8;
    if ( x > y ) 
    {  
        printf("\x is true!");  
    }  
    else  
    {  
        printf("\x is false!");  
    }  
}



There is the x is greater than y test.

Quote

I really have no clue what 'true' means in C.


The term 'true' is simply that. So, is 10 > 5? The answer is yes, so the expression is true. Do pigs have wings? No of course they don't ... so the answer is false.

Hope that helps.
Was This Post Helpful? 1
  • +
  • -

#5 PlasticineGuy  Icon User is offline

  • mov dword[esp+eax],0
  • member icon

Reputation: 281
  • View blog
  • Posts: 1,436
  • Joined: 03-January 10

Re: Declare true/false/if in C

Posted 24 February 2010 - 02:19 AM

Quote

Do pigs have wings? No of course they don't ... so the answer is false.
Oh really?
Posted Image

No, not really.
Was This Post Helpful? -3
  • +
  • -

#6 Guest_LaserGunz*


Reputation:

Re: Declare true/false/if in C

Posted 24 February 2010 - 02:21 AM

Thanks man, you have been very helpful.

Sorry, but one final question.

How do I go about 'calling' statements?

printTodaysDate is a function that accepts no parameters and returns no value.

Write a statement that calls printTodaysDate .


I know that since no parameters and no return value I put:

void printTodaysDate (void)

how do I call it though?

printTodaysDate = x;?
Was This Post Helpful? 0

#7 PlasticineGuy  Icon User is offline

  • mov dword[esp+eax],0
  • member icon

Reputation: 281
  • View blog
  • Posts: 1,436
  • Joined: 03-January 10

Re: Declare true/false/if in C

Posted 24 February 2010 - 02:23 AM

printTodaysDate();
Was This Post Helpful? 0
  • +
  • -

#8 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6092
  • View blog
  • Posts: 23,612
  • Joined: 23-August 08

Re: Declare true/false/if in C

Posted 24 February 2010 - 05:32 AM

In C, 0 is false, all else is true.
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1