2

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.

0

2 Answers 2

3

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';
Sign up to request clarification or add additional context in comments.

Comments

1

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.