Mixed C Examples Progress
0%
C Programming · 5 Mixed Examples

5 Mixed C Examples

Learn variables, arrays, strings, loops, functions, structures, and pointers through five small, practical examples.

Variables & Input
Arrays & Loops
Strings & Functions
Structures
Pointers
1

Example 1 — Variables and Input

0 – 10 min

This example reads two numbers and prints their sum.

example1_sum.c
C
#include <stdio.h>

int main() {
    int a, b;
    printf("Enter two numbers: ");
    scanf("%d %d", &a, &b);
    printf("Sum = %d\n", a + b);
    return 0;
}
2

Example 2 — Arrays and Loops

10 – 20 min

This example stores marks in an array and prints all values using a loop.

example2_array.c
C
#include <stdio.h>

int main() {
    int marks[5] = {85, 90, 78, 88, 92};
    for (int i = 0; i < 5; i++) {
        printf("%d ", marks[i]);
    }
    printf("\n");
    return 0;
}
Idea: Arrays are useful when you need to store many values of the same type.
3

Example 3 — Strings and Functions

20 – 30 min

This example uses a function to display a string.

example3_string_function.c
C
#include <stdio.h>
#include <string.h>

void showName(char name[]) {
    printf("Hello, %s\n", name);
}

int main() {
    char name[50] = "Aman";
    showName(name);
    return 0;
}
⚠️ Note: Strings in C are character arrays ending with \0.
4

Example 4 — Structures

30 – 40 min

Structures group related data like a student's roll number, name, and marks.

example4_structure.c
C
#include <stdio.h>

struct Student {
    int roll;
    char name[50];
    float marks;
};

int main() {
    struct Student s1 = {1, "Sara", 91.5};
    printf("%d %s %.2f\n", s1.roll, s1.name, s1.marks);
    return 0;
}
5

Example 5 — Pointers

40 – 50 min

This example changes a variable using a pointer.

example5_pointer.c
C
#include <stdio.h>

void change(int *x) {
    *x = 100;
}

int main() {
    int a = 10;
    change(&a);
    printf("a = %d\n", a);
    return 0;
}
Pointer rule: Use & to get an address and * to access the value at that address.
comparison
ExampleMain ConceptWhat It Does
1VariablesAdds two numbers
2Arrays + LoopsPrints all marks
3Strings + FunctionsPrints a greeting
4StructuresStores student data
5PointersChanges a value directly
Q

Quick Quiz

Question 1 of 5

Which concept stores many values of the same type?

Question 2 of 5

Which symbol is used to get the address of a variable?

Question 3 of 5

What does a function do?

Question 4 of 5

Which concept groups different data types together?

Question 5 of 5

Which operator gives the value stored at an address?

Lesson Checklist

  • I understand variables and input
  • I understand arrays and loops
  • I understand strings and functions
  • I understand structures
  • I understand pointers
  • I reviewed all 5 examples
  • I can write a similar program myself