Linux c編程執行個體_例子

來源:互聯網
上載者:User

標籤:style   blog   io   color   ar   sp   for   div   log   

例一:字元與整型變數的實現

#include <stdio.h>int main(){    int c1,c2;    char c3;    c1=‘a‘-‘A‘;    c2=‘b‘-‘B‘;    c3=‘c‘-32;    printf("c1 is %d and c2 is %d\n",c1,c2);     printf("c3 is %d and %c\n",c3,c3);                   //字元在記憶體中是以ASCII碼存在的, a就是65等等                  //字元型變數可以與整型變數進行運算         }

 

結果是: 

32 32

67 C

 

字串常量是 “”裡面的

字元常量是 ‘’裡面的

例二:

#include "stdio.h"int main(){  char a,b; a=97; b=98; printf("%c%c\n",a,b);  printf("%d%d",a,b); } 


ab

9798

-------------------------------

為什麼字串常量末尾要加一個‘\o’,因為字串是以ASCII儲存的,要有一個結束的標誌位。不讓很難判斷字串在記憶體中佔據多少空間。

 

例三:指標數組與二級指標【linux c 編程第84頁】

#include "stdio.h"int main(){    int a[5]={1,3,5,7,9 };  int *p[5],i;  int **pp=p;// 相當於 int a=12;int *b=&a;int **c=&b; 最後一個的這裡     for(i=0;i<5;i++)  p[i]=&a[i];    for(i=0;i<5;i++)  printf("%d\n",*p[i]);    for(i=0;i<5;i++,pp++)  printf("%d",**pp);}

 例四:指標和數組的關係。經典例子

#include "stdio.h"int main(){  //經典例子 linux c程式 第85頁   int a[2][5]={1,3,5,7,9,2,4,6,8,10};  int (*p)[5],i; // int (*p)[5] 表示p是一個指標,指向含有5個元素的一維指標,並且p是一維數組 的首地址   p=a;  for(i=0;i<5;i++)   printf("%d ",(*p)[i]);   printf("\n");      p++;  //p加1,指向二維數組a的第二行    for(i=0;i<5;i++)    printf("%d ",(*p)[i]);       printf("\n");   return 0; }

 

Linux 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.