Badly places ()'s on C

I dont know why this Linux would give me badly placed () error all the time for this;

#include <stdio.h>
int main() 
{
	register int num=0 ;
	while ((num < 5))
		++num;
	  	printf("Pass %d \n", num) ; 
	return 0 ; 
}

can anyone help me please?

Are you sure your error is in that bit of code you submitted?
Can you show us the error message you get ?

1 Like

It doesn't.

It is always MUCH easier to help you solve problems like this if you show us the error message(s) you get when you post a question like this.

As a very wild guess, I'd say that you're trying to execute C code with a shell.

Assuming that the above code is stored in a file with a name ending with ".c" (such as "example.c"), the following should compile your code (although it probably won't do what you expect it to):

gcc -o example example.c

and then you could run the compiled program with:

./example

and the output produced would be something like:

Pass 5 

If the output you wanted was something like:

Pass 1 
Pass 2
Pass 3
Pass 4 

you could get that by adding the braces shown in red above.

2 Likes

Yeah i think i was trying to execute it as a .c file whereas i compiled it as .o, nevermind, thankyou anyway :slight_smile:

---------- Post updated at 01:14 PM ---------- Previous update was at 01:10 PM ----------

#include <stdio.h>
main()
{
int a = 128;
int b = 253;
int i;

for (i=7; i>=0; i--0);
	{
	 if (a & b => 128);
	 {printf("1");}
	 else
	 {printf("0");}
	}

a = a >> 1;

}    

It won't work...

reg.c: In function 'main':
reg.c:8: error: expected ')' before numeric constant
reg.c:10: error: expected expression before '>' token
reg.c:12: error: expected expression before 'else'

thankyou

There are some fairly elementary syntax errors in your code.

#include <stdio.h>
main()
{
int a = 128;
int b = 253;
int i;

for (i=7; i>=0; i--)
        {
         if (a & b >= 128)
         {printf("1");}
         else
         {printf("0");}
        }

a = a >> 1;

}
1 Like

Thank you.

I wrote that code to get a binary form of the value of variable 'b'.. But its only giving me 00000000. Wheres the logical error?

Sahil

It was very close :slight_smile:

#include <stdio.h>
main()
{
int a = 128;
int b = 253;
int i;

  for (i=7; i>=0; i--) {
    if (b & a)
      printf("1");
    else
      printf("0");
    a = a >> 1;
  }
}
$ cc -o test1 test1.c
$ ./test1
11111101$
1 Like

if (b & a) ?
if (b&a) what? I dont see how that is a condition, could you explain it please?

Sorry about being so dumb

It's a bitwise and, meaning the resultant bits are one if the corresponding bits from both a and b are one, otherwise 0.

For example:

Dec Binary
---------------
179 10110011
217 11011001
---------------
145 10010001
---------------

As you had already used it, I presumed you knew what it meant :slight_smile:

1 Like

is short for

if (b & a != 0)

The following should work as well

int a;
int b = 253;
  for (a = 128; a > 0; a >>= 1) {
    printf ("%d", b & a);
  }

Thank you, I was trying to get the integer b = 253 in binary form though, I'm assuming this wouldn't help. because it gives me a totally different binary