condition convention 'if (10 == j)...'

Andrew Koenig ark at alice.UUCP
Wed May 1 00:10:55 AEST 1985


> Just that instead of writing, in the traditional manner,
>	if(pc=malloc(nbytes)==NULL)...
> you have to insert extra parens if you use the other form:
>	if(NULL==(pc=malloc(nbytes)))..
> This is because == has higher precedence than =, and the expression will
> first evaluate NULL==pc, then try to set the resulting constant to whatever 
> malloc() returns (except, of course, the compiler doesn't let you get that far).

If you write
	if(pc=malloc(nbytes)==NULL)...
that will set pc to 1 if malloc succeeds and 0 if it fails.
You need the parens anyway:
	if((pc=malloc(nbytes)==NULL)...



More information about the Comp.lang.c mailing list