String Behavior in C

I am reading course C (this is Dutch, so you probably donโ€™t know), and there is a little exercise to understand the behavior of the strings. So I created a small C program to start the exercise, but the very first output of my program (for me) is amazing.

Source of my C program:

#include <string.h> #include <stdio.h> void printString(char *string) { printf("0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19\n"); printf("%c ",string[0]); printf("%c ",string[1]); printf("%c ",string[2]); printf("%c ",string[3]); printf("%c ",string[4]); printf("%c ",string[5]); printf("%c ",string[6]); printf("%c ",string[7]); printf("%c ",string[8]); printf("%c ",string[9]); printf("%c ",string[10]); printf("%c ",string[11]); printf("%c ",string[12]); printf("%c ",string[13]); printf("%c ",string[14]); printf("%c ",string[15]); printf("%c ",string[16]); printf("%d ",string[17]); printf("%d ",string[18]); printf("%d\n",string[19]); } void main(){ char str[20]; strcpy(str,"Dag grootmoeder!"); printString(str); } 

I compiled gcc (without special switches) and ran the program several times: (For English-speaking people Dag grootmoeder! == Hi grandma! )

 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 D aggrootmoeder ! 94 -90 111 $./oefString 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 D aggrootmoeder ! 51 -12 96 $./oefString 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 D aggrootmoeder ! -17 -117 28 $./oefString 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 D aggrootmoeder ! 96 15 -28 $./oefString 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 D aggrootmoeder ! -20 -46 -18 $./oefString 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 D aggrootmoeder ! 68 -75 58 

Here is the question:

1) Why am I getting garbage values โ€‹โ€‹from the last 3 index of str? At first, I also had printf() with their %c and noticed that the characters were changed, so I used %d after that to display the integer values.

2) Why do these values โ€‹โ€‹change? I do nothing by copying the same line using strcpy() in str.

Thanks for the time spent reading and even more thanx for those in charge!

Jorn

+4
source share
3 answers

You just save memory at the end of the line . You did not fill it out, so you can find something there - so that every time you start, you find something else.

In C, "string" is actually 0 terminated arrays (or pointers to memory). Therefore, if you type characters using "%d" , you will notice the very last element immediately after ! , 0 .

If you want it to be predictable, you can initialize your string before using it:

 memset(str, 0, sizeof(str)); 

or

 char str[20] = {0,}; 

As a side note, it is int main , not void main .

+7
source
 char str[20]; ... strcpy(str,"Dag grootmoeder!"); 

str starts with random values โ€‹โ€‹(regardless of the last time RAM was used) Now you copy a 16-byte string into it, leaving the last 3 characters on the initial (random) values, so they are printed

0
source

Objects defined in a block area without any storage class specifier (for example, your str array object) have an automatic storage duration.

Automated objects that are not explicitly initialized have an undefined value. Therefore, after declaring the str object, all elements of the array have an undefined value. Copying the line "Dag grootmoeder!" in the array, you make the first 17 (line length + tail of the null character), the elements have the specified value. This leaves the last 3 elements of your array with an undefined value.

C says the indefinite value is either indefinite or a trap representation. C also says that reading an undefined value is undefined behavior, since normally with undefined behavior everything can happen when you do this, for example, when printing garbage.

0
source

Source: https://habr.com/ru/post/1388457/


All Articles