C语言 如何通过printf在C中打印字符数组?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/50312194/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 10:44:48  来源:igfitidea点击:

How to print a char array in C through printf?

carrayscharprintf

提问by Aquarius_Girl

This results in segmentation fault. What needs to be corrected?

这会导致分段错误。需要纠正什么?

int main(void)
{
    char a_static = {'q', 'w', 'e', 'r'};
    char b_static = {'a', 's', 'd', 'f'};

    printf("\n value of a_static: %s", a_static);
    printf("\n value of b_static: %s\n", b_static);
}

回答by chqrlie

The code posted is incorrect: a_staticand b_staticshould be defined as arrays.

发布的代码是不正确:a_staticb_static应该被定义为阵列。

There are two ways to correct the code:

有两种方法可以更正代码:

  • you can add null terminators to make these arrays proper C strings:

    #include <stdio.h>
    
    int main(void) {
        char a_static[] = { 'q', 'w', 'e', 'r', '
    #include <stdio.h>
    
    int main(void) {
        char a_static[] = { 'q', 'w', 'e', 'r' };
        char b_static[] = { 'a', 's', 'd', 'f' };
    
        printf("value of a_static: %.4s\n", a_static);
        printf("value of b_static: %.*s\n", (int)sizeof(b_static), b_static);
        return 0;
    }
    
    ' }; char b_static[] = { 'a', 's', 'd', 'f', '
    #include <stdio.h>
    
    int main(void) {
        char a_static[] = { 'q', 'w', 'e', 'r', '
    #include <stdio.h>
    
    int main(void) {
        char a_static[] = { 'q', 'w', 'e', 'r' };
        char b_static[] = { 'a', 's', 'd', 'f' };
    
        printf("value of a_static: %.4s\n", a_static);
        printf("value of b_static: %.*s\n", (int)sizeof(b_static), b_static);
        return 0;
    }
    
    ' }; char b_static[] = { 'a', 's', 'd', 'f', '
    char a_static = {'q', 'w', 'e', 'r'};
    
    ' }; printf("value of a_static: %s\n", a_static); printf("value of b_static: %s\n", b_static); return 0; }
    ' }; printf("value of a_static: %s\n", a_static); printf("value of b_static: %s\n", b_static); return 0; }
  • Alternately, printfcan print the contents of an array that is not null terminated using the precision field:

     char a_static[] = {'q', 'w', 'e', 'r','
    char b_static[] = {'a', 's', 'd', 'f','
    char mystring[6] = { 'a' , 'l', 'i', 'e' , 'n', 0}; //see the last zero? That is what you are missing (that's why C Style String are also named null terminated strings, because they need that zero)
    printf("mystring is \"%s\"",mystring);
    
    '};
    '}; /* add null terminator at end of array */

    The precision given after the .specifies the maximum number of characters to output from the string. It can be given as a decimal number or as *and provided as an intargument before the charpointer.

  • 您可以添加空终止符以使这些数组成为正确的 C 字符串:

    int main(void) 
    {
      char a_static[5] = {'q', 'w', 'e', 'r', 0};
      char b_static[5] = {'a', 's', 'd', 'f', 0}; 
      printf("\n value of a_static: %s", a_static); 
      printf("\n value of b_static: %s\n", b_static); 
      return 0;//return 0 means the program executed correctly
    }
    
  • 或者,printf可以使用精度字段打印非空终止的数组的内容:

    char *string = "my string"; //note: "my string" is a string literal
    

    .指定从字符串输出的最大字符数之后给出的精度。它可以作为十进制数给出,也可以作为指针前的参数*提供。intchar

回答by Achal

This results in segmentation fault. ?because of the below statement

这会导致分段错误。? 因为下面的声明

char mystring[6] = "alien"; //the zero is put by the compiler at the end 

a_staticshould be char arrayto hold multiple characters. make it like

a_static应该是char array容纳多个字符。让它像

a_static
b_static

Similarly for b_static

同样对于 b_static

int main()
{
  char a_static[] = {'q', 'w', 'e', 'r','##代码##'};
  char b_static[] = {'a', 's', 'd', 'f','##代码##'};
  printf("a_static=%s,b_static=%s",a_static,b_static);
  return 0;
}

回答by NotMe

The thing is that you are using C Style Strings, and a C Style String is terminated by a zero. For example if you'd want to print "alien" by using a char array:

问题是您使用的是 C 样式字符串,而 C 样式字符串以零结尾。例如,如果您想使用字符数组打印“alien”:

##代码##

The output should be:

输出应该是:

mystring is "alien"

mystring 是“外星人”

Back to your code, it should look like:

回到你的代码,它应该是这样的:

##代码##

By the way, instead of arrays you can use pointers (if you don't need to modify the string):

顺便说一句,您可以使用指针代替数组(如果您不需要修改字符串):

##代码##

Also you can initialize your char arrays with string literals too:

你也可以用字符串文字初始化你的字符数组:

##代码##

Also: Functions that operate on C Style Strings (e.g. printf, sscanf, strcmp,, strcpy, etc) need zero to know where the string ends

另外:对 C 样式字符串进行操作的函数(例如 printf、sscanf、strcmp、strcpy 等)需要零才能知道字符串在哪里结束

Hope that you learned something from this answer.

希望你从这个答案中学到了一些东西。

回答by Karthik Vg

You need to use array instead of declaring

您需要使用数组而不是声明

##代码##

as variables

作为变量

So it look like this:

所以它看起来像这样:

##代码##