Home » Programming Tips & Tricks » C - Tips & Tricks

# A safest way to check value using 'Equal To' (==) operator in C

**By: **IncludeHelp, **on ** 24 JAN 2017

Let's consider the following statement

if(result==0) { statements; }

Here, if value of result is equal to 0 then statements will be executed.

But, sometimes, by mistake programmers forget to use **"Equal To"** and use **"Assignment Operator"** (=).

### Then, what will happen?

Let suppose, we use **"="** instead of **"=="**, then the statement will be if(result=0), so condition will be false because 0 is assigning into result and throughout all program value of result will be 0 until other statement do not update the value.

### Safest way to check

Put the value (Remember, only value) at the left side of **"Equal To"** Operator and variable at the right side. Consider the below statement:

if(0==result) { statements; }

Here, 0 and result are comparing and if the condition is true statements will be executed.

### How this way is safe?

If by mistake, we forget to use **"=="** and we use **"="** then compiler will through an error because a variable's value cannot be assigned into a value (literal).

TOP Interview Coding Problems/Challenges

- Run-length encoding (find/print frequency of letters in a string)
- Sort an array of 0's, 1's and 2's in linear time complexity
- Checking Anagrams (check whether two string is anagrams or not)
- Relative sorting algorithm
- Finding subarray with given sum
- Find the level in a binary tree with given sum K
- Check whether a Binary Tree is BST (Binary Search Tree) or not
- 1[0]1 Pattern Count
- Capitalize first and last letter of each word in a line
- Print vertical sum of a binary tree
- Print Boundary Sum of a Binary Tree
- Reverse a single linked list
- Greedy Strategy to solve major algorithm problems
- Job sequencing problem
- Root to leaf Path Sum
- Exit Point in a Matrix
- Find length of loop in a linked list
- Toppers of Class
- Print All Nodes that don't have Sibling
- Transform to Sum Tree
- Shortest Source to Destination Path

Comments and Discussions

**Ad:**
Are you a blogger? Join our Blogging forum.

Learn PCB Designing: PCB DESIGNING TUTORIAL