Reputation: 44
For example,
#include <stdio.h>
int main (int argc, const char * argv[]) {
char out = printf("teststring");
printf("%d\n", out);
return 0;
}
will return teststring10
. Anyone has an idea how to get rid of the 10?
Thanks in advance.
Upvotes: 1
Views: 252
Reputation:
Lets look at your code:
#include <stdio.h>
int main (int argc, const char * argv[]) {
char out = printf("teststring");
printf("%d\n", out);
return 0;
}
And change it:
#include <stdio.h>
int main (int argc, const char * argv[]) {
const char *str = "teststring";
int out = printf("%s", str);
printf("\nPrintf wrote %d bytes when printing %s\n", out, str);
return 0;
}
Compile / run the edited version and you should immediately see the difference. Modify the length of str
then compile and run it again.
Now, just a tiny change to your original code:
#include <stdio.h>
int main (int argc, const char * argv[]) {
char out = printf("teststring\n");
printf("%d\n", out);
return 0;
}
You can see now, you are printing two different things, and out
has become 11, since a newline was added. Nothing is actually trailing here. Then change %d
to %c
(the correct format specifier for out
in your example) and notice the difference. ASCII (11) is a vertical tab (see here). YTMV (your terminal may vary). Here, you printed a character, not a character representation of an integer.
The actual type that printf()
returns is int
, which is an integer. The reason for this is so that you know exactly how many bytes printf()
actually printed. This typical for that family of functions, but more useful for functions like snprintf()
so you can know if it could not print the entire length of any given format to a buffer. Note that the return type is signed, it can be negative, so the storage type you pick to store its value matters quite a bit, especially if you work with that value while assuming that its greater than or equal to zero.
Here is a handy tutorial to format specifiers you can use with the printf()
family of functions. More importantly, check the return type of the functions that you are using.
I also suggest spending some quality time with this book, be sure to work through the exercises as time permits. Its the best self-study course in C and the exercises are fun. When you get a solid grasp of C, work through the exercises again .. then compare your answers, its eye opening :)
Upvotes: 5
Reputation: 1119
int printf ( const char * format, ... );
On success, the total number of characters written is returned.
When you execute this:
char out = printf("teststring");
The string "teststring" will be printed on your screen, and the value 10 will be atrib to variable out.
Notice that the print cursor will be kept on the current line at the end of "teststring", once you did not put sufix '\n' on the end of string.
So, when you execute printf("%d\n", out); on the next line, the program will print the value 10 just after "teststring", and you will see "teststring10 on your screen."
If you really want to get rid of the 10 just remove the line printf("%d\n", out); from your code...
int main (int argc, const char * argv[]) {
int out = printf("teststring\n");
/* printf("%d\n", out); */
return 0;
}
Upvotes: 0
Reputation: 16705
#include <stdio.h>
int main (int argc, const char * argv[]) {
char out = printf("teststring");
printf("%d\n", out);
return 0;
}
Assuming that you want to assign "out" to a function (as I think you've said somewhere):
#include <stdio.h>
int main (int argc, const char * argv[])
{
char *out = NULL;
out = funcReturns("teststring");
printf("%s\n", out);
return 0;
}
If you allocate the memory inside the function that returns "teststring" then I think you need to make the return value static. For example (code untested):
char *funcReturns(char *returnStr)
{
static char *returnThis = NULL;
returnThis = strdup(returnStr);
return returnThis;
}
Upvotes: 0
Reputation: 95395
int main(int argc, char *argv[])
{
const char *str = "This is a constant string";
printf("%s\n", str);
return 0;
}
%s
is the format specifier for a string. %d
is the format specifier for a decimal representation of an integer. printf
will return the number of characters written to the output.
Upvotes: 0
Reputation: 18306
the 10 is because of the
printf("%d\n", out);
printf return the number of characters written to the output stream the length of "teststring" which is 10 assigned to "out" and the n you print the value of out - so 10 is printed.
Upvotes: 0