2013-04-27 60 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; 
}