!printf()

K

Keith Thompson

Franken Sense said:
I was commenting-out a print statement, and I used fortran by mistake:

while((len = getline(fp, text, text_length)) > 0)
{
++ lineNumber;
!printf("%lu, %s", lineNumber, text);
root_node = add_to_tree( root_node, text);
}

Not only did this compile, it behaved, that is, it printed as if the !
didn't exist. How is this not an error?

printf() returns an int result (which is often ignored). "!" is the
unary logical not operator; it yields 1 if its argument is zero, 0 if
its argument is non-zero. printf returned some positive value
(assuming it succeeded), "!" yielded the value 0, and that value was
then discarded.
 
B

Beej Jorgensen

Franken Sense said:
!printf("%lu, %s", lineNumber, text);

Ha! That's one of the better non-errors I've seen.

printf() has a successful return value, usually ignored, of the number
of characters printed.

'!' is the logical "not" operator, which has a result of 0 if the
operand isn't 0. (Or a result of 1 if the operand is 0.)

So printf() probably returns some non-zero number, then you compute 0
with the '!', then you discard the result. No harm, no foul.

You could even stack them up if you wanted:

!!!!!!!!!printf("%lu, %s", lineNumber, text);

(none of this is recommended by me. :)

My compiler gives me a warning for these cases: "value computed is not
used".

-Beej

Here's some demo code that saves the result of printf() and shows it:

---------------------------- 8< ----------------------------
#include <stdio.h>

int main(void)
{
int x;

x = printf("Hello, world!\n");
printf("x is %d\n", x);

x = !printf("Hello, world!\n"); /* not! */
printf("x is %d\n", x);

return 0;
}
---------------------------- 8< ----------------------------
Hello, world!
x is 14
Hello, world!
x is 0
---------------------------- 8< ----------------------------
 
F

Franken Sense

I was commenting-out a print statement, and I used fortran by mistake:

while((len = getline(fp, text, text_length)) > 0)
{
++ lineNumber;
!printf("%lu, %s", lineNumber, text);
root_node = add_to_tree( root_node, text);
}

Not only did this compile, it behaved, that is, it printed as if the !
didn't exist. How is this not an error?
 
K

Keith Thompson

Beej Jorgensen said:
Ha! That's one of the better non-errors I've seen.

printf() has a successful return value, usually ignored, of the number
of characters printed.

'!' is the logical "not" operator, which has a result of 0 if the
operand isn't 0. (Or a result of 1 if the operand is 0.)

So printf() probably returns some non-zero number, then you compute 0
with the '!', then you discard the result. No harm, no foul.

You could even stack them up if you wanted:

!!!!!!!!!printf("%lu, %s", lineNumber, text);

(none of this is recommended by me. :)

My compiler gives me a warning for these cases: "value computed is not
used".
[...]

I get the same warning for this:

#include <stdio.h>
int main(void)
{
!!!printf("He%d%d%d, W%dRLD\n",
!!"hello", !!",", !" ", !!!"world");
return !!!11ll;
}
 
F

Franken Sense

In Dread Ink, the Grave Hand of Beej Jorgensen Did Inscribe:
Ha! That's one of the better non-errors I've seen.

printf() has a successful return value, usually ignored, of the number
of characters printed.

'!' is the logical "not" operator, which has a result of 0 if the
operand isn't 0. (Or a result of 1 if the operand is 0.)

So printf() probably returns some non-zero number, then you compute 0
with the '!', then you discard the result. No harm, no foul.

You could even stack them up if you wanted:

!!!!!!!!!printf("%lu, %s", lineNumber, text);

(none of this is recommended by me. :)

My compiler gives me a warning for these cases: "value computed is not
used".

-Beej

Here's some demo code that saves the result of printf() and shows it:

---------------------------- 8< ----------------------------
#include <stdio.h>

int main(void)
{
int x;

x = printf("Hello, world!\n");
printf("x is %d\n", x);

x = !printf("Hello, world!\n"); /* not! */
printf("x is %d\n", x);

return 0;
}
---------------------------- 8< ----------------------------
Hello, world!
x is 14
Hello, world!
x is 0
---------------------------- 8< ----------------------------

Interesting.

E:\gfortran\dan>gcc beej1.c -Wall -o out
beej1.c:16:28: warning: no newline at end of file

E:\gfortran\dan>out
Hello, world!
x is 14
Hello, world!
x is 1

E:\gfortran\dan>type beej1.c
#include <stdio.h>

int main(void)
{
int x;

x = printf("Hello, world!\n");
printf("x is %d\n", x);

x = !(!printf("Hello, world!\n")); /* not! */
printf("x is %d\n", x);

return 0;
}

// gcc beej1.c -Wall -o out
E:\gfortran\dan>
--
Frank

Most of us here in the media are what I call infotainers...Rush Limbaugh is
what I call a disinfotainer. He entertains by spreading disinformation.
~~ Al Franken
 
F

Franken Sense

In Dread Ink, the Grave Hand of Keith Thompson Did Inscribe:
printf() returns an int result (which is often ignored). "!" is the
unary logical not operator; it yields 1 if its argument is zero, 0 if
its argument is non-zero. printf returned some positive value
(assuming it succeeded), "!" yielded the value 0, and that value was
then discarded.

It takes me a bit to get used to all the return values with C.
--
Frank

...................... o _______________ _,
` Good Morning! , /\_ _| | .-'_|
`................, _\__`[_______________| _| (_|
] [ \, ][ ][ (_|
 
F

Franken Sense

In Dread Ink, the Grave Hand of Keith Thompson Did Inscribe:
My compiler gives me a warning for these cases: "value computed is not
used".
[...]

I get the same warning for this:

#include <stdio.h>
int main(void)
{
!!!printf("He%d%d%d, W%dRLD\n",
!!"hello", !!",", !" ", !!!"world");
return !!!11ll;
}

I get no warning.

E:\gfortran\dan> gcc beej2.c -Wall -o out

E:\gfortran\dan>out
He110, W0RLD

E:\gfortran\dan>type beej2.c
#include <stdio.h>
int main(void)
{
!!!printf("He%d%d%d, W%dRLD\n",
!!"hello", !!",", !" ", !!!"world");
return !!!11ll;
}

// gcc beej2.c -Wall -o out

E:\gfortran\dan>

I think that's hilarious.
--
Frank

The biases the media has are much bigger than conservative or liberal.
They're about getting ratings, about making money, about doing stories that
are easy to cover.
~~ Al Franken,
 
K

Keith Thompson

Franken Sense said:
In Dread Ink, the Grave Hand of Keith Thompson Did Inscribe:

Would you identify anything in this program as the comma operator?

No. Of the six commas in the program, two are within string literals,
and the other four separate function arguments.
 
F

Franken Sense

In Dread Ink, the Grave Hand of Keith Thompson Did Inscribe:
I get the same warning for this:

#include <stdio.h>
int main(void)
{
!!!printf("He%d%d%d, W%dRLD\n",
!!"hello", !!",", !" ", !!!"world");
return !!!11ll;
}

Would you identify anything in this program as the comma operator?
--
Frank

No Child Left Behind is the most ironically named act, piece of legislation
since the 1942 Japanese Family Leave Act.
~~ Al Franken, in response to the 2004 SOTU address
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,236
Members
46,825
Latest member
VernonQuy6

Latest Threads

Top