DevAcademia
C++C#CPythonJava
  • C Basics

  • Introduction to C
  • Getting Started with C
  • C Syntax
  • C Output
  • C Comments
  • C Variables
  • C Data Types
  • C Constants
  • C Operators
  • C Booleans
  • C If...Else Statements
  • C Switch Statement
  • C While Loops
  • C For Loops
  • C Break and Continue
  • C Strings
  • C User Input
  • C Memory Address
  • C Pointers
  • C Files
  • C Functions

  • C Functions
  • C Function Parameters
  • C Scope
  • C Function Declaration
  • C Recursion
  • C Math Functions
  • C Structures

  • C Structures
  • C Structs & Pointers
  • C Unions
  • C Enums

  • C Enums
  • C Memory

  • C Allocate Memory
  • C Access Memory
  • C Reallocate Memory
  • C Deallocate Memory
  • C Structs and Memory
  • C Memory Example
  • C Quiz

  • C Quiz
  • C Basics

  • Introduction to C
  • Getting Started with C
  • C Syntax
  • C Output
  • C Comments
  • C Variables
  • C Data Types
  • C Constants
  • C Operators
  • C Booleans
  • C If...Else Statements
  • C Switch Statement
  • C While Loops
  • C For Loops
  • C Break and Continue
  • C Strings
  • C User Input
  • C Memory Address
  • C Pointers
  • C Files
  • C Functions

  • C Functions
  • C Function Parameters
  • C Scope
  • C Function Declaration
  • C Recursion
  • C Math Functions
  • C Structures

  • C Structures
  • C Structs & Pointers
  • C Unions
  • C Enums

  • C Enums
  • C Memory

  • C Allocate Memory
  • C Access Memory
  • C Reallocate Memory
  • C Deallocate Memory
  • C Structs and Memory
  • C Memory Example
  • C Quiz

  • C Quiz

Loading C tutorial…

Loading content
C BasicsTopic 8 of 64
←PreviousPrevNextNext→

Create Variables in C

What is a Variable?

A variable is a named storage location in the computer’s memory that holds a value. In C, every variable has a type that determines the size of memory it uses and the operations that can be performed on it.

Variables allow programs to store, retrieve, and manipulate data dynamically during execution.

Type sizes are implementation-defined. The common sizes listed below are typical on many modern systems, but always prefer `sizeof(type)` to know the exact size on your platform.

ℹ️ Note: On some systems `int` can be 2 or 8 bytes; `char` is always 1 byte, but a byte is at least 8 bits.
TypeDescriptionMemory Size (Typical)Example
intStores whole numbers4 bytesint age = 25;
floatStores decimal numbers (single precision)4 bytesfloat pi = 3.14f;
doubleStores larger decimal numbers (double precision)8 bytesdouble g = 9.81;
charStores a single character (code unit)1 bytechar grade = 'A';

Declaring vs. Initializing

• **Declaration**: Tells the compiler to reserve memory for a variable of a specific type. Example: `int x;`

• **Initialization**: Assigns an initial value *at the moment of declaration*. Example: `int y = 10;`

If a variable is declared but not initialized, it holds an **indeterminate** value. Reading that value before assigning something is **undefined behavior**.

Example
#include <stdio.h>

int main(void) {
    int age;          // declaration only (uninitialized)
    age = 20;         // assignment after declaration

    int count = 10;   // declaration with initialization

    printf("Age (after assignment): %d\n", age);
    printf("Count (initialized): %d\n", count);
    return 0;
}
Output
Age (after assignment): 20
Count (initialized): 10
ℹ️ Note: Initialization happens at declaration; assigning later is not initialization, it’s just assignment.

Checking Sizes with sizeof

Use the `sizeof` operator to check the exact size of each type on your platform.

Example
#include <stdio.h>

int main(void) {
    printf("sizeof(int)   = %zu\n", sizeof(int));
    printf("sizeof(float) = %zu\n", sizeof(float));
    printf("sizeof(double)= %zu\n", sizeof(double));
    printf("sizeof(char)  = %zu\n", sizeof(char));
    return 0;
}

Constants vs Variables

Variables can change their values during program execution. Constants, defined using `const`, cannot.

Example: `const float PI = 3.14159f;` (attempting to assign `PI = 3.2f;` would be a compile-time error).

`const` gives type safety and scope; remember that `const` variables are not compile-time integer constants in standard C (use `enum` or `#define` where a compile-time constant is required).

Test your knowledge: Create Variables in C
Quiz Configuration
4 of 8 questions
Sequential
Previous allowed
Review enabled
Early close allowed
Estimated time: 5 min
C BasicsTopic 8 of 64
←PreviousPrevNextNext→