-3

I'm learning C programming and I'm having some issues to print a name that i store in a char array.

char nameArr[125];
for (int i = 0; i < 125; i++)
{
    if (nameArr[i] != "\0")
    {
        printf(nameArr[i]);
    }
    else
    {
        i = 125;
    }
}

This is my code in which I try to print out a name like "Joe Doe" that I already stored in the char array, but I get some errors in the compiler when I run this. If I'm not suppose to do like this, how can I print out just the name and not all the 125 slots of the array?

Vlok
  • 63
  • 2
  • 12
  • `printf(nameArr[i]` do see something amiss here? – babon Nov 20 '17 at 08:09
  • What's the colon (`:`) doing in the for loop header? –  Nov 20 '17 at 08:10
  • Opps yes, I see it. I will edit it – Vlok Nov 20 '17 at 08:10
  • Inquiring about basic syntax rules always help. – sjsam Nov 20 '17 at 08:10
  • @FelixPalmen fixed it. I just did a typo – Vlok Nov 20 '17 at 08:11
  • 1
    And now you have a serious bug, you treat `nameArr[i]` as the *format string* for `printf()`. –  Nov 20 '17 at 08:11
  • printf is a bit strange. it's first argument is a string with formatting and placeholders for the data you want to print, subsequent arguments give the data to fill the placeholders. try `printf("%c", nameArr[i]);`. You can also put a `printf("\n");` after the loop just to start a new line after your output. – lockcmpxchg8b Nov 20 '17 at 08:12
  • @FelixPalmen how am I suppose to get the char for the array then and print it if I can't do it like that? – Vlok Nov 20 '17 at 08:12
  • Is `nameArr` a *null-terminated byte string*? I.e. does it contain a proper string? Then just print it using the `"%s"` format: `printf("%s\n", nameArr)`. No need for the loop. – Some programmer dude Nov 20 '17 at 08:14
  • @lockcmpxchg8b ah okay. I remember now how it works in C – Vlok Nov 20 '17 at 08:14
  • @Someprogrammerdude does that mean that he printf understands where the string ends? I have checked inside the array and I can se that it contains both the "\n" and a "\0" char and after that its just null. – Vlok Nov 20 '17 at 08:16
  • As for the errors you get, when asking a question about build errors, *include the actual errors in the question!*. Copy them, as text and in full and complete, then paste it into the question body. A hint though: `nameArr[i]` is a single character, or type `char`. `"\0"` is a *string* of type `char [2]` (but will decay to `char *` for the comparison). You can't really compare a `char` with a `char *`. – Some programmer dude Nov 20 '17 at 08:16
  • 3
    It seems you could need [a couple of good beginners books](http://stackoverflow.com/questions/562303/the-definitive-c-book-guide-and-list). – Some programmer dude Nov 20 '17 at 08:17
  • 1
    instead of this articial `i = 125;` just use *`break`* – Antti Haapala -- Слава Україні Nov 20 '17 at 08:37
  • @AnttiHaapala or better use the actually correct loop condition (byte at current position is non-`0`). –  Nov 20 '17 at 08:48
  • Copy/paste the code you actually tested. Anything else, like manually transcribing it directly from a textbook or homework, is a waste of everybody's time. – Martin James Nov 20 '17 at 10:28

1 Answers1

1

Assuming your nameArr already contains a string, which is defined as a sequence of characters ending with 0, the obvious solution is to do

printf("%s", nameArr);

or

puts(nameArr); // appends newline automatically

If your question is how you would do this by hand, it would look something like this:

for (size_t i = 0; nameArr[i]; ++i)
{
    putchar(nameArr[i]);
    // or printf("%c", nameArr[i]);
}

nameArr[i] evaluates as true as long as this isn't a 0 byte. Also, always use size_t for array indices. int is not guaranteed to hold any size an object in C can have.