zoukankan      html  css  js  c++  java
  • c语言中<stdbool.h>的使用 分类: H_HISTORY 20130203 21:46 1416人阅读 评论(0) 收藏

    (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



    /**********************************************************
    *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;
    }
    

    版权声明:本文为博主原创文章,未经博主允许不得转载。

  • 相关阅读:
    Redis 的 5 个常见使用场景
    当别人给你一个wsdl或者webservice接口时
    Java事务
    Java分布式锁的三种实现方案(redis)
    使用Redis数据库(String类型)
    超详细Redis数据库入门教程
    java对redis的基本操作(初识)
    String、StringBuffer与StringBuilder之间区别
    java正则表达式替换空格和换行符
    Linux 批量管理工具
  • 原文地址:https://www.cnblogs.com/lujinhong2/p/4637448.html
Copyright © 2011-2022 走看看