C是否有布尔值(True / False)

时间:2013-04-27 19:09:18

标签: c boolean

这样的事情:

/*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做数学的东西,所以任何下降都会有所帮助。例如,我想知道我是否可以#define Pi 作为数字或任何具有该性质的常量,然后在True / False语句中使用它。任何帮助将不胜感激。

4 个答案:

答案 0 :(得分:2)

0相当于FALSE。其他任何东西都是正确的。

答案 1 :(得分:2)

是,但您必须包含头文件才能使用bool数据类型

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

答案 2 :(得分:0)

enum boolean {false = 0, true};

也许你试着说:

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

然后是......,你可以这样做

答案 3 :(得分: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。