Untitled Document 4
Untitled Document 4
Introduction to Computers
Computers are electronic devices capable of performing computations and making logical
decisions at speeds millions (and even billions) of times faster than humans. They can process,
store, and retrieve data efficiently. Modern computers range from tiny microcontrollers
embedded in everyday devices to powerful supercomputers performing complex scientific
calculations.
2. History of Computers
The history of computers can be traced back to early mechanical calculating devices such as
the abacus and later to inventions like Blaise Pascal's Pascaline and Charles Babbage's
Analytical Engine. The development of electronic computers began in the 20th century with
machines such as the ENIAC, which used vacuum tubes. The invention of the transistor and
later the integrated circuit paved the way for modern computers.
Proposed by John von Neumann in 1945, this architecture is the basis for most contemporary
computers. It describes a system where data and instructions are stored in a single read/write
memory, making computers programmable. The architecture includes components such as a
Central Processing Unit (CPU), memory, and input/output mechanisms, with the CPU having an
arithmetic logic unit (ALU), registers, and a control unit.
4. Memory System
- Hierarchy
Memory hierarchy is structured into levels with varying speeds and sizes. From fastest and
smallest to slowest and largest, it typically includes registers, cache memory, main memory
(RAM), and secondary storage (hard drives, SSDs).
- Characteristics
- Types
Memory types include volatile types like RAM and non-volatile types like ROM, flash storage,
and secondary storage devices.
6. Software Concepts
- System Software
This includes operating systems, device drivers, and utilities that manage computer hardware
and basic system operations, enabling application software to run.
- Application Software
These are programs designed for end-users to perform specific tasks like word processing,
gaming, or data management.
7. Data Representation
- Number Systems
Computers use different number systems, chiefly binary, but also octal and hexadecimal for
various tasks. These systems are used for simplicity and efficiency in digital circuit design.
These include ASCII and Unicode, which standardize the representation of text in computers.
- Binary Arithmetic
Binary arithmetic is fundamental to computer operations, enabling calculations using the binary
number system.
This is used for representing real numbers and performing arithmetic operations with them.
This distinction allows computers to represent both positive and negative values.
Data in computers is measured in bits and bytes, with larger units like kilobytes, megabytes,
gigabytes, and terabytes based on powers of two or ten.
This comprehensive view captures the essence of computer science fundamentals, linking
historical developments with modern technological components and theoretical principles.
1. History of C
The C programming language was developed in the early 1970s by Dennis Ritchie at Bell Labs.
It was designed to provide constructs that map efficiently to machine instructions, making it a
practical language that could be used for system programming, especially for writing operating
systems. The development of C was closely tied to the development of the Unix operating
system, which was originally written in assembly language but later rewritten in C.
2. Introduction to C
A typical C program consists of one or more functions that perform tasks. Every C program
begins execution from the main function. A basic C program includes:
● Variables: Named locations in memory used to store data that may change during
program execution.
● Constants: Unlike variables, constants hold values which cannot be altered once
defined.
● Data Types: Define the type of data a variable can hold, such as int for integers, char
for characters, float for floating-point numbers, and double for double-precision
floating points.
5. Operators and Expressions
- Introduction
Operators are symbols that tell the compiler to perform specific mathematical or logical
manipulations. Expressions are combinations of variables, literals, and operators that are
evaluated to produce another value.
- Types of Operators
● Arithmetic Operators: +, -, *, /, %
● Relational Operators: ==, !=, <, >, <=, >=
● Logical Operators: &&, ||, !
● Assignment Operators: =, +=, -=, *=, /=
● Increment and Decrement Operators: ++, --
● Conditional Operator: ? :
● Bitwise Operators: &, |, ^, ~, <<, >>
Defines how expressions are evaluated when multiple operators are present. Operators with
higher precedence are evaluated first.
● Standard Input and Output: C uses stdio.h library which provides functions like
printf() for output and scanf() for input.
● Formatting I/O: Functions like printf() allow formatting output using placeholders
(like %d, %s etc.), while scanf() uses similar placeholders to read input according to
the specified format.
These topics form the foundation of programming in C and provide a robust framework for
understanding more complex programming constructs. Each element, from basic structure and
data representation to detailed operations management, plays a critical role in building efficient
and effective C programs.
1. C Statements
In C programming, statements are the building blocks of a program that perform specific tasks.
They can be simple (like assigning a value to a variable) or complex (involving control
structures). A statement in C ends with a semicolon (;).
2. Conditional Execution
The if statement allows for conditional execution of code fragments. C uses the boolean
conditions to decide whether to execute the if block or the else block. For example:
c
Copy code
if (condition) {
// code to execute if condition is true
} else {
// code to execute if condition is false
}
- Nesting of if
if statements can be nested, meaning you can have an if or else if statement inside
another if or else block:
c
Copy code
if (condition1) {
// code
if (condition2) {
// code to execute if condition1 and condition2 are true
}
} else {
// code to execute if condition1 is false
}
The switch statement allows a variable to be tested for equality against a list of values. Each
value is called a case, and the variable being switched on is checked for each case.
c
Copy code
switch (expression) {
case constant1:
// code
break;
case constant2:
// code
break;
default:
// code to execute if no case is matched
}
3. Concepts of Loops
- for Loop
Used when the number of iterations is known before entering the loop.
c
Copy code
for (initialization; condition; increment) {
// code to execute
}
- while Loop
c
Copy code
while (condition) {
// code to execute
}
- do-while Loop
Similar to the while loop, but it executes at least once, checking the condition after the loop
body.
c
Copy code
do {
// code to execute
} while (condition);
continue skips the remaining code in the current loop iteration. break exits the loop
immediately.
c
Copy code
for (int i = 0; i < 10; i++) {
if (i == 5) {
continue; // Skips the rest of the code for i == 5
}
if (i == 8) {
break; // Exits the loop when i == 8
}
printf("%d\n", i);
}
4. Storage Classes
Storage classes define the scope (visibility) and lifetime of variables and/or functions within a C
program.
- auto
c
Copy code
{
auto int a = 32; // Same as 'int a = 32;'
}
- register
Suggests that the variable be stored in a register instead of RAM, making it faster.
c
Copy code
register int counter;
5. Predefined Preprocessor
The preprocessor in C is a tool that processes commands before the actual compilation starts.
Directives like #define for defining constants and #include for including headers are
common preprocessor commands.
Command line arguments allow users to pass arguments to the program when it is executed.
c
Copy code
int main(int argc, char *argv[]) {
printf("There are %d command-line arguments:\n", argc);
for(int i = 0; i < argc; i++) {
printf("Argument %d: %s\n", i, argv[i]);
}
return 0;
}
1. One-Dimensional Arrays
c
Copy code
int numbers[5] = {10, 20, 30, 40, 50}; // Array of 5 integers
Example of Iterative Program Using Arrays
c
Copy code
#include <stdio.h>
int main() {
int numbers[5] = {10, 20, 30, 40, 50};
int sum = 0;
for(int i = 0; i < 5; i++) {
sum += numbers[i];
}
printf("Sum of array elements: %d\n", sum);
return 0;
}
This program calculates the sum of all elements in the numbers array.
2. Two-Dimensional Arrays
Two-dimensional arrays can be thought of as tables with rows and columns. They are used
extensively in applications like matrix computations.
int main() {
int a[2][3] = {{1, 2, 3}, {4, 5, 6}};
int b[3][2] = {{1, 4}, {2, 5}, {3, 6}};
int result[2][2] = {0};
// Printing result
for (int i = 0; i < 2; i++) {
for (int j = 0; j < 2; j++) {
printf("%d ", result[i][j]);
}
printf("\n");
}
return 0;
}
This example shows how to multiply two matrices using a two-dimensional array.
3. Concept of Sub-Programming
4. Functions
int main() {
int result = add(10, 20);
printf("Result: %d\n", result);
return 0;
}
// Function definition
int add(int a, int b) {
return a + b;
}
This program defines a function add that takes two integers and returns their sum.
5. Function Prototype
It provides a function's name, return type, and parameters to the compiler before its actual
definition.
Functions in C can return any type, including int, float, char, or void (no return value).
7. Calling Function
A function is called by using its name followed by arguments in parentheses, as seen in the add
function example above.
8. Function Argument
Arguments are the values you pass to the function parameters when calling the function.
C supports functions with variable numbers of arguments using stdargs.h. Here’s how you
can define such a function:
c
Copy code
#include <stdio.h>
#include <stdarg.h>
int main() {
printf("Sum: %d\n", sum(4, 10, 20, 30, 40)); // Passing 4
arguments
return 0;
}
10. Recursion
A function that calls itself is known as a recursive function. It's useful for tasks that can be
divided into similar subtasks, like computing factorials.
c
Copy code
#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;
}
Each of these topics plays a crucial role in structuring C programs efficiently and handling
various computational problems effectively.
1. Pointers
A pointer in C is a variable that stores the memory address of another variable. Pointers are
powerful tools that allow for efficient manipulation of memory and can lead to more complex
data structures like linked lists and dynamic memory allocation.
Example:
c
Copy code
int val = 10;
int *ptr = &val;
printf("Value of val: %d\n", *ptr); // Dereferencing ptr gives 10
Arrays and pointers are closely related; the name of an array acts like a pointer to its first
element.
Example:
c
Copy code
int arr[] = {10, 20, 30};
int *ptr = arr; // Equivalent to &arr[0]
printf("%d\n", *(ptr + 1)); // Outputs 20
Passing arguments by pointers allows functions to modify the actual data in the calling function
because it passes addresses rather than copies of the values.
Example:
c
Copy code
void increment(int *p) {
(*p)++;
}
int main() {
int a = 10;
increment(&a);
printf("Incremented value: %d\n", a); // Outputs 11
return 0;
}
4. Array of Pointers
An array of pointers can be useful for storing strings or arrays of different sizes.
Example:
c
Copy code
const char *days[] = {"Monday", "Tuesday", "Wednesday"};
printf("%s\n", days[1]); // Outputs Tuesday
Arrays are passed to functions by passing the pointer to the first element of the array.
Example:
c
Copy code
void printArray(int *arr, int size) {
for(int i = 0; i < size; i++) {
printf("%d ", arr[i]);
}
printf("\n");
}
int main() {
int nums[] = {10, 20, 30, 40};
printArray(nums, 4); // Array is passed by reference
return 0;
}
In C, strings are arrays of characters terminated by a null character '\0'. The C standard library
provides numerous functions for handling strings, such as strcpy, strcat, strcmp, etc.
Example:
c
Copy code
#include <string.h>
char str[100];
strcpy(str, "Hello");
strcat(str, " World!");
printf("%s\n", str); // Outputs Hello World!
Structures
Structures are used to group different data types into a single unit.
Example:
c
Copy code
struct Person {
char name[50];
int age;
};
int main() {
struct Person p1;
strcpy(p1.name, "John Doe");
p1.age = 30;
printf("Name: %s, Age: %d\n", p1.name, p1.age);
return 0;
}
Unions
Unions allow storing different data types in the same memory location. It's useful when you
need to store something that could be one of several types but never at the same time.
Example:
c
Copy code
union Data {
int i;
float f;
char str[20];
};
int main() {
union Data data;
data.i = 10;
printf("data.i : %d\n", data.i);
return 0;
}
8. File Handling
File handling in C is performed using a pointer of type FILE included in the standard I/O library.
Example:
c
Copy code
#include <stdio.h>
int main() {
FILE *fp;
char c;