C Variables

Beginner
6 min read
Updated: Nov 2026
1 example

What Is Variables?

A variable is a named place in memory that holds a value of a given type. Declare it with a type, give it an initial value, and read or change it later. Local variables are not initialized automatically.

Remember
int age;              /* declaration */
age = 30;             /* assignment */
int year = 2026;      /* declaration + initialization */
int a = 1, b = 2;     /* several at once */

Cheat Sheet

Keep these facts handy while you work with Variables in C.

TopicDetail
Declaretype name;
Initializetype name = value;
ScopeLocal to the enclosing { } block
Default valueLocals are indeterminate; globals and statics start at 0

C Examples

Compile each example with gcc file.c -std=c11 -Wall -o out. Next: Constants.

Example 1 — Declare, assign, and update

Track a score as the program runs.

C
#include <stdio.h>

int main(void) {
    int score = 10;
    printf("start: %d\n", score);

    score = score + 5;
    printf("after bonus: %d\n", score);

    score *= 2;
    printf("doubled: %d\n", score);
    return 0;
}

How It Works

  1. score is initialized to 10 when declared.
  2. score = score + 5 reads the old value and stores the new one.
  3. score *= 2 is shorthand for score = score * 2.

Edge Cases & Pitfalls

Check these before relying on Variables in real programs.

init

Reading before assigning

int n; printf("%d", n); reads an indeterminate value. Initialize locals when you declare them.

type

Wrong type for the data

Storing 3.9 in an int truncates it to 3. Use double for fractions.

Key Takeaways

  • Declare: type first, then the name.
  • Initialize: do not rely on garbage values.
  • Update: assignment overwrites the old value.

One line: A variable is a named place in memory that holds a value of a given type.

Frequently Asked Questions

For ordinary local variables they are the same step: the declaration reserves storage. For globals, extern int x; only declares while int x; defines.
Since C99 you may declare variables after statements. In C89 all declarations had to come at the start of a block.
Continue with "C Constants" next. Values that must not change: literals, #define, const, and enum. The C Fundamentals hub lists every topic in order.

Next: Constants

Values that must not change: literals, #define, const, and enum.

Continue →

CODETOFUN NEWS

Stay Ahead in Tech

Get the latest IT news, programming updates, and technology trends directly on your phone.

Your daily dose of tech, straight to your feed.

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

12 people found this page helpful