0

How we use a string name in C to print a string without dereferencing it?

Like here:

char str[];
printf("%s",str);

But in case of arrays we use dereferencing by square brackets[] to print an array:

int a[10];
for(i=0;i<10;i++);
   printf("%d",a[i]);

What's the difference between printing a string and an array? We need dereferencing an array by using square brackets [] but in case of string without using any dereferencing printf(); is just printing the value of the string and not the address.

Jabberwocky
  • 48,281
  • 17
  • 65
  • 115
  • 4
    *But in case of arrays we use dereferencing by square brackets[] to print an array* - no you don't. `printf("%d",a[i]);` prints a single element, not the entire array. That's why you are looping the array – UnholySheep Apr 14 '21 at 08:32
  • 1
    These two examples are missleading, because they don't show the same szenario. – Devolus Apr 14 '21 at 08:33
  • https://stackoverflow.com/questions/1461432/what-is-array-to-pointer-decay + the "%s" format expects a pointer to a string –  Apr 14 '21 at 08:36
  • For the first case, try `printf("%d", str[0]);`, and you'll see that you get the ASCII value of the first character in `str`. That's a situation that displays the same scenario as the second case, not your first example. – mediocrevegetable1 Apr 14 '21 at 08:40

3 Answers3

1

Because that is what the interface to printf dictates.

  • In case of a "%s" it expects a pointer to a NUL terminated string.
  • In case of a "%d" it expects an int.
Jabberwocky
  • 48,281
  • 17
  • 65
  • 115
koder
  • 2,038
  • 7
  • 10
0

It all depends on what the function (printf or else) expects.

When you use printf with %s, like

printf("%s",str);

printf() wants "a string" which, in C, is the same as "an array of [NUL-terminated] char". So, you pass the only thing that identifies actually the whole array: its name (which decays to the address of the first element).

The same applies to every function that expects an array as argument.

If, to the function you call, you must pass something else, you use different approaches. When printf is used to print an integer, like

printf("%d",a[i]);

you must pass an integer; and an array is not an integer, so you must dereference (or "select") an integer from the array, using the correct notation/mechanism. But things are not different if you want to use a member of a struct, for example. You can not pass printf() a struct, so you use the dot to select a [correct] member of the struct. And the same applies to pointers to integer, you dereference them when you want the value and not the pointer itself. And so on for functions... like

printf("%d", (int) floor(PI));

In the above line, again, if printf wants an integer, you must give it an integer, so to use PI (3.14) you must convert it to an int.

0

For starters for example such a code snippet

char str[5] = "Hello";
printf("%s",str);

can result in undefined behavior.

The format string "%s" expects that the argument is a pointer to first character of a string.

In C the string is defined as a sequence of characters terminated by the zero character '\0'. That is a character array that contains a string has the sentinel value '\0'.

So in fact such a call of printf

printf("%s",str);

logically is similar to the following code

while ( *str != '\0' ) putchar( *str++ );

In the example above

 char str[5] = "Hello";

the character array str does not contain a string because its five elements were initialized by characters of the string literal "Hello" excluding its terminating zero character (there is no enough space in the array to accommodate the sixth character '\0' of the string literal).

So such a call

printf("%s",str);

for the array declared above will try to output anything that follows the array in the memory.

To avoid the undefined behavior you should declare the array for example like

 char str[6] = "Hello";

or like

 char str[] = "Hello";

As for arrays of other fundamental types then in most cases they do not have a standard sentinel value. Opposite to strings it is impossible to define such a sentinel value for example for integer or float arrays.

So character arrays that contains strings are exclusions from the total kinds of arrays. Strings always end with the terminating zero character '\0' by the definition of the string in C. That is not valid for other kinds of arrays.

Hence to output any array of other kinds you in general need to use a loop knowing how many elements you need to output.

Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335