列印字元串:%s--------printf(“%s”,”abcd”); 列印字元串數組中某一位:%c--------printf(“%c”,”abcd[2]”); 字元的定義及列印格式: struct { int amount; char *name; }coins[] = { {1,"one"}... ...
- 列印字元串:%s--------printf(“%s”,”abcd”);
- 列印字元串數組中某一位:%c--------printf(“%c”,”abcd[2]”);
- 字元的定義及列印格式:
struct { int amount; char *name; }coins[] = { {1,"one"}, {2,"two"}, {3,"three"} }; char *abc="zxcvbnm"; printf("%s\n",coins[2].name); printf("%s",abc);
- 字元串數組初始化;
char s1[]="array"; //字元數組 char s2[6]="array"; //數組長度=字元串長度+1,因為字元串末尾會自動添‘\0‘ printf("%s,%c\n",s1,s2[2]); //array,r
- 字元串指針初始化;
char *sp="pointer"; //字元指針 printf("%s,%c,%c\n",sp,*sp,*(sp+2)); //pointer,p,i
- 數組+scanf
char s1[6]; scanf("%s\n",s1); printf("%s,%c\n",s1,s1[2]); //array,r
- 指針+數組+scanf
char str[60], *sp=str;//將數組的首地址str賦給指針變數sp scanf("%s\n",sp); printf("%s,%c,%c\n",sp,*sp,*(sp+3));//pointer,p,n printf("%s,%c,%c\n",str,str[0],str[3]);//pointer,p,n
- 使用scanf對結構體進行賦值;
#include<stdio.h> struct student { int num; char name[10]; }; void main() { struct student s; printf("輸入學號:\n"); scanf("%d",&s.num);//scanf賦值給結構體的num成員 printf("輸入姓名:\n"); scanf("%s",&s.name); printf("輸入的信息為:學號:%d,姓名:%s\n",s.num ,s.name); }
- 輸入數據語句的格式:scanf(“%d”,&x);
- char*:字元指針類型