Re: How printf() works???????



On Thu, 6 Mar 2008 21:59:49 -0800 (PST), sant.tarun@xxxxxxxxx wrote:

Hello,

Hi Tarun.

I would appreciate some comments about the piece of code given below
and explanation about the result of this specified code.

int x = 20;

printf("%d %d %d",x < 30, x = 40; x > 10);

The ";" in this statement is a syntax error. I assume you meant ','
instead of ';'.

After fixing this error and encompassing the fixed code in a proper
program:

/*foo.c*/
#include <stdio.h>
int main(void)
{
int x = 20;
printf("%d %d %d",x < 30, x = 40, x > 10);
return 0;
}

I get the same output as you:

0 40 1

But this output being the same as yours is just a coincidence, because
the code has undefined behavior. In the printf call, the variable x
depends on the order of evaluation. I suspected this, and PC-lint [1]
confirms it:

foo.c(6) : Info 730: Boolean argument to function
foo.c(6) : Warning 564: variable 'x' depends on order of evaluation

The 564 warning identifies the undefined behavior.

The 730 warning suggests a potential problem (though not really a
problem in the instant case [2]), as explained by the PC-lint
documentation:

730 Boolean argument to function -- A Boolean was used as an
argument to a function. Was this intended? Or was the
programmer confused by a particularly complex conditional
statement. Experienced C programmers often suppress this
message. [snip C++-specific stuff]

Best regards
--
jay

[1]
http://www.gimpel.com/

[2]
Depending on whom you ask







.



Relevant Pages

  • Re: check-boxes in JTable
    ... public TableCellRenderer getCellRenderer(int row, int column) { ... TableColumn tableColumn = getColumnModel.getColumn; ... boolean isSelected, boolean hasFocus, int row, int column) { ... return editorComponent; ...
    (comp.lang.java.gui)
  • Re: identity...... Was: The wisdom of the object mentors
    ... // this is it's implementation/mapping from a hidden domain of {int x int} ... I'd say that the set of all propositions is language defined behavior or ... Boolean object has no place to keep the class identity. ... bool IsBool1() ...
    (comp.object)
  • Re: Design Questions re Subclassing
    ... public AgeRange(int firstAge, int secondAge) { ... public boolean between(boolean includesLow, int input, boolean ... >> really digested the proper use of Generics yet. ... >> You raise a good point in questioning whether TreeSet should be extended ...
    (comp.lang.java.programmer)
  • Re: KeyEvent consume() not working on JTextField, what am I doing wrong?
    ... public static final int YEAR_1 = 0; ... private boolean keyPressed_actionPerformed{ ... public void nextPosition(int intKeyCode, int intCharPos) { ... String strTempNewValue; ...
    (comp.lang.java.help)
  • Re: Duplicate Header Declaration
    ... void swap(char *s, char *t); ... int is_space; ... syntax error before '{' token ...
    (comp.lang.c)