user494216
user494216

Reputation: 530

Odd String Issue in C

I am writing some code that uses the function gethostbyname(). This function requires that I pass it a string of the host I am trying to find the host for. Right now I have my string declared in an array of characters, with a null byte at the end so that it is considered a string.

When I do a printf like this:printf("\n%s\n",hostName); the code will print correctly and say something like: facebook.com

However when I try to print the string like this: printf("\n%sX\n",hostName); the output will be Xacebook.com for some reason.

Does anyone know why the X would overwrite the first character of my string? I would think that it should print like "facebook.comX".

Upvotes: 4

Views: 130

Answers (1)

Laurion Burchall
Laurion Burchall

Reputation: 2853

You have a \r at the end of the string. That moves the cursor back to the start of the line. I'm guessing that you are reading in the hostname from a file?

Upvotes: 4

Related Questions