Join WhatsApp

Join Now

Join Telegram

Join Now

Why My Login System Failed—and What It Taught Me About Relational Operators in C

By Sagar Miraje

Published On:

Follow Us

A humbling lesson in the world of Relational Operators in C. I’ll never forget the look on my face when my simple login system let everyone in. Everyone. No matter what password you typed, it printed: “Welcome, Admin.” 

At first, I blamed the compiler. Then I blamed the logic. Eventually, I stared at the line that had betrayed me: 

if (inputPassword = password)  //  
There it was. A single equal sign. A moment of overconfidence.

The Mirror Test: == vs = 

Here’s how I now explain it in class sometimes dramatically: 

“Imagine standing in front of a mirror. You’re trying to see if what’s on the left matches what’s on the right. That’s ==.” 

Then I pause. I write on the board: 

if (x = y) 
 

“This,” I say, “is not a mirror. This is an assignment. You’re not asking if they’re equal you’re telling x to become y.” 

That’s when the eyes in the classroom widen. It clicks. And I always follow up with the real example from my login project. Students relate immediately. They laugh. One even wrote in their notes: “Don’t trust = in if.” 

Understanding the Six Relational Operators 

In C, relational operators are your primary tools for comparing values. They don’t change the values they inspect them. They return either 1 (true) or 0 (false). Always. 

Here are the six operators we use: 

They’re simple symbols, but the logic they express can define the behavior of your entire program. 

Classroom Moment: The Bingo Test 

I often bring up this scenario in class: 

“I have two variables. a = 300, b = 2090. Your goal is to write a condition inside if(…) such that the output will be Bingo! You are in.” 

I give them this code: 

int a = 300; 
int b = 2090; 
 
if (______) { 
   printf(“Bingo! You are in\n”); 
} else { 
   printf(“Oops! You are out\n”); 

 

I give them one minute. 

The correct condition? 

if (b >= a) 
 

Because 2090 is clearly greater than 300. And we’re checking a relationship, not performing math. 

But one student once wrote: 

if (a > b) 
 

And when I asked him why, he said, “I thought the first variable should always go on the left.” 

And that was another key teaching moment. 

Tip: Relational Operators Don’t Care About Left or Right 

C doesn’t care about which side is which. It only cares about truth. 

5 < 10     // true 
10 > 5     // also true 
 

So whenever students get stuck, I remind them: 

Focus on what you’re asking, not on keeping values on the left. 

It’s not about symmetry. It’s about meaning. 

The Time Someone Wrote: if (marks == 35 || marks == 36 || marks == 37 || … ) 

During an exam evaluation script demo, one student tried to check if a student had passed by writing a giant if condition: 

if (marks == 35 || marks == 36 || marks == 37 || marks == 38 || marks == 39 || marks == 40) 
 

He had typed every passing mark. I had to laugh. 

So I showed them a better way: 

if (marks >= 35) 
One operator. Cleaner. Clearer. Faster. That’s the power of relational operators when you understand them. 

A Word on Boolean Results 

Let’s not forget: every relational operation in C returns either 1 (true) or 0 (false). 

That’s why this works: 

int result = (a > b); 
printf(“%d”, result);  // Will print 1 or 0 
 

We can store the result of a comparison in a variable. It’s just a number under the hood. 

One student was stunned when I did this: 

int truth = (5 != 5);   // result: 0 
 

He thought != would “crash” the program. Nope. It just returns false. 

Common Mistakes and How to Avoid Them 

Mistake 1: Using = instead of == 

I’ve already shared my failure with this. One way I teach students to avoid it is to read their if conditions out loud: 

if (x == 10)  // “If x is equal to 10” 
if (x = 10)   // “If x becomes 10” wait, what?! 
 

Reading code like prose exposes intent errors quickly. 

Mistake 2: Thinking Relational Operators Are Math 

Relational operators are logical, not arithmetic. 

int result = 5 < 10;  // result is 1 
 

Students sometimes expect this to return 5 or 10. I explain: “You’re asking a yes or no question. You get a yes or no answer 1 or 0.” 

Mistake 3: Chaining Comparisons Incorrectly 

This comes up a lot: 

if (10 < x < 20)  // Looks good? It’s wrong! 
 

This doesn’t check if x is between 10 and 20. C interprets it as: 

(10 < x) < 20 
 

Let’s say x = 15. Then: 

  • 10 < 15 is true (1) 
  • 1 < 20 is also true 

Seems to work… but try it with x = 25: 

  • 10 < 25 is true (1) 
  • 1 < 20 is still true 

Boom. Your logic fails silently. 

Correct way? 

if (x > 10 && x < 20) 
 

I call this the logical sandwich two relational operators held together by &&. 

Real World Scenarios Where Relational Operators Shine 

1. Login Systems 

if (enteredPIN == storedPIN) 
 

Get it right, or let in the wrong user. 

2. Grading Logic 

if (marks >= 90) printf(“A grade”); 
 

Real logic, real stakes. 

3. Comparing Dates or Times 

if (eventTime <= currentTime) 
 

This comes up in scheduling apps. One of my students built a bus arrival notifier and used relational operators to great effect. 

4. Sorting Algorithms 

When you swap values in a sort, you’re constantly checking: 

if (arr[i] > arr[j]) 
 

Without relational operators, sorting wouldn’t exist. 

Relational Operators and Control Flow: The Dynamic Duo 

Relational operators are almost never used alone. They live inside if, while, for, and do while. 

Example: 

for (int i = 0; i < 10; i++) 
   printf(“%d “, i); 
 

Here, i < 10 controls everything. The moment it becomes false, the loop stops. 

Relational operators control behavior. They’re not just comparisons they’re decisions. 

Relational Operators with Functions 

One final insight: you can even use relational operators on function results. 

if (getTemperature() >= 100) 
   alertUser(); 
 

Think of the relational operator as the gatekeeper. It looks at the value returned by a function and decides what to do next. 

That’s elegant, powerful, and expressive. 

Closing Thoughts 

Teaching relational operators is about more than symbols. It’s about cultivating precision in thinking. Helping students stop treating code like a sequence of tricks, and start seeing it as a dialogue: “Is this true?” Yes or no. 

And all it takes is ==, !=, <, >, <=, and >=. 

Simple tools. Profound logic. 

Like I always say: 

“With arithmetic, you calculate. With relational operators, you decide.” 

Why did if (x = y) not give a compilation error? 

Because = is a valid expression in C it returns the value of y, and that value is treated as true or false. But it’s likely not what you intended. 

Are relational operators only used in if statements? 

No they’re also used in loops (for, while), logical expressions, and even return conditions from functions. 

Can relational operators compare characters or strings? 

They can compare characters (like ‘a’ < ‘z’), but for strings, use functions like strcmp() not ==. 

What’s the difference between == and = again? 

== checks for equality. = assigns a value. Think of == as asking a question, and = as making a statement. 

What does !(a == b) mean? 

It means “a is not equal to b.” It’s equivalent to a != b. Sometimes used for clarity or in compound logic.

I am Sagar Miraje, a Computer Science graduate with a Bachelor of Technology and 7 years of experience as a C language programming developer. I specialize in writing efficient, low-level code for systems programming, embedded applications, and performance-critical software. My strength lies in optimizing memory usage, handling pointers, and working close to the hardware to deliver fast and reliable solutions. Over the years, I’ve contributed to core system modules, debugged complex runtime issues, and collaborated with cross-functional engineering teams. I’m passionate about problem-solving and always eager to push the limits of what C programming can achieve.

Leave a Comment