ios學習Day3

來源:互聯網
上載者:User

標籤:style   blog   color   使用   os   資料   

bool 資料類型  

#define TRUE 1//#define FALAE 0#define BOOL intBool flag=1;

 

bool型 實質上是 int型

c89沒有提供

c99 有提供

賦值時,應該給true或 false。

如果給一個非零值 則儲存為1;

bool flag =tuue;
#include <stdio.h>
int main(int argc, const char * argv[])
{
       bool  y=1;
    
              {
           printf("%s",y?"true":"flase");      }//三元運算子
   
       }

 c語言的靈活性會帶來程式的隱患,在某些現代進階語言中不允許這樣寫 1 if(i=9) 2 { 3 } MSAN發射火箭!

邏輯運算子

&&邏輯與: 兩便同時為真;

#include <stdio.h>
int main(int argc, const char * argv[])
{
       if(i>=0&&i<=100)
       {
       
       }

}

短路所謂短路現象就是使用邏輯運算子時候,當前面的邏輯運算結果已經能夠決定整個運算條件的真假時就停止運算,後面的邏輯式都沒有進行運算。
如:a==0 && b==0 && c==0;當a==0這個運算式為假時,後面的b==0 && c==0都不用算了,因為a==0為假就已經可以決定了整個邏輯條件為假。
如果要使用位元運算符來代替邏輯運算子,如下:
a==0 & b==0 & c==0;這其實是幾個bool類型資料進行位與操作,相當於1 & 1 & 1(這是全部邏輯運算式為真的情況),結果也是為1,當作為if條件時候,其實也和上面的沒有區別。
使用邏輯運算子或位操作運算子來作為if的條件時候,並沒有多大區別,主要是位操作符需要把每個邏輯式都運算,而邏輯運算就會出現短路現象,這可以說是加快了運算速度。

int  a=3,b=5;A||B++;b=5//

 

 if語句

三種形式 if()  {  語句  } 

#include <stdio.h>int main(int argc, const char * argv[])

{ char str; printf("請輸入:m或f\n"); scanf("%s",&str); if (str==‘m‘) { printf("\n男性"); } else { printf("\n女性"); }}
//
#include <stdio.h>    int main(int argc, const char * argv[])    {        char str;        printf("請輸入:m或f\n");        scanf("%s",&str);        if (str==‘m‘)        {            printf("\n男性");                    }        else if (str==‘f‘)        {            printf("\n女性");        }        else        {            printf("\n未知性別");        }            }

 輸入一個年份,判斷是否閏年


#include <stdio.h>    int main(int argc, const char * argv[])    {        int year;        printf("請輸入年份");        scanf("%d",&year);        if((year%4==0&&year%100!=0)||year%400==0)        {            printf("是閏年");        }        else{            printf("不是閏年");        }        }

 鍵入一個字元 判斷是字母還是

#include <stdio.h>    int main(int argc, const char * argv[])    {        printf("請輸入字元");        char c;        scanf("%c",&c);        if(c<32)        {            printf("輸入的控制字元");        }              else if(c>‘0‘&&c<‘9‘)            {                printf("輸入的是數字");            }                 else if(c>‘a‘&&c<‘z‘)            {                printf("輸入的是小寫");            }            else if(c>‘A‘&&c<‘Z‘)            {                printf("輸入的是大寫");            }             else            {                printf("輸入的是其他");            }    }

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.