C 入門教程 第2講 數值、字元與字串

來源:互聯網
上載者:User

聲明方式:

資料類型 變數名;

舉例:

// int類型int  i;           /* 普通聲明 */int  j,k;         /* 同時聲明多個 */int  age = 18;    /* 聲明的同時賦值 */int  Alan ,Sam=16;/* 聲明與同時賦值 */// float類型float f;          /* 普通聲明 */float q,money;    /* 同時聲明多個 */float v = 2.0;    /* 聲明的同時賦值 */// char類型char c;               /* 普通聲明 */char zh,text;         /* 同時聲明多個 */char letter = 'A';    /* 聲明的同時賦值 */// char數組(字串)char c[20] = { 'H','e','l','l','o',' ','w','o','r','l','d' };char name[] = { 'A', 'l', 'a', 'n' };

 

逸出字元

在一段字串中,不能直接出現,需要轉義的字元

'\n' 換行

'\t'' 水平製表

'\'' 單引號

'\"' 雙引號

'\\' 反斜線

#include<stdio.h>main(){    printf("Num\tName\n");    printf("001\tAlan\n");    printf("002\tLellansin\n");}

輸出結果:

Num  Name

001   Alan

002   Lellansin

 

printf與scanf

printf列印,scanf為輸入

printf(字串,... );

scanf(字串,... );

 

如:

 

printf("Alan的年齡是%d", age);

scanf("%d", &i);

 

舉例:

#include<stdio.h>main(){    int age;    printf("你的年齡是?\n");    scanf("%d",&age);    printf("你的年齡是%d",age);}

常見格式:

%d   十進位整數

%i    十進位整數

%c   單個字元

%s   字串

%f   浮點數

聯繫我們

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