C
C Data Types, Variables and Constants
Data Types

Data Types in C

A data type tells C what kind of value is being stored and how that value should be interpreted.

Which declaration is suitable for storing a student's age?
Begin with one sentence: “A variable is a named memory location; a data type tells what may be stored there.”
int
age
18

Type → Name → Value

Why data types matter

Every value needs meaning

1

Kind of value

Integer, character, decimal value, or another form of data.

int marks = 90;
2

Memory requirement

The implementation chooses storage according to the type.

sizeof(int)
3

Valid operations

The type influences which operations and interpretations are meaningful.

marks + 5
student_data.cDifferent types
int marks = 90; char grade = 'A'; float percentage = 87.5f;
Avoid saying that C guarantees the same size for every type on every system. Emphasize that sizeof checks the current implementation.
Classification

Major categories of C data types

Basic

Built-in types

  • char
  • int
  • float
  • double
  • void
Derived

Built from other types

  • Array
  • Pointer
  • Function type
User-defined

Created for a program

  • struct
  • union
  • typedef name
Enumerated

Named integer constants

  • enum Day
  • enum State
  • enum TrafficLight
Explain that enum is commonly treated as a user-defined type, but it is highlighted separately here because the syllabus lists it separately.
Basic data types

Explore the built-in types

char

Stores a character value such as 'A'. It is also an integer type in C.

Example
char grade = 'A';
Typical output
A
printf specifier
%c
Use
Character
Mention that exact ranges and sizes are implementation-dependent. Keep the focus on purpose and syntax.
Input and output

Format specifiers connect values with printf and scanf

Data typeprintf()scanf()
char%c%c
int%d%d
unsigned int%u%u
short int%hd%hd
long int%ld%ld
long long int%lld%lld
float%f%f
double%f%lf
char name[]%s%s

Choose the correct specifier

double percentage;

For scanf()

Select an answer.
Emphasize the common confusion: printf uses %f for both float and double arguments, while scanf uses %f for float and %lf for double.
Formatted output

Width and precision control how values appear

Formatting controls

Generated printf format
printf("%8.2f", value);
Displayed output
87.46
Show that %.2f controls digits after the decimal point, while %8.2f adds a minimum field width. For integers, %05d pads with zeros.
Integer range

Range depends on the number of bits

Unsigned integer

Minimum = 0
Maximum = 2n − 1
All bit patterns represent non-negative values.

Signed two's-complement integer

Minimum = −2n−1
Maximum = 2n−1 − 1
This is the representation used by modern C implementations.
BitsUnsigned rangeSigned range
40 to 15−8 to 7
80 to 255−128 to 127
160 to 65,535−32,768 to 32,767
320 to 4,294,967,295−2,147,483,648 to 2,147,483,647
The C standard does not require int to have the same number of bits on every implementation.
Derive the 8-bit examples on the board: unsigned 0 to 2^8−1; signed −2^7 to 2^7−1.
Range calculator

Calculate signed and unsigned limits

Choose the representation

Students calculate first, then click the result panel.

8-bit signed integer
Click to reveal the range and steps
Keep the result hidden until students calculate it. For 64-bit values, the page uses exact BigInt arithmetic.
Actual implementation limits

Check the current system with sizeof and limits.h

integer_limits.cImplementation values
#include <stdio.h> #include <limits.h> int main(void) { printf("Bytes in int = %zu\n", sizeof(int)); printf("Bits in int = %zu\n", sizeof(int) * CHAR_BIT); printf("INT_MIN = %d\n", INT_MIN); printf("INT_MAX = %d\n", INT_MAX); printf("UINT_MAX = %u\n", UINT_MAX); return 0; }
sizeof(int)
Returns the size of int in bytes.
CHAR_BIT
Number of bits in one C byte.
INT_MIN
Smallest value supported by int.
INT_MAX
Largest value supported by int.
UINT_MAX
Largest value supported by unsigned int.
A common system has CHAR_BIT = 8 and sizeof(int) = 4, giving 32 value bits in an unsigned int. Treat that as an example, not a universal rule.
Type modifiers

Changing the range or form of integer types

S

short

Requests an integer type no wider than ordinary int.

short int year;
L

long

Requests an integer type with at least the range of int.

long int population;
±

signed / unsigned

Unsigned integer types represent only non-negative values.

unsigned int count;
Checking sizeCurrent system
printf("%zu", sizeof(int));
Do not make students memorize fixed byte sizes. Explain that sizeof reports the size used by the current implementation.
Derived data types

Types built using other types

[ ]

Array

Stores a fixed number of elements of the same type.

int marks[5];
*

Pointer

Stores an address associated with an object or function type.

int *ptr;
f()

Function type

Describes a function's return type and parameter types.

int add(int, int);
This is only an introduction. Arrays, pointers, and functions should be studied in detail in their own lectures.
User-defined and enumerated types

Giving meaningful structure and names to data

enum_demo.cNamed integer constants
enum TrafficLight { RED, YELLOW, GREEN }; enum TrafficLight signal = GREEN;

Traffic light value

Select a named value.

GREEN → 2
struct Student { int rollNo; char grade; };
By default, the first enum constant has value 0 and the following constants increase by 1 unless explicitly assigned otherwise.
Implementation

Using basic data types in one program

student_profile.cBasic data types
#include <stdio.h> int main(void) { int age = 18; float height = 5.8f; char grade = 'A'; double percentage = 87.456; printf("Age = %d\n", age); printf("Height = %.1f\n", height); printf("Grade = %c\n", grade); printf("Percentage = %.2f\n", percentage); return 0; }
This program connects data types with variables and constants. Ask students to identify the type, variable name, and initial value in each declaration.

Variables and Constants

A variable may store a value that changes. A constant represents a value that should not change.

Core idea
int score = 10; const int MAX_SCORE = 100;
Variable lifecycle

Declaration, initialization and assignment

Declaration
int age;

Introduces the variable name and its data type.

Initialization
int age = 18;

Gives the variable its first value when it is created.

Assignment
age = 20;

Stores a new value in an existing modifiable variable.

This distinction is important. Ask students to identify which line creates the variable and which line changes it later.
Variable value changes

Follow the value stored in memory

int
score
10
The variable is initialized with 10.
Pause before every click and ask students to predict the next stored value.
Identifier rules

Valid names for variables

Check a variable name

student_age is a valid identifier.

Main rules

  • Begin with a letter or underscore
  • Do not begin with a digit
  • No spaces
  • No symbols except underscore
  • Do not use a C keyword
  • C is case-sensitive

Valid

age student_age marks1 _total

Invalid

1marks student age float total-marks

Case-sensitive

age Age AGE
Explain that age, Age and AGE are different identifiers in C.
Constant values

Common forms of constants

Integer constant

A whole-number literal.

25

Floating constant

A numeric literal with a fractional form.

3.14

Character constant

One character written in single quotes.

'A'

String literal

A sequence of characters in double quotes.

"Hello"
Examples
int age = 18; float pi = 3.14f; char grade = 'A'; char message[] = "Hello";
Distinguish a character constant in single quotes from a string literal in double quotes.
Symbolic constants

const and #define

const object

C language
const float PI = 3.14159f;
  • Has a C data type
  • Creates an object that should not be modified
  • Follows C scope rules

#define macro

Preprocessor
#define PI 3.14159
  • Handled before normal compilation
  • Performs macro replacement
  • A simple object-like macro has no C data type
Connect this slide with the previous preprocessor lecture. Keep the distinction simple: const is typed C code; #define is a macro.
Complete implementation

Data types, variables and constants together

marks_report.cIntegrated example
#include <stdio.h> #define MAX_MARKS 100 int main(void) { const float PASS_PERCENTAGE = 40.0f; int marks = 75; float percentage; percentage = (marks * 100.0f) / MAX_MARKS; printf("Marks = %d\n", marks); printf("Maximum Marks = %d\n", MAX_MARKS); printf("Percentage = %.2f\n", percentage); printf("Pass Requirement = %.2f\n", PASS_PERCENTAGE); return 0; }
Ask students to locate one data type, one variable, one initialization, one assignment, one const object, and one macro.
Quick assessment

Check both lectures

Question 1 of 8
Ask students to answer aloud before selecting an option.
Final recap

Three ideas to remember

T

Data type

Explains what kind of value is stored and how C interprets it.

V

Variable

A named object whose stored value may change during execution.

C

Constant

A value or symbolic form intended not to change.

Remember
int age = 18; const int MAX_AGE = 100;