zoukankan      html  css  js  c++  java
  • C语言中的字符数组

    周末赶数据结构的作业,整理的一些关于C语言中字符数组的困惑与解答。
    1.赋值
    C语言中,给字符数组char s[]赋值使用strcpy (在string.h头文件中)
    1. #include <stdio.h>
    2. #include <string.h>
    3. int main()
    4. {
    5. char test[3][4];
    6. strcpy(test[0], "abc");
    7. strcpy(test[1], "adf");
    8. strcpy(test[2], "dsg");
    9. for (int i = 0; i < 3; i++)
    10. printf("%s ", test[i]);
    11. return 0;
    12. }
    2.长度 strlen  (string.h)
    unsigned int string(char *str) 得到的长度不包括字符串数组后面结尾的''

    3.指向字符串数组的指针
    char str[] = "Hello World!";
    char *str = "Hello World!";
    上下两个是一样的意思。

    4.输入/输出字符串
    输入:gets / scanf (stdio.h)
    字符串输入结束后,都会自动加''
    1. #include <stdio.h>
    2. #include <string.h>
    3. int main()
    4. {
    5. char str[30];
    6. char str2[30];
    7. gets(str); // 遇到回车认为输入结束
    8. printf("%s ", str);
    9. scanf("%s", &str2); // 遇到空格认为输入结束
    10. printf("%s ", str2);
    11. return 0;
    12. }

    输出:puts / printf (stdio.h)
    区别在于:puts只能单个地输出字符串且自动换行;而printf可以格式化输出多个字符串,但需要加' '换行。
    1. #include <stdio.h>
    2. #include <string.h>
    3. int main()
    4. {
    5. char str[30] = "Hello World!";
    6. printf("%s ", str);
    7. puts(str);
    8. puts("end");
    9. }







  • 相关阅读:
    以太坊客户端Geth命令用法
    ubuntu 下载地址
    以太坊(二)安装Solidity编译器
    git 查看&修改用户名
    以太坊(一)
    centos7 时间设置
    centos7 桥接配置
    nginx http转 https
    centOS7 安装mysql5.7
    webpack--关于babel的配置
  • 原文地址:https://www.cnblogs.com/cnblogsnearby/p/4560369.html
Copyright © 2011-2022 走看看