×

C Programming

📌 Important Notice & Guidelines

  • Scholarship Rule: The percentage of marks you score in the test will be equal to the percentage of scholarship you receive.
  • Weekly Test: Tests will be conducted every Friday.
  • Result Update: Your marks/results will be updated on the WhatsApp Channel every Saturday.
  • New Batches: New batches will start every Monday.

Introduction

C is known as the mother language of many modern programming languages. It is a high-level, procedural language developed in the early 1970s and is widely used in system programming, application development, embedded systems, and software engineering. Many languages like C++, Java, and Python have roots in C. Being called the “mother language” emphasizes its foundational importance, as understanding C gives a strong base for learning other languages and understanding computer programming concepts.

#include <stdio.h>
int main() {
    printf("C Programming Basics\n");
    return 0;
}
                

C Programming Basics

1. Basic Structure of C Program

Every C program has a basic structure: header files, main function, statements, and return. Header files provide libraries for input/output and other functions. int main() is the entry point. Statements define logic and each ends with a semicolon. return 0; indicates successful execution.

#include <stdio.h>
int main() {
    printf("C Programming Basics\n");
    return 0;
}
                

C Programming Basics

2. Variables and Data Types

Variables store data in memory. Data types include int, float, char, double, etc. Correct data type selection ensures efficiency and prevents errors.

#include <stdio.h>
int main() {
    int a = 10;
    float b = 5.5;
    char c = 'A';
    double d = 20.55;
    printf("Integer: %d\n", a);
    printf("Float: %.2f\n", b);
    printf("Char: %c\n", c);
    printf("Double: %.2lf\n", d);
    return 0;
}
                

Integer: 10
Float: 5.50
Char: A
Double: 20.55

3. Input/Output in C

scanf() is used to get input, printf() is used to display output. Both are part of stdio.h.

#include <stdio.h>
int main() {
    int num;
    printf("Enter a number: ");
    scanf("%d", &num);
    printf("You entered: %d\n", num);
    return 0;
}
                

Enter a number: 25
You entered: 25

4. Operators

C uses arithmetic, relational, logical, and assignment operators to perform operations on data.

#include <stdio.h>
int main() {
    int a = 10, b = 5;
    printf("Addition: %d\n", a+b);
    printf("Subtraction: %d\n", a-b);
    printf("Multiplication: %d\n", a*b);
    printf("Division: %d\n", a/b);
    printf("Modulo: %d\n", a%b);
    printf("Greater? %d\n", a>b);
    printf("Equal? %d\n", a==b);
    return 0;
}
                

Addition: 15
Subtraction: 5
Multiplication: 50
Division: 2
Modulo: 0
Greater? 1
Equal? 0

5. Conditional Statements

if, if-else, else-if allow decision-making based on conditions.

#include <stdio.h>
int main() {
    int num;
    printf("Enter a number: ");
    scanf("%d", &num);
    if(num % 2 == 0)
        printf("Even Number\n");
    else
        printf("Odd Number\n");
    return 0;
}
                

Enter a number: 8
Even Number

6. Nested if and else-if

For multiple conditions, nested if or else-if statements are used.

#include <stdio.h>
int main() {
    int marks;
    printf("Enter marks: ");
    scanf("%d", &marks);
    if(marks >= 90)
        printf("Grade A\n");
    else if(marks >= 75)
        printf("Grade B\n");
    else if(marks >= 50)
        printf("Grade C\n");
    else
        printf("Grade F\n");
    return 0;
}
                

Enter marks: 82
Grade B

7. Switch Case

switch allows multiple choice selection for different cases.

#include <stdio.h>
int main() {
    int choice;
    printf("Enter 1-3: ");
    scanf("%d", &choice);
    switch(choice) {
        case 1: printf("One\n"); break;
        case 2: printf("Two\n"); break;
        case 3: printf("Three\n"); break;
        default: printf("Invalid choice\n");
    }
    return 0;
}
                

Enter 1-3: 2
Two

8. Loops (for, while, do-while)

Loops allow repeated execution of a block of code. for, while, and do-while loops are used based on the condition and number of iterations.

#include <stdio.h>
int main() {
    int i;
    for(i=1; i<=5; i++) {
        printf("Number: %d\n", i);
    }
    return 0;
}
                

Number: 1
Number: 2
Number: 3
Number: 4
Number: 5

9. Arrays

Arrays store multiple values of the same data type. They are useful for storing lists and performing operations on sequential data.

#include <stdio.h>
int main() {
    int arr[5] = {10, 20, 30, 40, 50};
    for(int i=0; i<5; i++) {
        printf("Element %d: %d\n", i, arr[i]);
    }
    return 0;
}
                

Element 0: 10
Element 1: 20
Element 2: 30
Element 3: 40
Element 4: 50

10. Strings

Strings are arrays of characters used to store text. Standard library functions help in string manipulation.

#include <stdio.h>
#include <string.h>
int main() {
    char name[20] = "Avinash";
    printf("Name: %s\n", name);
    printf("Length: %lu\n", strlen(name));
    return 0;
}
                

Name: Avinash
Length: 7

11. Functions

Functions allow modular code by breaking a program into smaller reusable blocks.

#include <stdio.h>
int add(int a, int b) {
    return a + b;
}
int main() {
    int result = add(10, 20);
    printf("Sum: %d\n", result);
    return 0;
}
                

Sum: 30

12. Pointers

Pointers store memory addresses and allow efficient manipulation of data.

#include <stdio.h>
int main() {
    int a = 10;
    int *ptr = &a;
    printf("Value: %d\n", *ptr);
    return 0;
}
                

Value: 10

13. Structures

Structures allow grouping of different data types under a single name.

#include <stdio.h>
struct Student {
    int id;
    char name[20];
};
int main() {
    struct Student s1 = {1, "Avinash"};
    printf("ID: %d, Name: %s\n", s1.id, s1.name);
    return 0;
}
                

ID: 1, Name: Avinash

14. File Handling

C allows reading/writing files using fopen, fclose, fprintf, fscanf.

#include <stdio.h>
int main() {
    FILE *fp = fopen("test.txt", "w");
    fprintf(fp, "Hello File\n");
    fclose(fp);
    return 0;
}
                

File created with text: Hello File

15. Recursion

Recursion is when a function calls itself to solve smaller instances of a problem. It helps in tasks like factorial, Fibonacci series, etc.

#include <stdio.h>
int factorial(int n) {
    if(n == 0)
        return 1;
    else
        return n * factorial(n-1);
}
int main() {
    int num = 5;
    printf("Factorial of %d is %d\n", num, factorial(num));
    return 0;
}
                

Factorial of 5 is 120

Best of Luck to All Students!

Give your best in the test, stay focused, and keep learning something new every day. Believe in yourself, and let each lesson make you stronger and smarter.

WhatsApp Chat