C語言基礎文法

來源:互聯網
上載者:User

標籤:函數   amp   view   ase   動態數組   不同的   bre   語言   open   

#include <stdio.h>int main(){    int age;    printf("input your age");    scanf("%d",&age);    if(age>22)    {        printf("you have become old");                    }    else if(age < 18)    {                printf("you are too young");            }    else    {                printf("you are strong");            }        }
View Code

判斷語句:

if()

{

}

else if()

{

}

else

{

}

switch case 語句

#include <stdio.h>int main(){    int grade;    scanf("%d",&grade);    grade/=10;    switch(grade)    {          case 10:        case 9:            printf("A");            break;        case 8:            printf("B");            break;        case 7:            printf("C");            break;        case 6:            printf("D");            break;    }}
View Code

迴圈while

#include <stdio.h>int main(){    int n=0;    int sum=0;    while(n<=100)    {        sum+=n;        n++;            }    printf("%d",sum);    return 0;}

for迴圈

#include <stdio.h>int main(){    int n;    int sum = 0;    for(n=1;n<=100;n++)    {        sum+=n;    }    printf("%d",sum);    return 0;}
View Code

 do while迴圈

int main(){    int i = 0,sum = 0;    int n;    scanf("%d",&n);    do{        sum+=i;        i++;                    }while(i<=n);    printf("%d",sum);    return 0;}
View Code

while語句是先檢票後上車,do while語句是先上車後檢票,dowhile語句至少會被執行一次。

 

 

 

函數定義調用

#include <stdio.h>int getmax(int x,int y){    int result;    return x>y ? x:y;    }int main(){    int x,y,s;    scanf("%d %d",&x,&y);     s=getmax(x,y);    printf("%d",s);        return 0;}

C語言的函數都是從main函數開始執行的

函數定義時的參數稱為形式參數,(parameter)

函數調用時的參數稱為實際參數,(argument)

 指標

#include <stdio.h>void swap(int *x,int *y);int main(){    int a=4;    int b=1;    swap(&a,&b);    printf("a=%d,b=%d",a,b);    return 0;}void swap(int *x,int *y){    int temp;    temp=*x;    *x=*y;    *y=temp;}
View Code

指標的概念

指標是一種資料類型

指標類型的變數稱為指標變數

指標不是一個地址,指標變數的值是一個地址。

想讓指標變數指向哪個儲存單元,就讓其儲存哪個單元的地址。

  • 儲存一個變數的地址
  • 儲存一個數組的首地址
  • 儲存一個字串的首地址
  • 儲存一個函數的首地址

使用指標變數的基本原則

明確指標指向了哪裡--指標初始化

明確指標指向單元的內容是什嗎?---基底類型

只能指向同一基底類型的資料。

指標的重要應用

作函數參數,向函數傳遞變數或函數地址

動態分配記憶體,實現動態數組和動態資料結構

指向函數的指標做函數參數。

被調函數根據傳入的不同地址調用不同的函數。

 

C語言基礎文法

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.