0% found this document useful (0 votes)
9 views6 pages

C Programming

C is a procedural programming language developed in 1972, known for its efficiency and portability, particularly in system programming. The document covers fundamental concepts such as data types, variables, operators, control structures, loops, arrays, functions, pointers, structures, and file handling, accompanied by example programs. Each section provides code snippets demonstrating key features and their applications.

Uploaded by

Poornima Eg
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)
9 views6 pages

C Programming

C is a procedural programming language developed in 1972, known for its efficiency and portability, particularly in system programming. The document covers fundamental concepts such as data types, variables, operators, control structures, loops, arrays, functions, pointers, structures, and file handling, accompanied by example programs. Each section provides code snippets demonstrating key features and their applications.

Uploaded by

Poornima Eg
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

Introduction to C

C, developed by Dennis Ritchie in 1972, is a procedural language for system programming. It


offers direct memory access via pointers and is highly portable across platforms. Programs
compile to machine code for efficiency, used in OS like Linux and embedded systems.

Structure: Preprocessor directives (#include), global declarations, main() function, sub-


functions.[3]

Program 1: Hello World

#include <stdio.h>
int main() {
printf("Hello, World!\n");
return 0;
}

Compile: gcc hello.c -o hello; Run: ./hello. Output: Hello, World! Demonstrates I/O and program
skeleton.[2][1]

Page 2: Data Types & Variables

Basic types: int (integers), float/double (decimals), char (characters), void (empty). Modifiers:
short/long, signed/unsigned. Variables declared before use; scope local/global.

Example: int age=30; float pi=3.14;

Program 2: Add Two Numbers

#include <stdio.h>
int main() {
int a, b, sum;
printf("Enter two numbers: ");
scanf("%d %d", &a, &b);
sum = a + b;
printf("Sum: %d\n", sum);
return 0;
}

Input: 5 3; Output: Sum: 8. Uses scanf for input, & for addresses.[2]

Page 3: Operators & Expressions

Arithmetic (+,-,*,/,%), relational (>,<,==), logical (&&,||), bitwise (&,|,^), assignment (=,+=).[4]
Precedence: * / > + -.

Program 3: Check Even/Odd

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

Input: 4; Output: Even. % computes remainder.

Page 4: Decision Making

if, if-else, nested if, switch. Ternary ?: for short conditions.

Program 4: Largest of Three

#include <stdio.h>
int main() {
int a, b, c;
scanf("%d %d %d", &a, &b, &c);
if (a >= b && a >= c) printf("%d\n", a);
else if (b >= c) printf("%d\n", b);
else printf("%d\n", c);
return 0;
}

Input: 10 20 5; Output: 20.

Page 5: Loops

for (fixed count), while/do-while (condition-based).

Program 5: Factorial

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

Input: 5; Output: 120.

Page 6: Arrays

Fixed-size collections: int arr[^5] = {1,2,3,4,5}; Multi-D: int mat[^2][^3]; Strings: char arrays
ending '\0'.

Program 6: Sum Array Elements

#include <stdio.h>
int main() {
int n, arr[^100], sum = 0;
scanf("%d", &n);
for (int i = 0; i < n; i++) {
scanf("%d", &arr[i]);
sum += arr[i];
}
printf("Sum: %d\n", sum);
return 0;
}

Input: 3 then 1 2 3; Output: 6.

Page 7: Functions

Reusable code blocks: return_type name(params) { body }. Pass by value/reference.

Program 7: Prime Check

#include <stdio.h>
int isPrime(int n) {
if (n < 2) return 0;
for (int i = 2; i * i <= n; i++) if (n % i == 0) return 0;
return 1;
}
int main() {
int num; scanf("%d", &num);
printf("%s\n", isPrime(num) ? "Prime" : "Not");
return 0;
}

Input: 7; Output: Prime.

Page 8: Pointers

Variables holding addresses: int *p = &x; *p = 20; changes x. Arrays decay to pointers.

Program 8: Swap Numbers

#include <stdio.h>
void swap(int *a, int *b) {
int temp = *a; *a = *b; *b = temp;
}
int main() {
int x = 5, y = 10;
swap(&x, &y);
printf("x=%d y=%d\n", x, y);
return 0;
}

Output: x=10 y=5.

Page 9: Structures

Custom types: struct Student { char name[^50]; int age; };

Program 9: Simple Calculator

#include <stdio.h>
int main() {
char op; double a, b;
scanf("%lf %c %lf", &a, &op, &b);
switch (op) {
case '+': printf("%.2lf\n", a + b); break;
case '-': printf("%.2lf\n", a - b); break;
case '*': printf("%.2lf\n", a * b); break;
case '/': if (b) printf("%.2lf\n", a / b); break;
}
return 0;
}

Input: 10 + 5; Output: 15.00.

Page 10: Files & Advanced

FILE *f = fopen("[Link]", "w"); fprintf(f, "data"); fclose(f);


Program 10: Fibonacci Series

#include <stdio.h>
int main() {
int n, a=0, b=1, next;
scanf("%d", &n);
printf("%d %d ", a, b);
for (int i = 2; i < n; i++) {
next = a + b; printf("%d ", next);
a = b; b = next;
}
return 0;
}

Input: 8; Output: 0 1 1 2 3 5 8 13. Great for recursion practice too.

You might also like