2013-04-27 61 views
-1

事情是這樣的:確實c具有布爾(真/假)

/*Simple program to calculate the circumference of a circle. */ 

#include <stdio.h> 
#define PI 3.14159 

int main() 

{ 

    float r1 /*R1 being the radius.*/ 
    /* Since the Circumference is R * PI * 2, or R2 * PI */ 
    /* we do the following */ 

    printf("This is a program that calculates the Circumference\n"); 
    printf("Of a circle. Please enter your radius.\n"); 

    scanf("%f", r1"\n"); /*This being the first number.*/ 
    printf("Your radius times PI times 2 is\n"); 
    /*Now it calculates the circumference. */ 

    printf("%f", (r1 * PI * 2)"\n"); 

} 

我也用C做數學的東西,等等,任何內幕會有所幫助。例如,我想知道我是否可以將#definePi作爲一個數字或任何該性質的常量,然後在True/False語句中使用它。任何幫助,將不勝感激。

+0

「的#define PI多項[...],並把它作爲一個TRUE/FALSE陳述」 - 1.不一個聲明,一個表達。 2.什麼是「布爾函數」? 3.你如何使用PI作爲真/假? – 2013-04-27 19:16:56

+0

我想我錯了,不是布爾函數,而只是一個布爾值,所以將PI定義爲一個數字,然後在那之後有一個True/False?我很抱歉問這個問題不對。 – 2013-04-27 19:19:25

+0

Pi已經在''中定義爲'M_PI',請不要定義它。另外,你是指「在Pi之後有真/假」是什麼意思? – 2013-04-27 19:20:19

回答

0

有一個在C.你的最新版本的布爾數據類型可以使用枚舉來創建它們。但我認爲你的意思是這樣的

int v=42; 
if(v) printf("hi there\n"); 
v=0; 
if(v) printf("hi there\n"); 
if(! v) printf("hi there\n"); 
v=42; 
if(! v) printf("hi there\n"); 

這適用於整數數據類型。在示例中 - 如果v == 0,則(!v)返回true並且(v)返回false。當v不爲零(包括負數)時,那麼! v)返回false並且(v)返回true。

+0

謝謝,這就是我想要得到的,有時我的問題會變得更廣泛。所以我可以使用if語句來確保它是True/False? – 2013-04-27 19:24:06

+0

PI不是真的或假的,不是概念上的,如果你說嘿,PI!= 0所以PI是真的,沒有意義 – DGomez 2013-04-27 19:27:26

+0

Aaah我對那個問題有疑問。謝謝@DGomez – 2013-04-27 19:29:16

2

0等於FALSE。其他任何事情都是真的。

0
enum boolean {false = 0, true}; 

也許你試着這樣說:

if(PI*someRadius == someNumber);//... 

然後是......,你能做到這一點

2

是,但你必須包含頭文件使用布爾數據類型

#include <stdbool.h> 
int main() 
{ 
    bool arr[2] = {true, false}; 
    return 0; 
} 
相關問題