is this code legal w.r.t. C standard ?

P

Pallav singh

Hi ,

is this code legal w.r.t. C standard ?
Kindly also consider C99 and C0x standard also

func( )
{
/* certain statements - A*/
if ( condition - 1) {
/* certain statements - B */
} else if ( condition - 2) {
if ( condition ) {
/* certain statements - C*/
}
int * ptr = malloc( 4 );
// Can we delarecalre variable here
*ptr = 10;
printf( " value at *ptr = %d \n",*ptr);
} else {
/* certain statements - D */
}
return ;
}








}
 
K

Keith Thompson

Pallav singh said:
is this code legal w.r.t. C standard ?
No.

Kindly also consider C99 and C0x standard also

There is no C0x standard. Work is in progress on a C1x standard, but
it's only available in draft form.

Legal but deprecated in C90, illegal in C99. C90 deprecated
non-prototype function declarations. C99 eliminated implicit int.

Better:

void func(void)
{
/* certain statements - A*/
if ( condition - 1) {

You're asking whether this code is legal, but it's obviously
pseudo-code.

You could have written an actual program and tried compiling it
yourself; that won't definitively tell you whether it's legal, but
with appropriate compiler options it could give you a reasonable
indication.

(I'm assuming that "condition - 1" is a stand-in for some arbitrary
condition; otherwise you're subtracting 1 from '"condition", which
is undeclared.)
/* certain statements - B */
} else if ( condition - 2) {
if ( condition ) {
/* certain statements - C*/
}
int * ptr = malloc( 4 );
// Can we delarecalre variable here

Illegal (a syntax error) in C90. Declarations and statements may be
mixed in C99. In C99, this is legal but buggy, or at least
non-portable.

You seem to be assuming sizeof(int) == 4.

Better:

int *ptr = malloc(sizeof *ptr);
*ptr = 10;

Undefined behavior if malloc() failed.
 
P

Pallav singh

Keith said:
There is no C0x standard. Work is in progress on a C1x standard, but
it's only available in draft form.


Legal but deprecated in C90, illegal in C99. C90 deprecated
non-prototype function declarations. C99 eliminated implicit int.

Better:

void func(void)


You're asking whether this code is legal, but it's obviously
pseudo-code.

You could have written an actual program and tried compiling it
yourself; that won't definitively tell you whether it's legal, but
with appropriate compiler options it could give you a reasonable
indication.

(I'm assuming that "condition - 1" is a stand-in for some arbitrary
condition; otherwise you're subtracting 1 from '"condition", which
is undeclared.)


Illegal (a syntax error) in C90. Declarations and statements may be
mixed in C99. In C99, this is legal but buggy, or at least
non-portable.

You seem to be assuming sizeof(int) == 4.

Better:

int *ptr = malloc(sizeof *ptr);


Undefined behavior if malloc() failed.


--
Keith Thompson (The_Other_Keith) (e-mail address removed) <http://www.ghoti.net/~kst>
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"

+++++++++++++++++++++++++++++++++++++++++++++++++

Hi keith

is the deleration after if( ) {} legal ??
can we have declerative statement after the Block inside a Block in
C Language??
int *ptr = malloc(sizeof *ptr);

Pallav
 
B

Barry Schwarz

+++++++++++++++++++++++++++++++++++++++++++++++++

Hi keith

is the deleration after if( ) {} legal ??

In C90, both the declaration not at the beginning of the block and the
// comments are illegal. In C99, both are legal.
can we have declerative statement after the Block inside a Block in
C Language??

A "declaration" (which is different than a "statement" as the standard
uses the words) must be at the beginning of a block in C90. Even if
the block is nested within a larger (or higher level) block,
declarations can still be coded at the start of the inner block.
Declarations are not allowed to follow statements within the same
block.

C99 eliminated this restriction.
 
E

Eric Sosman

Hi ,

is this code legal w.r.t. C standard ?

No.
Kindly also consider C99 and C0x standard also

ANSI/C89/C90:

pallav.c:2: warning: return type defaults to 'int'
pallav.c: In function 'func':
pallav.c:4: error: 'condition' undeclared (first use in this function)
pallav.c:4: error: (Each undeclared identifier is reported only once
pallav.c:4: error: for each function it appears in.)
pallav.c:10: warning: implicit declaration of function 'malloc'
pallav.c:10: warning: incompatible implicit declaration of built-in
function 'malloc'
pallav.c:10: warning: ISO C90 forbids mixed declarations and code
pallav.c:11: error: expected expression before '/' token
pallav.c:13: warning: implicit declaration of function 'printf'
pallav.c:13: warning: incompatible implicit declaration of built-in
function 'printf'
pallav.c:17: warning: 'return' with no value, in function returning non-void

C99:

pallav.c:2: warning: return type defaults to 'int'
pallav.c: In function 'func':
pallav.c:4: error: 'condition' undeclared (first use in this function)
pallav.c:4: error: (Each undeclared identifier is reported only once
pallav.c:4: error: for each function it appears in.)
pallav.c:10: warning: implicit declaration of function 'malloc'
pallav.c:10: warning: incompatible implicit declaration of built-in
function 'malloc'
pallav.c:13: warning: implicit declaration of function 'printf'
pallav.c:13: warning: incompatible implicit declaration of built-in
function 'printf'
pallav.c:17: warning: 'return' with no value, in function returning non-void

C0x:

cc1.exe: error: unrecognized command line option "-std=c0x"
 
K

Kenny McCormack

ANSI/C89/C90:

pallav.c:2: warning: return type defaults to 'int'
pallav.c: In function 'func':
pallav.c:4: error: 'condition' undeclared (first use in this function)

Blah, blah, blah.

What makes you think you are entitled to assume that this is the entire
implementation of the program?

For example, "condition" is almost certainly assumed to be an external
(global, if you will, but of course, we know that C doesn't have global
variables...)

--
One of the best lines I've heard lately:

Obama could cure cancer tomorrow, and the Republicans would be
complaining that he had ruined the pharmaceutical business.

(Heard on Stephanie Miller = but the sad thing is that there is an awful lot
of direct truth in it. We've constructed an economy in which eliminating
cancer would be a horrible disaster. There are many other such examples.)
 
K

Kenny McCormack

Richard said:
I find it amazing that a supposedly intelligent and experienced
programmer/sentient being would bother to be so graceless, petty and
vindictive and post such a useless, rude and bitter reply.

Indeed. But, emphasis on the word "supposedly". I think it is well
understood that most of the CLC regs are folks who've been worked out of
the system. Consultants at best.
Why do you do it?

Good question. I'd be very interested to see Eric's answer.

--
Religion is regarded by the common people as true,
by the wise as foolish,
and by the rulers as useful.

(Seneca the Younger, 65 AD)
 
S

Seebs

is the deleration after if( ) {} legal ??

I've just quoted the part where he answered EXACTLY this question.
can we have declerative statement after the Block inside a Block in
C Language??

Again, he just answered this question.

Mixed declarations and code are allowed in C99 but not in C90.

-s
 
K

Keith Thompson

Seebs said:
I've just quoted the part where he answered EXACTLY this question.


Again, he just answered this question.

Mixed declarations and code are allowed in C99 but not in C90.

To be clear, mixed declarations and statements within a block are
allowed in C99 but not in C90. (It's not 100% clear, at least to
me, that "code" means "statements", and that declarations are not
"code".)
 
S

Seebs

To be clear, mixed declarations and statements within a block are
allowed in C99 but not in C90. (It's not 100% clear, at least to
me, that "code" means "statements", and that declarations are not
"code".)

Good clarification. I think the "declarations and code" nomenclature is
semi-canonical, but I don't object at all to something less ambiguous.

-s
 

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,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top