What is the difference between = (Assignment) and == (Equal to) operators in C?
Difference between Assignment (=) Vs Equal to (==) Operators in C
Many times this question arises what is the difference between = and == operators in C programming language? Here we are going to tell you exactly what the differences between these two operators are.
Assignment Operator (=)
= is an Assignment Operator in C, C++ and other programming languages, It is Binary Operator which operates on two operands.
= assigns the value of right side expression’s or variable’s value to the left side variable.
Let's understand by example:
x=(a+b); y=x;
Here, When first expression evaluates value of (a+b) will be assigned into x and in second expression y=x; value of variable x will be assigned into y.
Equal To Operator (==)
== is an Equal To Operator in C and C++ only, It is Binary Operator which operates on two operands.
== compares value of left and side expressions, return 1 if they are equal other will it will return 0.
Let's understand by example:
int x,y; x=10; y=10; if(x==y) printf("True"); else printf("False");
When expression x==y evaluates, it will return 1 (it means condition is TRUE) and "TRUE" will print.
Conclusion
So it's cleared now, ,both are not same, = is an Assignment Operator it is used to assign the value of variable or expression, while == is an Equal to Operator and it is a relation operator used for comparison (to compare value of both left and right side operands).
Function prototype is the important feature of C programming which was borrowed from C++. Early versions of C programming did not use function prototype.
Function prototype in C is a function declaration that provides information to the compiler about the return type of the function and the number, types, and order of the parameters the called function expect to receive.
Function prototype in C programming: Importance
Function prototype in C is used by the compiler to ensure whether the function call matches the return type and the correct number of arguments or parameters with its data type of the called function.
In the absence of the function prototype, a coder might call function improperly without the compiler detecting errors that may lead to fatal execution-time errors that are difficult to detect.
Syntax of function prototype in C programming
return_type function_name( type argument1, type argument2, ...);
#include
preprocessor directive for function prototypes of standard library functions.
Example of function prototype
Let’s consider following function definition:
int area( int length, int breadth ) //function definition
{
.... //function body
}
Now, the corresponding prototype declaration of the above function is:
int area( int length, int breadth ); //function prototype
It states that function area takes two arguments of type int
and returns area of type int
.
Difference between function prototype and function definition in C
The only difference between the function definition and its function prototype is the addition semicolon (;) at the end of prototype declaration.
But, the parameter identifier could be different in function prototype and function definition because the scope of parameter identifier in a function prototype is limited within the prototype declaration.
Actually, the compiler ignores the name of the parameter list in the function prototype. Having said that, it is good programming practice to include parameter names which increase program clarity.
Please carefully observe the following prototype declaration:
int area(int length, int breadth );
int area(int x, int y );
int area(int , int );
All of the above function prototypes are same.
Please note that a function call that does not match prototype declaration is a compilation error.
Function prototype in C: Scope and Conversion
The scope of the function prototype in C is determined by its position in the program. Generally, the function prototype is placed after the header file in the program. The scope of the function prototype is considered within the same block as the function call.
Another interesting feature of function prototype is argument conversion.
For example, the standard math library function sqrt
has a double
type parameter in the function prototype. However, it can be called with an integer argument and works perfectly.
x = sqrt(16);
The output of this statement is:
x = 4.000
As we talked earlier about conversion feature, the compiler converts a copy of integer value 16 to the double 16.0 before passing to sqrt
.
0 comments:
Post a Comment