C語言學習記錄_2019.02.09
阿新 • • 發佈:2019-02-10
student 數據 nbsp left 打印 char* \n right abc
- 打印字符串:%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*:字符指針類型
C語言學習記錄_2019.02.09