30

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);
}
Aquarius_Girl
  • 21,790
  • 65
  • 230
  • 411

4 Answers4

54

The code posted is incorrect: a_static and b_static should be defined as arrays.

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', '\0' };
        char b_static[] = { 'a', 's', 'd', 'f', '\0' };
    
        printf("value of a_static: %s\n", a_static);
        printf("value of b_static: %s\n", b_static);
        return 0;
    }
    
  • Alternately, printf can print the contents of an array that is not null terminated using the precision field:

    #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;
    }
    

    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 int argument before the char pointer.

chqrlie
  • 131,814
  • 10
  • 121
  • 189
1

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

char a_static = {'q', 'w', 'e', 'r'};

a_static should be char array to hold multiple characters. make it like

 char a_static[] = {'q', 'w', 'e', 'r','\0'}; /* add null terminator at end of array */

Similarly for b_static

char b_static[] = {'a', 's', 'd', 'f','\0'};
Achal
  • 11,821
  • 2
  • 15
  • 37
1

You need to use array instead of declaring

a_static
b_static

as variables

So it look like this:

int main()
{
  char a_static[] = {'q', 'w', 'e', 'r','\0'};
  char b_static[] = {'a', 's', 'd', 'f','\0'};
  printf("a_static=%s,b_static=%s",a_static,b_static);
  return 0;
}
Karthik Vg
  • 108
  • 13
0

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:

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);

The output should be:

mystring is "alien"

Back to your code, it should look like:

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
}

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

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

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

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

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

Hope that you learned something from this answer.

NotMe
  • 97
  • 7
  • That's problematic and partially wrong in various places. For a start: pointers are **never** a replacement for arrays. If one feels like answering questions witrh broken code and lack of fundamentals like that, they at least should get it very correct so OP will not need to buy a textbook to learn the language properly which would be the correct way. – too honest for this site May 13 '18 at 12:56