How to Fix “expected a ‘(’” in C++ (Visual Studio)
Short answer: an if, while, for or switch is missing the round brackets around its condition. In C++ they aren’t optional: write if (x > 5), not if x > 5. This is one of the most common mistakes for people who learned Python first.
| Where | What you see |
|---|---|
| Visual Studio editor (red squiggle) | expected a '(' |
| Visual Studio build | C2061: syntax error: identifier 'x' |
| GCC (g++, MinGW, Code::Blocks) | expected '(' before 'x' |
| Clang (Xcode, macOS) | expected '(' after 'if' |
Hit “expected a '('”? It's a classic beginner trap, and there are others like it.
Free 1-page checklist: The 10 Mistakes Every C++ Beginner Makes. It shows the exact traps that slow down every C++ beginner, so you can avoid them from day one.
🔒 No spam. Unsubscribe anytime.
Cause 1: An if Without Brackets
#include <iostream>
int main() {
int x = 7;
if x > 5 {
std::cout << "Big\n";
}
}
Visual Studio editor: expected a '('
MSVC build: error C2061: syntax error: identifier 'x'
error C2143: syntax error: missing ';' before '{'
GCC: error: expected '(' before 'x'
Clang: error: expected '(' after 'if'
Clang says it most clearly: after if must come a (. Fix:
#include <iostream>
int main() {
int x = 7;
if (x > 5) {
std::cout << "Big\n";
}
}
Big
The same goes for else if: else if (x > 3). A plain else has no condition, so it has no brackets.
Cause 2: A while Loop Without Brackets
#include <iostream>
int main() {
int x = 0;
while x < 3 {
x++;
}
}
Visual Studio editor: expected a '('
MSVC build: error C2061: syntax error: identifier 'x'
GCC: error: expected '(' before 'x'
error: expected ')' before '{' token
Clang: error: expected '(' after 'while'
Fix: while (x < 3) {. After the loop, x is 3.
Want the fundamentals explained properly? C++ Better Explained covers the core of C++ in plain English, with analogies and code diagrams, including the parts that trip beginners up most: pointers, memory and OOP.
Get the book — $27Cause 3: A for Loop Without Brackets
#include <iostream>
int main() {
for int i = 0; i < 3; i++ {
std::cout << i << '\n';
}
}
Visual Studio editor: expected a '('
MSVC build: error C2062: type 'int' unexpected
GCC: error: expected '(' before 'int'
error: expected ')' before '{' token
Clang: error: expected '(' after 'for'
error: use of undeclared identifier 'i'
All three parts of a for loop go inside one pair of brackets, separated by semicolons:
#include <iostream>
int main() {
for (int i = 0; i < 3; i++) {
std::cout << i << '\n';
}
}
0
1
2
C++ has no for i in range(3). The three-part loop above is the equivalent; see the loops tutorial and range-based for loops for looping over a container.
Cause 4: A switch Without Brackets
#include <iostream>
int main() {
int choice = 1;
switch choice {
case 1:
std::cout << "One\n";
}
}
Visual Studio editor: expected a '('
MSVC build: error C2061: syntax error: identifier 'choice'
GCC: error: expected '(' before 'choice'
Clang: error: expected '(' after 'switch'
Fix: switch (choice) {, which prints One. More in the switch statement guide.
Why the Other Errors Appear
After a missing (, the compiler can’t tell where the condition ends, so the lines after it are misread too. In the examples above, Visual Studio’s editor adds parsing restarts here after previous syntax error further down, and other compilers add errors about ) or ;. Fix the first error and recompile; the rest usually go away.
Quick Reference
| You wrote | Fix |
|---|---|
if x > 5 { | if (x > 5) { |
else if x > 3 { | else if (x > 3) { |
while x < 3 { | while (x < 3) { |
for int i = 0; i < 3; i++ { | for (int i = 0; i < 3; i++) { |
switch choice { | switch (choice) { |
For other common messages, see C++ error messages explained.
Take Your C++ Further

If you'd like the fundamentals explained in one place, C++ Better Explained covers them in 87 plain-English pages, with analogies and code diagrams, including the parts that trip beginners up most: pointers, memory and OOP.
Get the book — $2787 pages · plain English · 30-day money-back guarantee
Related Articles
- C++ Error Messages Explained — the most common errors and what they mean.
- How to Fix
expected a ')'— when the closing bracket is the one missing. - C++ Conditionals Tutorial — if, else if and else.
- C++ Loops Tutorial — for, while and do-while.
- C++ vs Python — the differences that trip up Python programmers.