0% found this document useful (0 votes)
10 views

Untitled Document 4

Uploaded by

angshuman1018
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)
10 views

Untitled Document 4

Uploaded by

angshuman1018
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
You are on page 1/ 17

1.

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.

3. Von Neumann Architecture

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

Key characteristics of memory include volatility, accessibility, speed, and cost.

- Types

Memory types include volatile types like RAM and non-volatile types like ROM, flash storage,
and secondary storage devices.

5. Hardware Concepts: I/O Devices


Input/Output devices are hardware used to interact with a computer. Input devices (like
keyboards, mice, and scanners) allow users to input data, while output devices (like monitors
and printers) display or project data. Some devices, like touchscreen displays, function as both
input and output.

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.

- Character Representation Codes

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.

- Floating Point Arithmetic

This is used for representing real numbers and performing arithmetic operations with them.

- Signed and Unsigned Numbers

This distinction allows computers to represent both positive and negative values.

- Memory Storage Units

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

C is a general-purpose programming language that has been widely used in various


applications from system programming to game development. It provides a good level of
abstraction while also allowing low-level manipulation of data. The language's design aims for
simplicity and efficiency and has influenced many other programming languages, including C++,
C#, and Java.

3. Basic Structure of a C Program

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:

● Preprocessor Commands: Instructions to the compiler to preprocess the information


before actual compilation starts.
● Functions: Code blocks that perform specific tasks, where main() is mandatory.
● Variables: Used to store data.
● Statements & Expressions: Instructions that the program follows to perform any action.
● Comments: For explaining the code, ignored by the compiler.

4. Variables, Constants, and Data Types

● 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: &, |, ^, ~, <<, >>

- Operator Precedence and Associativity

Defines how expressions are evaluated when multiple operators are present. Operators with
higher precedence are evaluated first.

6. Managing Input and Output Operations

● 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

- Using if and else

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
}

- Using switch and break

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
}

The break statement is used to exit from the switch block.

3. Concepts of Loops

Loops allow repeating a block of code multiple times.

- 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

Executes as long as the condition remains true.

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 and break

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

The default storage class for local variables.

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.

6. Command Line Argument

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;
}

In this overview, we covered various crucial concepts of C programming, which provide a


structured way to control the flow of execution and manage memory in efficient ways,
fundamental to mastering C.

1. One-Dimensional Arrays

A one-dimensional array in C is a collection of variables of the same type, stored in contiguous


memory locations. Arrays are indexed starting at 0. Here's how you declare and initialize an
array:

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.

Use in Matrix Computations


c
Copy code
#include <stdio.h>

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};

// Matrix multiplication of a[2][3] and b[3][2]


for (int i = 0; i < 2; i++) {
for (int j = 0; j < 2; j++) {
for (int k = 0; k < 3; k++) {
result[i][j] += a[i][k] * b[k][j];
}
}
}

// 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

Sub-programming involves breaking down a large program into smaller, manageable


sub-programs or functions. This enhances modularity and code reuse.

4. Functions

Functions are self-contained blocks of code that perform a specific task.

Example of User-Defined Functions


c
Copy code
#include <stdio.h>

// Function declaration (prototype)


int add(int, int);

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.

6. Return Values and Their Types

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.

9. Function with Variable Number of Arguments

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 sum(int count, ...) {


int total = 0;
va_list args;
va_start(args, count);
for (int i = 0; i < count; i++) {
total += va_arg(args, int);
}
va_end(args);
return total;
}

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;
}

This example calculates the factorial of a number using recursion.

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

2. Relationship Between Arrays and Pointers

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

3. Argument Passing Using Pointers

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

5. Passing Arrays as Arguments

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;
}

6. Strings and C String Library

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!

7. Structure and Union

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;

fp = fopen("file.txt", "r"); // Open file for reading


if (fp == NULL) {
perror("Error opening file");
return -1;
}

while ((c = fgetc(fp)) != EOF) { // Read a character at a time


printf("%c", c);
}

fclose(fp); // Close the file


return 0;
}

You might also like