C Programming for Beginners: 15-Day Hands-On Practical Course
C PROGRAMMING · DAY 2

Variables, Data Types and Input/Output

Learn how C stores numbers and text in variables, picks the right data type, and talks to the user with printf() and scanf(). Then build your student information and calculator programs.

Code on a screen

4-Hour Lesson Plan

TimeSegmentContentHands-on
0:00Recap and hookA variable is a named box in memory. What happens when you type the wrong type in?Variables (memory view) in the playground
0:20Data typesint, float, double, char: size, range, specifierType Lab
1:00Names and constantsNaming rules, const, #defineName checker
1:30printf and scanfFormat specifiers, width, precision, the & operatorFormat Specifier Lab
2:15Build labStudent information, total and average, temperature, rectangle, calculator5 programs in the playground
3:15ErrorsMissing &, wrong specifier, integer division, overflowError and pitfall examples
3:40CheckQuiz and checklistQuick quiz

Core Ideas

📦

Variable

A named place in memory that holds a value, like int age = 20;. The declaration tells C the type and the name.

🔢

Data types

The data type decides what a variable can hold and how many bytes it uses: int, float, double, char.

🔒

Constants

A constant never changes. Use const for a protected variable or #define for a named value.

🎯

Format specifier

The % code in printf and scanf that says what type of value is being printed or read, like %d or %f.

⌨️

Input and output

printf() shows output. scanf() reads input and needs the address operator & before the variable.

Data Types at a Glance

TypeHoldsTypical sizeRange or precisionprintfscanfExample
intWhole numbers4 bytes-2,147,483,648 to 2,147,483,647%d%dint age = 20;
floatDecimals4 bytesAbout 6-7 digits%f%ffloat marks = 87.5;
doublePrecise decimals8 bytesAbout 15-16 digits%lf%lfdouble pi = 3.14159;
charOne character1 byte-128 to 127 (ASCII code)%c%cchar grade = 'A';
char name[30]Text (string)30 bytesUp to 29 characters plus end mark%s%schar name[30];

Sizes are typical on common computers. Use sizeof(int) in a program to check yours.

Lab 1: Type Lab

Choose a type and type a value. See what C really stores and what printf shows. Try 3.99 as an int, 0.1 as a float, or A as a char.

Lab 2: Variable Names and Constants

Name checker

const#define
Exampleconst float PI = 3.14;#define PI 3.14
Has a typeYesNo (text replacement)
Ends with ;YesNo
Uses = signYesNo
WhereInside or outside main()Usually at the top
Change laterErrorError

Format Specifier Table

CodeMeaningExample valueOutput
%dint4242
%f / %.2ffloat, 6 or 2 decimals3.141593.141590 / 3.14
%lf / %.2lfdouble3.141593.141590 / 3.14
%cchar'A'A
%sstring"Riya"Riya
%5d / %-5dwidth 5, right / left42   42 / 42   
%zuresult of sizeofsizeof(int)4
%%a percent sign%

Lab 3: Format Specifier Lab

Type a format string and the values (separated by commas, text in double quotes). The | bars show the exact width. Try %-8s|%5d|%.1f with "Riya", 42, 3.14159.

Lab 4: Calculators (check your answers)

Pick a calculator, change the numbers, and compare the results with what your C program prints.

Your Programs (Deliverable)

Each program shows the code, a sample input and the expected output. Download the file, compile it on your computer with gcc -Wall file.c -o file, and run it with ./file (Windows: file.exe).

Live C Playground: Auto-Run, Errors and Fixes

Every C program must start at main(). Type code below: it is colour-highlighted, checked and run as you type. Supports printf, scanf, int/float/double/char, const, #define, sizeof and basic maths. Use the Examples menu for the five programs and the error demos. Turn on Smart suggestions for autocomplete, and use Format code to tidy any code into a clean structure.

Terminal

When the program asks for input, type in the terminal and press Enter.

Variables (memory view)

NameTypeValueBytes

Problems and suggestions

Common Input and Format Errors

MistakeWhat happensFix
scanf("%d", age);Missing &: the program may crash or store nothingWrite &age
printf("%d", price); with a floatWrong or garbage numberUse %f or %.2f
scanf("%f", &d); with a doubleWrong value storedUse %lf for double
total / 3 with intsDecimal part is lost (7 / 2 gives 3)Use total / 3.0
Using a variable before setting itRandom garbage valueGive it a value or read it with scanf first
char g = "A";Double quotes make text, not a charUse single quotes: 'A'
#define PI 3.14;The ; becomes part of the valueNo semicolon after #define
scanf("%s", name) with spacesReads only the first wordUse one word, or fgets later
Number too large for intValue wraps around (overflow)Use long

Deliverable Checklist

    Quick Quiz

    0% Complete