Progress
0%
Practice & Concepts

Geometry, Type Conversion & sizeof

Simple practice programs for perimeter and area, plus a complete explanation of how C converts between data types and measures memory.

Perimeter Programs
Area Programs
Implicit Conversion
Explicit Conversion
sizeof Operator
1

Perimeter Programs — int & float

Geometry basics

These three programs use the simplest possible formulas to practise int and float inputs. Perimeter just means the total distance around the outside of a shape.

  • Square: all 4 sides equal → perimeter = 4 × side
  • Rectangle: 2 lengths + 2 widths → perimeter = 2 × (length + width)
  • Triangle: add all 3 sides → perimeter = a + b + c
Perimeter of Square — using int
perimeter_square.c
C
#include <stdio.h>

int main() {
    int side;
    int perimeter;

    printf("Enter side of square (cm): ");
    scanf("%d", &side);

    perimeter = 4 * side;   // formula: 4 × side

    printf("Perimeter of square = %d cm\n", perimeter);

    return 0;
}
terminal
output
Enter side of square (cm): 7
Perimeter of square = 28 cm
Perimeter of Rectangle — using float
perimeter_rectangle.c
C
#include <stdio.h>

int main() {
    float length, width;
    float perimeter;

    printf("Enter length (cm): ");
    scanf("%f", &length);

    printf("Enter width  (cm): ");
    scanf("%f", &width);

    perimeter = 2 * (length + width);  // formula: 2×(l+w)

    printf("Perimeter of rectangle = %.2f cm\n", perimeter);

    return 0;
}
terminal
output
Enter length (cm): 8.5
Enter width  (cm): 4.2
Perimeter of rectangle = 25.40 cm
Perimeter of Triangle — three float sides
perimeter_triangle.c
C
#include <stdio.h>

int main() {
    float a, b, c;
    float perimeter;

    printf("Enter side a: ");
    scanf("%f", &a);
    printf("Enter side b: ");
    scanf("%f", &b);
    printf("Enter side c: ");
    scanf("%f", &c);

    perimeter = a + b + c;  // formula: a + b + c

    printf("Perimeter of triangle = %.2f cm\n", perimeter);

    return 0;
}
terminal
output
Enter side a: 3.0
Enter side b: 4.0
Enter side c: 5.0
Perimeter of triangle = 12.00 cm
💡 int vs float — when to use which:
Use int when the value will always be whole — number of sides, count of items, age.
Use float when the value might be decimal — measurements, prices, averages.
2

Area Programs — Triangle, Circle, Square

More geometry
Area of Triangle — ½ × base × height
area_triangle.c
C
#include <stdio.h>

int main() {
    float base, height, area;

    printf("Enter base   (cm): ");
    scanf("%f", &base);

    printf("Enter height (cm): ");
    scanf("%f", &height);

    area = 0.5 * base * height;   // formula: ½ × b × h

    printf("Area of triangle = %.2f sq cm\n", area);

    return 0;
}
terminal
output
Enter base   (cm): 10
Enter height (cm): 6
Area of triangle = 30.00 sq cm
Area of Square — side × side
area_square.c
C
#include <stdio.h>

int main() {
    int side, area;

    printf("Enter side of square (cm): ");
    scanf("%d", &side);

    area = side * side;   // formula: side²

    printf("Area of square = %d sq cm\n", area);

    return 0;
}
terminal
output
Enter side of square (cm): 6
Area of square = 36 sq cm
Area of Circle — π × r²
area_circle.c
C
#include <stdio.h>

int main() {
    float radius, area;
    float pi = 3.14159;

    printf("Enter radius (cm): ");
    scanf("%f", &radius);

    area = pi * radius * radius;  // formula: π × r²

    printf("Area of circle = %.2f sq cm\n", area);

    return 0;
}
terminal
output
Enter radius (cm): 7
Area of circle = 153.94 sq cm
ShapePerimeter FormulaArea FormulaType to use
Square4 × sideside × sideint or float
Rectangle2 × (l + w)l × wfloat
Trianglea + b + c0.5 × b × hfloat
Circle2 × π × rπ × r × rfloat/double
type conversion
3

Implicit Conversion — Automatic

C does it for you

Implicit conversion happens automatically when C assigns a value of one type to a variable of another type. C always converts to the larger or more precise type — so no data is lost going upward (int → float → double).

Going downward (double → int) also happens automatically but data IS lost — the decimal part is simply dropped without any warning.

Type promotion ladder — C promotes upward automatically (safe)

char
1 byte
int
4 bytes
float
4 bytes
double
8 bytes
← going this way is SAFE — no data loss
double
float
int
char
← going this way LOSES data ⚠️
implicit_conversion.c
C
#include <stdio.h>

int main() {

    // ── Safe: int stored in float (no data loss) ──
    float myFloat = 9;          // int 9 → float 9.0 automatically
    printf("int 9 as float:    %f\n", myFloat);   // 9.000000

    // ── Safe: int math in expression with float ──
    float result = 5 + 2.5;     // int 5 → float 5.0, then +2.5
    printf("5 + 2.5 as float:  %f\n", result);    // 7.500000

    // ── Unsafe: float stored in int (decimal LOST) ──
    int truncated = 9.99;        // 9.99 → 9, decimal dropped!
    printf("9.99 as int:       %d\n", truncated);  // 9

    // ── Integer division — both operands int ──
    int x = 5, y = 2;
    int sum = x / y;             // 5/2 = 2, decimal dropped!
    printf("5 / 2 as int:      %d\n", sum);        // 2

    // ── Mixed: int + float = float result ──
    float mixed = x + 2.5;      // x(5) promoted to float first
    printf("5 + 2.5 mixed:     %f\n", mixed);     // 7.500000

    return 0;
}
terminal
output
int 9 as float:    9.000000
5 + 2.5 as float:  7.500000
9.99 as int:       9           ← decimal LOST!
5 / 2 as int:      2           ← decimal LOST!
5 + 2.5 mixed:     7.500000
⚠️ Implicit int-to-int division is the most common bug!
int x=5, y=2; int sum = x/y; gives 2 not 2.5.
C sees two ints, does integer division, and stores 2. The .5 is silently discarded. No error. No warning.
4

Explicit Conversion — Casting

You tell C what to do

Explicit conversion (also called type casting) is when you manually tell C to treat a value as a different type. You write the target type in parentheses before the value: (float)x.

This is the solution to the integer division problem — cast one operand to float before dividing and C does proper decimal division.

explicit_cast.c
C
#include <stdio.h>

int main() {
    int x = 5, y = 2;

    // Without cast — integer division
    printf("Without cast: %d\n",   x / y);              // 2

    // With cast — (float) before x forces float math
    printf("With cast:    %.2f\n", (float)x / y);       // 2.50

    // Using 1.0 trick — multiply by 1.0 forces float
    printf("Using 1.0:    %.2f\n", x * 1.0 / y);        // 2.50

    // Cast double back to int
    double pi = 3.14159;
    int piInt = (int)pi;      // chops off decimal manually
    printf("(int) 3.14159 = %d\n", piInt);             // 3

    // Cast char to int — see ASCII value
    char letter = 'A';
    printf("'A' as int = %d\n",  (int)letter);         // 65

    return 0;
}
terminal
output
Without cast: 2
With cast:    2.50
Using 1.0:    2.50
(int) 3.14159 = 3
'A' as int = 65
ConversionTypeSyntaxResultData Lost?
int → floatImplicit or Explicitfloat f = 9;9.000000No
float → intImplicit (dangerous!)int n = 9.99;9Yes — decimal
int / intImplicit (int math)5 / 22Yes — decimal
(float)int / intExplicit cast(float)5 / 22.50No
char → intExplicit(int)'A'65 (ASCII)No
double → intExplicit(int)3.993Yes — decimal
💡 Simple rule for correct division:
Whenever you divide and want decimal result — cast at least one operand:
(float)a / b   OR   a / (float)b   OR   a * 1.0 / b
All three work. The cast only needs to be on one side.
sizeof operator
5

sizeof Operator — How Big is Each Type?

Memory sizes

sizeof() is a built-in C operator that returns the number of bytes a data type or variable uses in memory. It is not a function — it is evaluated by the compiler, not at runtime.

  • Returns type size_t — always print it with %zu (not %d)
  • Works on types: sizeof(int)
  • Works on variables: sizeof(myVar)
  • Works on expressions: sizeof(a + b)
  • Results may vary slightly between 32-bit and 64-bit systems
int
4
bytes = 32 bits
float
4
bytes = 32 bits
double
8
bytes = 64 bits
char
1
byte = 8 bits
sizeof_demo.c
C
#include <stdio.h>

int main() {

    // sizeof with type names
    printf("sizeof(int)         = %zu bytes\n", sizeof(int));
    printf("sizeof(float)       = %zu bytes\n", sizeof(float));
    printf("sizeof(double)      = %zu bytes\n", sizeof(double));
    printf("sizeof(char)        = %zu bytes\n", sizeof(char));
    printf("sizeof(long int)    = %zu bytes\n", sizeof(long int));
    printf("sizeof(long double) = %zu bytes\n", sizeof(long double));

    printf("\n");

    // sizeof with variables — same result
    int    myInt;
    float  myFloat;
    double myDouble;
    char   myChar;

    printf("sizeof(myInt)    = %zu bytes\n", sizeof(myInt));
    printf("sizeof(myFloat)  = %zu bytes\n", sizeof(myFloat));
    printf("sizeof(myDouble) = %zu bytes\n", sizeof(myDouble));
    printf("sizeof(myChar)   = %zu bytes\n", sizeof(myChar));

    printf("\n");

    // sizeof an array — total bytes used
    int arr[10];
    printf("sizeof(int arr[10]) = %zu bytes\n", sizeof(arr));  // 40
    printf("Number of elements  = %zu\n", sizeof(arr) / sizeof(arr[0]));  // 10

    return 0;
}
terminal
output
sizeof(int)         = 4 bytes
sizeof(float)       = 4 bytes
sizeof(double)      = 8 bytes
sizeof(char)        = 1 bytes
sizeof(long int)    = 8 bytes
sizeof(long double) = 16 bytes

sizeof(myInt)    = 4 bytes
sizeof(myFloat)  = 4 bytes
sizeof(myDouble) = 8 bytes
sizeof(myChar)   = 1 bytes

sizeof(int arr[10]) = 40 bytes
Number of elements  = 10

Why does sizeof(arr) = 40?

Because int arr[10] holds 10 integers, each 4 bytes → 10 × 4 = 40 bytes total.

The trick sizeof(arr) / sizeof(arr[0]) divides total bytes by bytes-per-element to get the count — 40 / 4 = 10 elements. This is the safest way to get array size in C.

💡 Why use %zu not %d for sizeof?
sizeof() returns type size_t — which is an unsigned long. Printing it with %d (signed int) works on most systems but can cause compiler warnings or wrong output on some systems. %zu is always correct and safe.
All together — conversion + sizeof in one program
all_together.c
C
#include <stdio.h>

int main() {
    int   a = 7, b = 2;
    float result;

    // Without cast — wrong answer
    printf("7 / 2 (int):         %d\n",    a / b);

    // With explicit cast — correct answer
    result = (float)a / b;
    printf("7 / 2 (float cast):  %.2f\n", result);

    // Implicit — int auto-converts to float
    float autoConv = 100;   // 100 → 100.000000
    printf("Auto int→float:      %f\n", autoConv);

    // sizeof all types
    printf("\n-- Memory sizes --\n");
    printf("int    : %zu bytes\n", sizeof(int));
    printf("float  : %zu bytes\n", sizeof(float));
    printf("double : %zu bytes\n", sizeof(double));
    printf("char   : %zu bytes\n", sizeof(char));

    return 0;
}
terminal
output
7 / 2 (int):         3
7 / 2 (float cast):  3.50
Auto int→float:      100.000000

-- Memory sizes --
int    : 4 bytes
float  : 4 bytes
double : 8 bytes
char   : 1 bytes
quiz
Q

Quick Quiz

Question 1 of 5

What is the perimeter of a square with side = 9?

Question 2 of 5

What does float f = 9; store in f?

Question 3 of 5

What is the output of: int sum = 5 / 2; printf("%d", sum);

Question 4 of 5

How do you fix integer division to get 2.50 from 5 / 2?

Question 5 of 5

What format specifier should you use to print the result of sizeof(int)?

Lesson Checklist

  • I can write perimeter of square using int
  • I can write perimeter of rectangle using float
  • I can write area of triangle using 0.5 × base × height
  • I know when to use int vs float in geometry programs
  • I understand implicit conversion — C promotes types automatically
  • I know int stored in float adds .000000 — no data lost
  • I know float stored in int LOSES the decimal part
  • I understand int/int = int — decimal is dropped
  • I can use explicit cast (float) to fix division
  • I understand sizeof() and use %zu to print it
  • I can use sizeof(arr)/sizeof(arr[0]) to get array element count
  • I completed the quiz