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. = 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.

3. 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 * 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. is there any time, apart from using an 'if' statement, that '==' is actually used?

5. 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. 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?

7. 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

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

8. 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;
}```