🧭 Navigation
⬅️ Previous | 🏠 Home | ➡️ Next
Identifiers are names given to various program elements such as variables, functions, arrays, etc.
They help uniquely identify these elements in your code.
- Can contain letters (A-Z, a-z), digits (0-9), and underscores (_)
- Must begin with a letter or underscore
- Cannot be a reserved keyword (e.g.,
int,return) - Case-sensitive (
Countandcountare different)
total_valuesum1student_name
1value(cannot start with a digit)float(reserved keyword)my-value(hyphen not allowed)
Variables are named memory locations used to store data that can change during program execution.
-
Local Variables
- Declared inside a function or block
- Accessible only within that function/block
- Example:
void func() { int x = 10; // local variable }
-
Global Variables
- Declared outside all functions
- Accessible from any function in the file
- Example:
int count = 0; // global variable void func() { count++; }
-
Static Variables
- Declared with
statickeyword - Retain their value between function calls
- Example:
void func() { static int num = 0; num++; printf("%d", num); }
- Declared with
-
Extern Variables
- Declared with
externkeyword - Used to access variables defined in other files
- Example:
extern int shared;
- Declared with
Variables can change their value during program execution, while constants have fixed values that do not change.
- Use the
constkeyword or#definepreprocessor directive.
int age = 25; // variable: value can change
age = 30; // valid
const float PI = 3.14; // constant: value cannot change
// PI = 3.1415; // invalid, will cause a compile error
#define MAX_SIZE 100 // constant using preprocessor#include <stdio.h>
int globalVar = 100; // global variable
void demo() {
int localVar = 10; // local variable
static int staticVar = 0; // static variable
staticVar++;
printf("Local: %d, Static: %d, Global: %d\n", localVar, staticVar, globalVar);
}
int main() {
demo();
demo();
const int daysInWeek = 7; // constant
int x = 5; // variable
x = 10; // valid
// daysInWeek = 8; // invalid, will cause error
printf("Days in a week: %d\n", daysInWeek);
printf("x: %d\n", x);
return 0;
}- Use local variables for temporary data within functions.
- Use global variables for data shared across multiple functions.
- Use static variables to preserve state between function calls.
- Use extern variables for sharing data between different source files.
- Use constants for values that should not change (like mathematical constants, array sizes).
Identifiers, variables, and constants are fundamental for organizing and managing data in your
| ⬅️ Previous | 🏠 Home | ➡️ Next |
|---|---|---|
| Tokens | README | Data Types |
🧭 Navigation
⬅️ Previous | 🏠 Home | ➡️ Next