simple question about = and ==

This is a discussion on simple question about = and == within the C Programming forums, part of the General Programming Boards category; whats the diffrence between = and ==? in the following program if i replace = with == the square root ...

  1. #1
    Registered User
    Join Date
    Jul 2004
    Posts
    1

    simple question about = and ==

    whats the diffrence between = and ==? in the following program if i replace = with == the square root of 5 comes out to be 134513664..

    Code:
      main()
    {
            int num;
            printf("Hello, World!\n");
            num = square(5);
            printf("The square root of 5 is %i\n", num);
    }
    
    int square(int n)
    {
            return(n * n);
    }

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    = is for assignment
    == is for comparison

    The reason you get a huge wierd number, is because the variable ends up being used uninitialized, and so, it stores some arbitrary number. Thus, when you use it in your printf, you get some unexpected result.

    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Registered User caroundw5h's Avatar
    Join Date
    Oct 2003
    Posts
    751
    It might help to be less confusing if you interperet = as gets and == as is

    for Example try looking at your code like this:
    PHP Code:
    #include <stdio.h>
    #include <math.h>
    int square(int);
    int  main(void){
            
    int num;
            
    printf("Hello, World!\n");
            
    num square(5); /* num gets the value of 5 squared */
            
    printf("5 squared is  is %d\n\n"num);
             
    /* and extra example */
            
    if ( num == 22){
                 
    printf("Num is 22\n");
             }
             else
                 
    printf("Num is not 22, it's really %d"num);
            
    getchar();
            return 
    0;

    }

    int square(int n){
            return(
    n);

    Also. My math is pretty bad but your function returns the square of n. if you want the square root of a number. Include <math.h> an use the the sqrt() function.
    Hope that helps a bit.

  4. #4
    ---
    Join Date
    May 2004
    Posts
    1,379
    is there any time, apart from using an 'if' statement, that '==' is actually used?

  5. #5
    Registered User caroundw5h's Avatar
    Join Date
    Oct 2003
    Posts
    751
    Quote Originally Posted by sand_man
    is there any time, apart from using an 'if' statement, that '==' is actually used?

    yeah
    PHP Code:
     num == 22 printf("num is 22") : printf("See i'm not using if"); 

  6. #6
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,163
    You can use it anywhere you want to test a condition.

    Code:
    #include <stdio.h>
    
    int main(void)
    {
      int i, num = 0;
    
      for(i = 0;i < 100;i++)
        num += i % 3 == 0;
    
      printf("There are %d numbers divisible by 3 in the range 0 to 99!\n", num);
    
      return 0;
    }
    That's just one example! Of course, I'm not really sure that that code will always come up with the correct number since according to the C standard a true condition can be any non-zero value, not necessarily 1. I'm just not positive that == will always give a 0 or 1 value across different compilers. Anyone know if that's part of the standard?
    Last edited by itsme86; 07-28-2004 at 10:28 PM.

  7. #7
    Registered User linuxdude's Avatar
    Join Date
    Mar 2003
    Location
    Louisiana
    Posts
    926
    first
    Also. My math is pretty bad but your function returns the square of n. if you want the square root of a number. Include <math.h> an use the the sqrt() function.
    To square a number is different from square root. He must have wrote his own square function which is n to the power of 2, not to the power of .5 (two different things) secondly
    is there any time, apart from using an 'if' statement, that '==' is actually used?
    Yes besides caroundw5h's code(i consider the ternary operator an if) to return a boolean valude
    Code:
    int is_even(int a){
         return ((a%2)==0);
    }
    of course this is an unispiring kindergarten example to see if a number is even, by returning 1 if it is. So then you can use it like so
    Code:
    if(is_even(num)){
    . But you get the idea, anything can become very complex and quite elegant

    [edit]about the standard of 1 or 0. The compiler will interpret 0 as false and any nonzero number as true. But the conditional will always be 1 or 0.

    I interpret = as "equals" and == as "is equal to." That is what C for Dummies told me to say
    Last edited by linuxdude; 07-28-2004 at 10:53 PM.

  8. #8
    Helper
    Join Date
    Jun 2004
    Posts
    255
    Quote Originally Posted by sand_man
    is there any time, apart from using an 'if' statement, that '==' is actually used?
    The result of a logical expression is 0 or 1. It can be stored in an int:
    Code:
    int main (int argc, char ** args)
    {
       int ret;
       int ok = argc == 2;
    
       if (ok)
       {
          /* app() */
          ret = EXIT_SUCCESS;
       }
       else
      {
          /* usage() */
          ret = EXIT_FAILURE;
      }
    
       return ret;
    }
    Emmanuel Delahaye

    "C is a sharp tool"

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Reading string one character at a time.
    By mkthnx001 in forum C++ Programming
    Replies: 10
    Last Post: 06-14-2009, 02:18 PM
  2. Separate long string into multiple arrays
    By cashmerelc in forum C Programming
    Replies: 6
    Last Post: 11-27-2007, 01:57 AM
  3. process killer on a loop
    By Anddos in forum Windows Programming
    Replies: 8
    Last Post: 01-11-2006, 12:50 AM
  4. Need help with Tic Tac Toe
    By Zerostatic in forum C++ Programming
    Replies: 19
    Last Post: 07-19-2002, 07:20 PM
  5. Contest Results - May 27, 2002
    By ygfperson in forum A Brief History of Cprogramming.com
    Replies: 18
    Last Post: 06-18-2002, 01:27 PM

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