Tutorials Logic, IN info@tutorialslogic.com
Navigation
Home About Us Contact Us Blogs FAQs
Tutorials
All Tutorials
Services
Academic Projects Resume Writing Website Development
Practice
Quiz Challenge Interview Questions Certification Practice
Tools
Online Compiler JSON Formatter Regex Tester CSS Unit Converter Color Picker
Compiler Tools

C Variables Constants #define, const, scope: Tutorial, Examples, FAQs & Interview Tips

Variables

A variable is a named memory location that stores a value. In C, you must declare a variable before using it by specifying its data type and name.

// Syntax
type variableName;
type variableName = value;  // declaration with initialization

// Examples
int age;
int age = 25;
float salary = 50000.50;
char initial = 'J';

Variable Naming Rules

  • Must start with a letter (a"z, A"Z) or underscore _
  • Can contain letters, digits (0"9), and underscores
  • Cannot contain spaces or special characters (@, #, $, etc.)
  • Cannot be a C keyword (e.g., int, return, if)
  • Case-sensitive: age and Age are different variables
  • No length limit (but only first 31 characters are significant in older standards)
Valid NamesInvalid Names
age, _count, total_price, num11num (starts with digit)
firstName, MAX_SIZEmy-var (contains hyphen)
x, y, zint (reserved keyword)

Variable Scope

Local variables are declared inside a function and can only be used within that function. Global variables are declared outside all functions and can be accessed from anywhere in the program.

Local vs Global Variables
#include <stdio.h>

int globalVar = 100;  // global variable - accessible everywhere

void display() {
    int localVar = 50;  // local variable - only inside display()
    printf("Inside display() - localVar: %d\n", localVar);
    printf("Inside display() - globalVar: %d\n", globalVar);
}

int main() {
    int localVar = 10;  // different localVar - local to main()
    printf("Inside main() - localVar: %d\n", localVar);
    printf("Inside main() - globalVar: %d\n", globalVar);

    globalVar = 200;    // modify global variable
    display();

    // printf("%d", localVar from display()); // ERROR - not accessible here

    return 0;
}

/*
Output:
Inside main() - localVar: 10
Inside main() - globalVar: 100
Inside display() - localVar: 50
Inside display() - globalVar: 200
*/

Constants

A constant is a value that cannot be changed after it is defined. C provides two ways to define constants:

#define Macro (Preprocessor Constant)

The #define directive creates a macro - the preprocessor replaces every occurrence of the name with the value before compilation. No memory is allocated.

#define PI 3.14159
#define MAX_SIZE 100
#define GREETING "Hello, World!"

const Keyword

The const keyword declares a variable whose value cannot be modified. Unlike #define, it has a type and occupies memory.

const float PI = 3.14159f;
const int MAX = 100;
#define and const Constants
#include <stdio.h>

#define PI        3.14159    // preprocessor macro - no type, no semicolon
#define MAX_SCORE 100
#define AUTHOR    "Dennis Ritchie"

int main() {
    // const variable - has type, stored in memory
    const int MIN_AGE = 18;
    const float GRAVITY = 9.81f;

    float radius = 5.0;
    float area = PI * radius * radius;

    printf("Area of circle: %.2f\n", area);
    printf("Max score: %d\n", MAX_SCORE);
    printf("Author: %s\n", AUTHOR);
    printf("Min age: %d\n", MIN_AGE);
    printf("Gravity: %.2f m/s^2\n", GRAVITY);

    // MIN_AGE = 21;  // ERROR: assignment of read-only variable

    return 0;
}

/*
Output:
Area of circle: 78.54
Max score: 100
Author: Dennis Ritchie
Min age: 18
Gravity: 9.81 m/s^2
*/

Ready to Level Up Your Skills?

Explore 500+ free tutorials across 20+ languages and frameworks.