Originally Posted by
Ferris
Perhaps my use of puts is wrong, however, changing the code to printf still outputs 'his is a test' given this code, instead of outputting a single character:
What did you expect it to print?
I suggest that you compile and run this program:
Code:
#include <stdio.h>
int main(void)
{
const char *text = "hello world!";
puts(text);
puts(text + 6);
puts(&text[6]);
return 0;
}
I expect the output to be:
Code:
hello world!
world!
world!
The reason is that puts(text + 6) prints the substring starting from the character at index 6, i.e., the substring "world!". &text[6] yields the same address as text + 6, hence puts(&text[6]) prints the same substring.
What you did with printf is to use the string (or substring starting from index 1) as a format string. By right, you should have used:
Code:
printf("%s", &buffer[1]);
But if you really want to print a single character, then write:
Code:
printf("%c", buffer[1]);
All the confusion that you have right now is due to the fact that whenever you claimed that you printed a single character, you didn't. You printed a string, or a substring thereof, or simply attempted to print a character as a string.