0% found this document useful (0 votes)
15 views7 pages

C Programming Concepts and Examples

The document provides a structured overview of C programming concepts, including basics, input/output, operators, conditional statements, loops, functions, pointers, arrays, structures, and file I/O with dynamic memory. Each section includes concise explanations and examples of code to illustrate the concepts. It serves as a comprehensive guide for understanding fundamental C programming principles and practices.

Uploaded by

vishalkumarpat33
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
15 views7 pages

C Programming Concepts and Examples

The document provides a structured overview of C programming concepts, including basics, input/output, operators, conditional statements, loops, functions, pointers, arrays, structures, and file I/O with dynamic memory. Each section includes concise explanations and examples of code to illustrate the concepts. It serves as a comprehensive guide for understanding fundamental C programming principles and practices.

Uploaded by

vishalkumarpat33
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

\

Here is a more structured, topic‑wise note set with examples so you can map it to the video
chapters while keeping everything original and concise. [1] [2]

1. Basics, variables, data types


A C program usually has #include lines for headers and one main() function where execution
starts. [3]
Variables are named storage locations; the data type decides how much memory is used
and what kind of data is stored. [4] [1]
Example:

#include <stdio.h>

int main() {
int roll = 5; // integer
float cgpa = 8.5; // decimal
char section = 'A'; // single character

printf("Roll = %d\n", roll);


printf("CGPA = %.1f\n", cgpa);
printf("Section = %c\n", section);
return 0;
}

2. Input and output


printf prints text and values using format specifiers like %d for int, %f for float, %c for char,
%s for string. [5] [6]

scanf reads user input; use & before variable names to pass their addresses. [6] [5]
Example:

#include <stdio.h>

int main() {
int age;
float salary;

printf("Enter age and salary: ");


scanf("%d %f", &age, &salary);

printf("Age = %d, Salary = %.2f\n", age, salary);


return 0;
}

3. Operators and expressions


Arithmetic operators: + - * / %; relational: == != > < >= <=; logical: && || !. [3] [7]

Compound assignment like +=, -=, and increment/decrement ++, -- help write shorter
expressions. [7] [3]
Example:

#include <stdio.h>

int main() {
int a = 10, b = 3;
int sum = a + b;
int rem = a % b;

int x = 5;
x += 2; // x = x + 2 => 7
x++; // x = x + 1 => 8

printf("Sum = %d, Remainder = %d, x = %d\n", sum, rem, x);


return 0;
}

4. Conditional statements
if and if–else selectbetween blocks based on a condition; else if chains many conditions;
switch is good for menu‑like choices. [2] [8]

Example (if–else and switch):

#include <stdio.h>

int main() {
int day;
printf("Enter day (1-7): ");
scanf("%d", &day);

if (day == 1) {
printf("Monday\n");
} else if (day == 2) {
printf("Tuesday\n");
} else {
printf("Another day\n");
}
switch (day) {
case 6:
case 7:
printf("Weekend\n");
break;
default:
printf("Weekday\n");
}
return 0;
}

5. Loops
for is ideal when the number of iterations is known; while checks condition first; do–while
runs at least once. [9] [10]
Example (all three loops):

#include <stdio.h>

int main() {
// for: 1 to 5
for (int i = 1; i <= 5; i++) {
printf("%d ", i);
}

// while: countdown
int n = 3;
while (n > 0) {
printf("\nwhile: %d", n);
n--;
}

// do-while: at least once


int x = 0;
do {
printf("\nInside do-while, x = %d", x);
x--;
} while (x > 0);

return 0;
}

6. Functions and recursion


A function has a return type, name, parameters, and body; it is declared before use or via
prototype. [3] [4]
Recursion means a function calls itself with a smaller problem and has a base case to stop.
[11]

Example (normal and recursive function):


#include <stdio.h>

// normal function
int square(int n) {
return n * n;
}

// recursive factorial
int fact(int n) {
if (n == 0 || n == 1) // base case
return 1;
return n * fact(n - 1); // recursive call
}

int main() {
printf("Square of 4 = %d\n", square(4));
printf("Factorial of 5 = %d\n", fact(5));
return 0;
}

7. Pointers
A pointer stores the memory address of another variable; use & to get an address and * to
access the value at that address. [11]
Pointer and variable stay connected: changing one using the other reflects the same
memory. [11]
Example:

#include <stdio.h>

int main() {
int num = 10;
int *ptr = &num;

printf("num = %d\n", num);


printf("Address of num = %p\n", (void*)ptr);
printf("Value via pointer = %d\n", *ptr);

*ptr = 25; // modify through pointer


printf("num after change = %d\n", num);
return 0;
}
8. Arrays and strings
Array: fixed‑size collection of same‑type elements in continuous memory; index starts from
0. [7] [3]
String: char array ending with '\0', often used with %s in printf and scanf. [6] [7]
Example:

#include <stdio.h>

int main() {
int marks[^3] = {90, 80, 70};
char city[^10] = "Delhi";

for (int i = 0; i < 3; i++) {


printf("Mark %d = %d\n", i, marks[i]);
}

printf("City = %s\n", city);


return 0;
}

9. Structures
A struct groups different data types to represent one entity like a student or book. [12] [13]
Members are accessed using . for variables and -> for pointers to structures. [13] [12]
Example:

#include <stdio.h>

struct Student {
char name[^20];
int age;
float cgpa;
};

int main() {
struct Student s1 = {"Aman", 19, 8.7};

printf("Name = %s\n", [Link]);


printf("Age = %d\n", [Link]);
printf("CGPA = %.1f\n", [Link]);

return 0;
}
10. File I/O and dynamic memory
File handling uses FILE *, open modes like "r" (read), "w" (write), "a" (append), plus close
functions to release files. [3]
Dynamic memory uses functions like malloc and free to manage heap memory while the
program is running. [14] [15]
Example (file + malloc):

#include <stdio.h>
#include <stdlib.h>

int main() {
FILE *fp = fopen("[Link]", "w");
if (fp == NULL) {
printf("File error\n");
return 1;
}

int n = 3;
int *arr = malloc(n * sizeof(int));
if (arr == NULL) {
printf("Memory error\n");
fclose(fp);
return 1;
}

for (int i = 0; i < n; i++) {


arr[i] = (i + 1) * 10;
fprintf(fp, "%d\n", arr[i]);
}

free(arr);
fclose(fp);
return 0;
}

If you say “start from Chapter X and make detailed notes with 4–5 practice‑style examples”, a
focused chapter‑wise PDF‑style text outline can be created for you to copy into your notebook.

1. [Link]
2. [Link]
3. [Link]
4. [Link]
5. [Link]
6. [Link]
7. [Link]
8. [Link]
9. [Link]
10. [Link]
11. [Link]
12. [Link]
13. [Link]
14. [Link]
15. [Link]
oc/

You might also like