c语言中<stdbool.h>的使用
2015-06-17 10:22
447 查看
(1)使用了<stdbool.h>后,可使用true和false来表示真假。
(2)在循环语句中进行变量声明是C99中才有的,因此编译时显式指明 gcc -std=c99 prime.c
[lujinhong@lujinhong chapter9]$ gcc prime.c
prime.c: In function ‘isPrime’:
prime.c:23: error: ‘for’ loop initial declarations are only allowed in C99 mode
prime.c:23: note: use option -std=c99 or -std=gnu99 to compile your code
[lujinhong@lujinhong chapter9]$ gcc -std=c99 prime.c
(2)在循环语句中进行变量声明是C99中才有的,因此编译时显式指明 gcc -std=c99 prime.c
[lujinhong@lujinhong chapter9]$ gcc prime.c
prime.c: In function ‘isPrime’:
prime.c:23: error: ‘for’ loop initial declarations are only allowed in C99 mode
prime.c:23: note: use option -std=c99 or -std=gnu99 to compile your code
[lujinhong@lujinhong chapter9]$ gcc -std=c99 prime.c
/********************************************************** *purpose: * 判断一个整数是否素数。 *method: * 从2开始,至这个整数的平方根,若能整除其中任何一个则非素数并返回。 ***********************************************************/ #include <stdio.h> #include <stdbool.h> bool isPrime(int n); int main(void){ int n; printf("Please enter a digit to test is it a prime or not: "); scanf("%d",&n); if(isPrime(n)) printf("%d is a prime.\n", n); else printf("%d is not a prime.\n", n); return 0; } bool isPrime(int n){ for(int i=2; i*i<n; i++){ if(n%2==0) return false; } return true; }
相关文章推荐
- C++ 常用函数
- c/c++创建删除文件 文件夹
- 使用了非托管的第三方库的c++动态库在c#中的调用
- Effective C++ 条款52
- 使用VS2010在项目中编写C++头文现出"PCH 警告:标头停止点不能位于宏或#if块中"
- 《深入应用C++11:代码优化与工程级应用》
- c语言结构体用法
- C++ 关于滚动条的滚动问题
- 用C/C++开发基于VLC SDK的视频播放器
- 关于C语言中函数调用和参数传递机制的探讨 (转)
- C/C++中赋值语句的返回值问题
- C++实现DES加密解密
- Effective C++ 条款51
- 【转】[C++]实现委托模型
- 2015年oj刷题:实现复数类中的加运算符重载【C++运算符重载】
- c++基础学习(05PM)
- C++学习笔记(二)STL简介(一)
- C++利用IO流对浮点数进行格式化控制输出
- C++头文件预编译与命名空间使用方法
- 【学习笔记】【C语言】进制