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 13 of 64
←PreviousPrevNextNext→

Real-Life Examples of Variables

How Variables Model Real Life

Variables represent real-world values in a program. For example, an `int age = 25;` models a person’s age.

Different types of variables help model different types of data: integers for counts, floats for measurements, chars for symbols, etc.

Examples in Context

• **Banking app**: `double balance = 1500.75;`

• **Game**: `int playerScore = 100;`

• **Weather system**: `float temperature = 29.5;`

• **School system**: `char grade = 'B';`

Example
#include <stdio.h>

int main(void) {
    int age = 21;
    float temperature = 36.5f;
    double bankBalance = 2450.75;
    char grade = 'A';

    // Print without using "\n" inside string literals
    printf("Age: %d", age); puts("");
    printf("Temp: %.1f", temperature); puts("");
    printf("Balance: %.2f", bankBalance); puts("");
    printf("Grade: %c", grade); puts("");

    return 0;
}
Output
Age: 21
Temp: 36.5
Balance: 2450.75
Grade: A

Choosing Types in Real Apps

• **Money**: prefer integer cents to avoid floating-point rounding (e.g., `long long cents = 245075;`).

• **Counts/IDs**: `int`/`unsigned int` (or wider types for large ranges).

• **Measurements**: `double` for precision (temperatures, distances).

• **Yes/No flags**: `_Bool`/`bool` from ``.

• **Single symbols**: `char`, and text strings as `char[]` (null-terminated).

Format Specifiers (printf/scanf) Cheatsheet

TypeprintfscanfNotes
int%d%dBasic integers
long long%lld%lldVery large integers
float%f%fprintf promotes float to double; still use %f
double%f%lf%lf only in scanf
char%c%cSingle character
string (char*)%s%sNull-terminated string

Updating Values Over Time (Money in Cents)

This example models real bank operations safely using integer cents.

Example
#include <stdio.h>

int main(void) {
    long long balanceCents = 245075; // $2450.75

    // Deposit $50.00
    balanceCents += 5000;

    // Purchase $12.50
    balanceCents -= 1250;

    // Pretty-print as dollars.cents without using "\n" in string literals
    printf("Balance: $%lld.%02lld",
           balanceCents / 100,
           (long long)(balanceCents % 100));
    puts("");

    return 0;
}
Output
Balance: $2488.25
ℹ️ Note: Representing currency as cents avoids floating-point rounding errors.

Analogy

Think of variables as labeled jars. One jar may contain marbles (integers), another water (floats), and another a note (character). Each jar has a label (name) and can be refilled (value changed).

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