Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C Arbitrary length string

Tags:

arrays

c

string

I have a doubt how the length for an array is allocated

#include <stdio.h>
#include <string.h>

int main()
{
    char str[] = "s";
    long unsigned a = strlen(str);
    scanf("%s", str);
    printf("%s\n%lu\n", str, a);
    return 0;
}

In the above program, I assign the string "s" to a char array. I thought the length of str[] is 1. so we cannot store more than the length of the array. But it behaves differently. If I reading a string using scanf it is stored in str[] without any error. What was the length of the array str?

Sample I/O :

Hello

Hello 1
like image 371
Ubendren96 Avatar asked Dec 08 '22 10:12

Ubendren96


2 Answers

Your str is an array of char initialized with "s", that is, it has size 2 and length 1. The size is one more than the length because a NUL string terminator character (\0) is added at the end.

Your str array can hold at most two char. Trying to write more will cause your program to access memory past the end of the array, which is undefined behavior.

What actually happens though, is that since the str array is stored somewhere in memory (on the stack), and that memory region is far larger than 2 bytes, you are actually able to write past the end without causing a crash. This does not mean that you should. It's still undefined behavior.

Since your array has size 2, it can only hold a string of length 1, plus its terminator. To use scanf() and correctly avoid writing past the end of the array, you can use the field width specifier: a numeric value after the % and before the s, like this:

scanf("%1s", str);
like image 116
Marco Bonelli Avatar answered Dec 09 '22 22:12

Marco Bonelli


When an array is declared without specifying its size when the size is determined by the used initializers.

In this declaration of an array

char str[] = "s";

there is used a string literal as an initializer. A string literal is a sequence of characters terminated by an included zero-terminating character. That is the string literal "s" has two characters { 's', '\0' }.

Its characters are used to initialize sequentially elements of the array str.

So if you will write

printf( "sizeof( str ) = %zu\n", sizeof( str ) );

then the output will be 2. The length of a string is determinate as a number of characters before the terminating zero character. So if you will write

#include <string.h>
//...
printf( "strlen( str ) = %zu\n", strlen( str ) );

then the output will be 1.

If you will try to write data outside an array then you will get undefined behavior because a memory that does not belong to the array will be overwritten. In some cases you can get the expected result. In other cases the program can finish abnormally. That is the behavior of the program is undefined.

like image 44
Vlad from Moscow Avatar answered Dec 09 '22 22:12

Vlad from Moscow