C Programming Language

 

C Programming Notes with Examples


1. Introduction to C Programming

1.1 What is C?
  • C is a general-purpose, procedural, and structured programming language. It was developed by Dennis Ritchie in 1972 at Bell Labs.
  • C is widely used for system software development, embedded systems, and application programming.
1.2 Structure of a C Program

A basic C program consists of:

  1. Preprocessor directives (e.g., #include).
  2. Main function where the program execution begins.
  3. Statements inside the main function to perform tasks.
#include <stdio.h>
int main() { printf("Hello, World!\n"); return 0; }

2. C Data Types

2.1 Basic Data Types
  • int: Integer type, used for storing whole numbers.
  • float: Floating-point type, used for storing decimal numbers.
  • char: Character type, used for storing single characters.
  • double: Double-precision floating-point type for storing decimal numbers with higher precision.
int a = 5;
float b = 3.14; char c = 'A'; double d = 3.141592;
2.2 Derived Data Types
  • Arrays: A collection of elements of the same data type.
  • Pointers: Variables that store memory addresses of other variables.
  • Structures: A collection of different data types grouped together.
int arr[5] = {1, 2, 3, 4, 5}; // Array
int *ptr = &a; // Pointer struct Person { char name[50]; int age; }; // Structure

3. C Operators

3.1 Arithmetic Operators

These operators perform mathematical operations.

int sum = a + b;
int diff = a - b; int product = a * b; int quotient = a / b; int remainder = a % b;
3.2 Relational Operators

These operators compare two values.

a == b; // Equal to
a != b; // Not equal to a > b; // Greater than a < b; // Less than a >= b; // Greater than or equal to a <= b; // Less than or equal to
3.3 Logical Operators

These operators are used for logical operations.

a && b; // AND
a || b; // OR !a; // NOT
3.4 Assignment Operators

Used to assign values to variables.

a = 5; // Simple assignment
a += 3; // a = a + 3 a -= 2; // a = a - 2 a *= 4; // a = a * 4 a /= 2; // a = a / 2

4. C Control Structures

4.1 If-Else Statement

The if-else statement allows you to execute code conditionally.

if (a > b) {
printf("a is greater than b\n"); } else { printf("a is less than or equal to b\n"); }
4.2 Switch-Case Statement

The switch-case statement is used to execute one out of multiple possible options.

switch (a) {
case 1: printf("Option 1\n"); break; case 2: printf("Option 2\n"); break; default: printf("Default option\n"); }
4.3 Loops
  • For loop: Used when the number of iterations is known.
for (int i = 0; i < 5; i++) {
printf("%d ", i); }
  • While loop: Used when the number of iterations is not known and depends on a condition.
int i = 0;
while (i < 5) { printf("%d ", i); i++; }
  • Do-While loop: Executes the loop body at least once before checking the condition.
int i = 0;
do { printf("%d ", i); i++; } while (i < 5);

5. Functions in C

5.1 Function Declaration

A function is declared and then defined to perform specific tasks.

#include <stdio.h>
// Function declaration void greet(); int main() { greet(); // Function call return 0; } // Function definition void greet() { printf("Hello, Student!\n"); }
5.2 Function with Arguments and Return Value

Functions can take input arguments and return values.

#include <stdio.h>
// Function declaration int add(int a, int b); int main() { int result = add(5, 3); printf("Sum: %d\n", result); return 0; } // Function definition int add(int a, int b) { return a + b; }

6. Arrays in C

6.1 One-Dimensional Arrays

An array is a collection of elements of the same type.

int arr[5] = {1, 2, 3, 4, 5};
printf("%d\n", arr[0]); // Output: 1
6.2 Multi-Dimensional Arrays

Multi-dimensional arrays represent matrices or tables.

int arr[2][3] = {{1, 2, 3}, {4, 5, 6}};
printf("%d\n", arr[1][2]); // Output: 6

7. Pointers in C

7.1 What is a Pointer?

A pointer is a variable that stores the memory address of another variable.

int a = 5;
int *ptr = &a; // Pointer storing the address of 'a' printf("Address of a: %p\n", ptr); printf("Value of a: %d\n", *ptr); // Dereferencing the pointer
7.2 Pointer to Array

You can use pointers to access array elements.

int arr[3] = {10, 20, 30};
int *ptr = arr; printf("%d\n", *(ptr + 1)); // Output: 20

8. Structures in C

8.1 What is a Structure?

A structure is a user-defined data type that allows you to group different data types.

#include <stdio.h>
// Structure definition struct Person { char name[50]; int age; }; int main() { struct Person p1 = {"John", 25}; printf("Name: %s, Age: %d\n", p1.name, p1.age); return 0; }

9. File Handling in C

9.1 Opening a File

You can open a file for reading, writing, or appending.

FILE *file;
file = fopen("example.txt", "w"); // Open file for writing if (file == NULL) { printf("Error opening file!\n"); return 1; } fprintf(file, "Hello, file!\n"); fclose(file);
9.2 Reading from a File

You can read content from a file using the fscanf() or fgets() functions.

FILE *file;
char buffer[100]; file = fopen("example.txt", "r"); // Open file for reading if (file == NULL) { printf("Error opening file!\n"); return 1; } fgets(buffer, 100, file); printf("Content: %s", buffer); fclose(file);

10. C Memory Management

10.1 Dynamic Memory Allocation

You can dynamically allocate memory using functions like malloc(), calloc(), and realloc().

int *arr;
arr = (int *)malloc(5 * sizeof(int)); // Allocate memory for 5 integers if (arr == NULL) { printf("Memory allocation failed!\n"); return 1; } free(arr); // Free allocated memory

Assessment: Regular Tests, Assignments, and Final Project Evaluation

Certification: Certificate of Completion from Disha Institute

 

Number of Days Depends on your practice and feedback. The more you practice and review your work, the faster you will complete the course.

For Batch time and Fess contact to Below Address and Number.

Zamanat Sir

(MCA / Bsc. I.T / ‘A’ / ‘O’ / CCC / Govt. Certified  Domain Skill Trainer )

   Disha Institute 153 Vijay Nagar Opp. Rg Pg College W.K Road Meerut 

(9411617329 , 9458516690) 

Comments

Popular posts from this blog

VBA Modules for Excel Automation (Advance Excel)

Java Programming

Excel Shortcuts