0

Well, I've a weird problem with printf(). It's outputting garbage on the screen. It's kind of connected with memory, I guess. Have a look:

char string1[] = "SAMPLE STRING";
char string2[20]; // Some garbage in it

/* Let's clear this madness*/
int i = 0;
for (i; i < 20; i++) string2[i] = ' ';   // Space, why not?

printf("output: %s", string2);

Output

output:      ╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠SAMPLE STRING
// Ten spaces and random characters, why?
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
krzakov
  • 3,871
  • 11
  • 37
  • 52
  • Are you sure it's not *20* spaces and then random characters after that? – Alok Singhal Nov 26 '12 at 15:34
  • ╠ is 0xCC in codepage 437, and [MSVC fills 0xCC to uninitialized memory to help debugging](https://stackoverflow.com/q/370195/995714). That means you've accessed uninitialized memory. You can find tons of questions about ╠ and 0xCC here on SO – phuclv Aug 18 '18 at 10:54

3 Answers3

8

Because C strings need to be NUL terminated. This means the last char of your string must be '\0'. This is how printf (and all other C string functions) know when a string is finished.

Alfonso
  • 8,386
  • 1
  • 43
  • 63
2

Finish your string2 with a null character, '\0':

string2[19] = '\0';

Or you can do it in this way:

for (i; i < 19; i++)
    string2[i] = ' ';
string2[i] = '\0'; // After the end of the loop, i=19 here
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
MOHAMED
  • 41,599
  • 58
  • 163
  • 268
0
#include <stdio.h>

int main() {
    char string1[] = "SAMPLE STRING";

    // Using curly braces {} for initialization
    char string2[20] = {}; // Initialize all elements to 0 (null)

    printf("Using curly braces initialization:\n");
    printf("output: %s\n\n", string2);

    // Using designated initializer for spaces and null-terminator
    char string3[20] = {[0 ... 18] = ' '}; // Initialize elements 0 to 18 with spaces

    printf("Using designated initializer for spaces:\n");
    printf("output: %s\n\n", string3);

    return 0;
}
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
  • 2
    The empty braces intitializer `{}` has been illegal in standard C until the upcoming C23 where it will finally be in the language (some compilers support it, but it is not portable). Using ranges, e.g., `[0 ... 18]` is not legal in standard C either; this is a Gnu C extension. – ad absurdum Aug 17 '23 at 18:10
  • 1
    An explanation would be in order. E.g., what is the idea/gist? From [the Help Center](https://stackoverflow.com/help/promotion): *"...always explain why the solution you're presenting is appropriate and how it works"*. Please respond by [editing (changing) your answer](https://stackoverflow.com/posts/76923766/edit), not here in comments (and *** *** *** *** *** *** *** *** *** *** *** ***[without](https://meta.stackexchange.com/a/131011)*** *** *** *** *** *** *** *** *** *** *** *** *** *** *** "Edit:", "Update:", or similar - the answer should appear as if it was written today). – Peter Mortensen Aug 17 '23 at 18:15
  • Information about what compiler (incl. version and compiler options) in what environment was used to test this could also be useful. – Peter Mortensen Aug 17 '23 at 18:17
  • Has the code been generated by some tool? Which one? – Peter Mortensen Aug 17 '23 at 18:21
  • As it’s currently written, your answer is unclear. Please [edit] to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Aug 18 '23 at 09:01