-5

I have code in one of the source file as follows:

#include <stdio.h>
#include <math.h>

int a ;
int b = 256 ;
int c = 16 ;
int d = 4 ;

int main() {

    if ((d <= (b) && (d == ( c / sizeof(a))))
    {
        printf("%d",sizeof(a) );
    }
    return 0;

} 

I have removed the casts and have simplified on the data names. The sizeof(a) can be taken as 4. I want to know if the the if syntax is a valid one and if so why doesn't it execute?

PS : I haven't sat down on this for long due to time constraints. Pardon me if you find a childish error in the code.

PeeHaa
  • 71,436
  • 58
  • 190
  • 262
Borrito
  • 357
  • 1
  • 3
  • 6

6 Answers6

4

Have you tried to compile it?

Your if statement needs one more ) . That or simplify it to:

if ((d <= b) && (d == c / sizeof(a)))

Your printf statement should use "%zu\n" for C99, although it's complicated.

Community
  • 1
  • 1
Yusuf X
  • 14,513
  • 5
  • 35
  • 47
2

One closing bracket is missing IMO. I opended up your if with vertical parantheses alignment technique.

#include <stdio.h>
#include <math.h>

int a ;
int b = 256 ;
int c = 16 ;
int d = 4 ;

int main() {

    if (
        (
         d <= (b) && (
                      d == (
                             c / sizeof(a)
                           )
                     )
        )
       //Here needs another closing parantheses
    {
        printf("%d",sizeof(a) );
    }
    return 0;

}

Actually you can sefely remove some parantheses here in your snippet.

Seçkin Savaşçı
  • 3,446
  • 2
  • 23
  • 39
1

Try adding a newline to your printf statement:

printf("%d\n", sizeof a);

Standard output is usually buffered, so output doesn't always show up on your console immediately unless there's a newline or you add a fflush(stdout); after the printf call.

John Bode
  • 119,563
  • 19
  • 122
  • 198
1
((d <= (b) && (d == ( c / sizeof(a)))) 

Looks an extra ( before the b.

( (d <= b) && (d == (c/sizeof(a))))
scrappedcola
  • 10,423
  • 1
  • 32
  • 43
  • As my comment under OP are all those parenthesis really needed. (I'm not a C guy). It looks like bad mistakes going to happen to me (which is proven by OP). – PeeHaa Sep 06 '12 at 15:33
  • @PeeHaa None of the inner parens are needed. Some people (not me) think the ones around the relational operators are good practice. – Jim Balter Sep 06 '12 at 15:39
1

You have forgotten closing one paranthesis in line 11 you have opened six and closed five.

user1180619
  • 338
  • 1
  • 4
  • 11
1

I think you have an issue with your if statement, it should look like this

if ( (d <= b) && (d == ( c / sizeof(a))))

you had an extra ( on the left side of the 'b'

PeeHaa
  • 71,436
  • 58
  • 190
  • 262
AnthonyFG
  • 68
  • 8