Like Tree1Likes
  • 1 Post By stahta01

Why does compiler say invalid operands to binary & (have char* and int)

This is a discussion on Why does compiler say invalid operands to binary & (have char* and int) within the C Programming forums, part of the General Programming Boards category; (I honestly can't see anything wrong here. Code: #include <stdio.h> int main() { int num1; /*Variable to hold num*/ printf:("Please ...

  1. #1
    Registered User
    Join Date
    Oct 2011
    Posts
    81

    Why does compiler say invalid operands to binary & (have char* and int)

    (I honestly can't see anything wrong here.

    Code:
    #include <stdio.h>
    
    int main()
    
    
    {
    	int num1; /*Variable to hold num*/
    	printf:("Please enter a number:\n");
    	scanf:("%d" &num1);
    		
    if (num1%2==0) /*If clause to determine if number is prime*/
    {
    printf("Number %d is a prime number.\n" ,num1);
    }
    
    
    else
    {
    printf("Number %d is not a prime number.\n" ,num1);
    }
    
    
    return 0; /*Exits the program*/
    }

  2. #2
    Registered User
    Join Date
    May 2010
    Posts
    2,713
    You are missing a comma in your scanf() between the format string and the variable.

    In the future when you have a compile, linker error post the complete error message exactly as it appears in your development environment. These messages have information to help pinpoint the location of the error.

    Jim

  3. #3
    Registered User
    Join Date
    May 2009
    Posts
    2,553
    Edit: jimblumberg has the correct answer; but, I still wonder.

    Code:
    scanf:("%d" &num1);
    Why is there a ":" after scanf and printf?

    Tim S.
    Salem likes this.

  4. #4
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Take the colons out of lines 8 and 9 ... it's printf( not printf:

    Also num % 2 does not indicate if a number is prime or not... it indicates whether it's even or odd.

    Prime numbers are numbers that can only be divided evenly by 1 and themselves.

    eg.
    ... 3 is prime ... 3/2 = 1.5
    ... 4 is not ... 4/2 = 2

  5. #5
    Registered User
    Join Date
    Oct 2011
    Posts
    81
    Oddly, it now compiles, but assigns its own number to the num1 variable and tells me 2147315712 is a prime number.

    I want it to prompt for a number and scanf it. I have a printf statement so can't figure out why it doesn't print that.

  6. #6
    Registered User
    Join Date
    May 2010
    Posts
    2,713
    Show your current code.

    Jim

  7. #7
    Registered User
    Join Date
    Oct 2011
    Posts
    81
    Thanks all! Colons after printf/scanf. Crazy the mistakes that are made when I'm stressed.

  8. #8
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by Interista View Post
    Oddly, it now compiles, but assigns its own number to the num1 variable and tells me 2147315712 is a prime number.

    I want it to prompt for a number and scanf it. I have a printf statement so can't figure out why it doesn't print that.
    Sounds like scanf() isn't working...

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Invalid Operands to Binary Error
    By MikeyVeeDubb in forum C Programming
    Replies: 17
    Last Post: 09-23-2009, 04:13 AM
  2. error: invalid operands to binary %
    By OxKing033 in forum C Programming
    Replies: 12
    Last Post: 03-18-2008, 09:21 AM
  3. Invalid operands to binary *
    By jrfall349 in forum C Programming
    Replies: 8
    Last Post: 03-31-2007, 11:15 PM
  4. invalid operands to binary %
    By Mathsniper in forum C Programming
    Replies: 3
    Last Post: 12-03-2005, 10:21 PM
  5. invalid operands to binary ^ ?
    By seal in forum C Programming
    Replies: 14
    Last Post: 09-13-2005, 04:59 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21