c语言中<stdbool.h>的使用
2013-02-03 21:46
363 查看
(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语言中<stdbool.h>的使用 分类: H_HISTORY 2013-02-03 21:46 1416人阅读 评论(0) 收藏
- C语言之加入头文件<stdbool.h>可以使用true和false
- c语言中<stdbool.h>的使用
- c语言中<stdbool.h>的使用
- c语言中<stdbool.h>的使用
- c语言中&lt;stdbool.h&gt;的使用
- 为什么不建议使用vector<bool>
- 《C++ Concurrency in Action》笔记23 std::atomic<bool>
- EF学习和使用(八)你必须知道的效率问题根源之Expression<Func<TObject, bool>>与Func<TObject, bool>
- 避免使用vector<bool>
- #include<cstdlib> #include<cstdio> #include<cstring> using namespace std; int w[25][25][25]; bool vi
- 条款18:避免使用vector<bool>(转)
- C语言在中<math.h>中sqrt()函数的使用
- warning C4251: “std::vector<_Ty>”需要有 dll 接口由 class“Test”的客户端使用错误
- Error 42 error C2784: 'bool std::operator <(const std::_Tree<_Traits>
- error C2079: “out”使用未定义的 class“std::basic_ofstream<_Elem,_Traits>”
- 避免使用vector<bool>
- std::vector<bool>中的坑
- 条款18:避免使用vector<bool>
- 实战c++中的vector系列--使用sort算法对vector进行排序(对vector<string>排序、使用稳定的排序std::stable_sort())