Reputation: 97
I am stuck on the same code for quit some time now. I am trying to fill a char array with characters i read from a text file (ascii). But for some reason when i printf the char array it only displays the letter H.
Code:
void append(char c)
{
//int len = strlen(cStr);
cStr[iCounter] = c;
cStr[iCounter + 1] = '\0';
printf("char c:%c char array%c\n",c,cStr);
}
The char array (cStr) is declared outside this function because i need to acces it from different functions. So is iCounter which is incremented every time it executes this function.
Any help would be appreciated.
Upvotes: 2
Views: 2189
Reputation: 17332
You print one character with %c
use string specifier %s
instead:
printf("char c:%c char array%s\n",c,cStr);
Note: iCounter
is not actually incremented:
cStr[iCounter++] = c;
cStr[iCounter] = '\0';
Upvotes: 3
Reputation: 13661
Use %s
to print string. %c
is use to print unique character.
From printf man page
c
If no l modifier is present, the int argument is converted to an unsigned char, and the resulting character is written. If an l modifier is present, the wint_t (wide character) argument is converted to a multibyte sequence by a call to the wcrtomb(3) function, with a conversion state starting in the initial state, and the resulting multibyte string is written.
Upvotes: 1