Why is it that in this statement "c = '\t'" is not "c == '\t'" (an additional equal sign). I read an explanation that it is because \t is a visual representation of the tab character.

Isn't \n and ' ' also visual representations of a newline and space character respectively?

if (c == ' ' || c == '\n' || c = '\t')

Dani AI

Generated

: the core issue is not that \t is "visual" — it is an escape sequence that denotes the horizontal-tab character in the execution character set. is right that you want a comparison in that test, and is also right that a single = in a condition performs an assignment and that the assignment expression itself yields a value (which a C if treats as true when non-zero).

A few practical, reliable points to avoid bugs and undefined behavior:

  • In C, character literals like '\t' are integer character constants and get converted as needed; storing input from getchar should use int so EOF can be distinguished. See the language notes on character constants for details: character constants.
  • Prefer the C library functions for whitespace checks instead of chaining character comparisons. isspace (and isblank for just horizontal-space) handles all standard whitespace characters and avoids missing cases. Important: pass an unsigned char (or EOF) to these functions to avoid undefined behavior.

Example pattern:

#include <ctype.h>

int ch = getchar();
if (ch != EOF && isspace((unsigned char)ch)) {
    /* ch is a whitespace character */
}
  • Turn on compiler warnings (for example -Wall -Wextra with GCC/Clang). They will flag suspicious constructs such as an assignment used as a condition. If an assignment in a condition is intentional, surround it with extra parentheses to document intent, or use a different structure so the intent is clear.
  • For quick checks of only space-or-tab, use isblank; for any whitespace (space, tab, newline, vertical tab, form-feed, carriage return) use isspace. Reference: isspace / isblank and assignment expression behavior.

These steps reduce subtle bugs from accidental = vs ==, signed-char pitfalls, and locale/whitespace edge cases.

Recommended Answers

All 2 Replies

Why is it that in this statement "c = '\t'" is not "c == '\t'" (an additional equal sign). I read an explanation that it is because \t is a visual representation of the tab character.

Isn't \n and ' ' also visual representations of a newline and space character respectively?

if (c == ' ' || c == '\n' || c = '\t')

Incorrect! It should be c == '\t' There's nothing special about the '\t' c = '\t' will assign a tab to variable c

In addition to what Aia said about c = '\t' :
The assignment operator produces a value, values can be used to control an if-statement, like in your example. That's the reason why your code will still compile and run. In C every zero and non-zero value are respectively equivalent for false and true, the '\t' character has a non-zero value thus is equivalent for true.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.