欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

C语言学习记录_2019.02.09

程序员文章站 2022-05-27 22:01:49
打印字符串:%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*:字符指针类型