C Keywords & Identifiers
type identifier = value; // identifier: letters, digits, _ (no leading digit)
int count = 10; // int is a reserved keyword
Keywords
Keywords are words the C language reserves for its own syntax -- int, return, if, while, and about 30 others -- and the compiler will reject any attempt to use one of them as a variable or function name.
Example: Keywords
// Include standard input/output (printf, scanf)
#include <stdio.h>
// Program execution starts in main()
int main() {
// Declare x and set it to 5
int x = 5;
// Check whether x
if (x) {
// Send 0 back to the caller
return 0;
}
// Send 1 back to the caller
return 1;
}
Login to try C/C++/Java/PHP code in the editor
Identifiers
An identifier is any name you choose for a variable, function, or array in your own code. Picking clear identifiers (like totalPrice instead of x) makes your program far easier to read months later or for someone else reviewing it.
Example: Identifiers
// Include standard input/output (printf, scanf)
#include <stdio.h>
// Program execution starts in main()
int main() {
// Declare totalPrice and set it to 50
int totalPrice = 50;
// Print formatted text to the screen
printf("%d", totalPrice);
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Naming Rules
C requires every identifier to start with a letter or underscore, and to contain only letters, digits, and underscores after that -- names like 2total or total-price will fail to compile because they break these rules.
Example: Naming Rules
// Include standard input/output (printf, scanf)
#include <stdio.h>
// Program execution starts in main()
int main() {
// Declare _count and set it to 1
int _count = 1;
// Declare total2 and set it to 2
int total2 = 2;
// Print formatted text to the screen
printf("%d %d", _count, total2);
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Case Sensitivity in Names
Because C treats case as significant, count and Count are two distinct identifiers that could coexist in the same scope -- a frequent source of subtle bugs when a typo accidentally introduces a second, unintended variable.
Example: Case Sensitivity in Names
// Include standard input/output (printf, scanf)
#include <stdio.h>
// Program execution starts in main()
int main() {
// Declare count and set it to 1
int count = 1;
// Declare Count and set it to 2
int Count = 2;
// Print formatted text to the screen
printf("%d %d", count, Count);
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Best Practices for Names
Favor descriptive multi-word names like studentAge over cryptic single letters, except for very short-lived loop counters like i, where the convention is well understood and brevity doesn't hurt readability.
Example: Best Practices for Names
// Include standard input/output (printf, scanf)
#include <stdio.h>
// Program execution starts in main()
int main() {
// Declare studentAge and set it to 20
int studentAge = 20;
// Loop: repeat while the condition holds
for (int i = 0; i < 3; i++) {
// Print formatted text to the screen
printf("%d ", studentAge);
}
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
- Using a keyword such as
int for = 5;as a variable name, which is a compile error. - Starting an identifier with a digit like
1stValue, which is not allowed. - Mixing up
totalandTotal, since identifiers are case-sensitive and are different names.
Chapter Quiz — Complete all 21 topics to unlock
0/21 topics done
Complete these topics first:
- C Introduction
- C History & Features
- C Environment Setup
- C First Program
- C Syntax & Structure
- C Statements
- C Comments
- C Keywords & Identifiers
- C Data Types
- C Character Data Type
- C Numeric Data Types
- C Decimal (Floating-Point) Numbers
- C sizeof Operator
- C Extended Data Types
- C Type Conversion
- C Booleans
- C Variables
- C Changing Variable Values
- C Multiple Variables
- C Constants
- C Fixed-Width Integers