Computing & Programming Basics
ARCHITECTURE OF COMPUTING SYSTEM
Applications Software: Designed to perform specific tasks or solve particular problems for users. Examples include word processors, web browsers, media players, and specialized business applications. They interact with the operating system for basic functions.
System Software: Manages the fundamental operations of the computer hardware and provides a platform for application software to run. This includes:
Operating System (OS): Manages computer hardware and software resources, provides common services for computer programs (e.g., Windows, macOS, Linux).
Compilers: Translate high-level source code into machine code or intermediate code.
Linkers: Combine object files (generated by the compiler) and libraries into a single executable program.
Loaders: Load executable programs into memory for execution.
Hardware: The physical components of the computing system, including the CPU, memory, storage devices, and input/output devices.
HARDWARE IN COMPUTING
Primary Storage (RAM - Random Access Memory): Volatile memory used for actively running programs and data. It provides very fast read/write access but loses its contents when power is turned off. It's directly accessible by the CPU.
Secondary Storage: Non-volatile memory used for long-term data storage. It's slower than primary storage but retains data without power. Examples include:
SSD (Solid State Drive): Uses flash memory, offering faster performance and durability than traditional hard disks.
Hard Disk Drive (HDD): Uses spinning platters and read/write heads for data storage, generally cheaper per gigabyte than SSDs.
Input Devices: Allow users to send data and control signals to a computer. Examples: Keyboard, Mouse, Microphone, Scanner, Webcam.
Output Devices: Used to display or output data from a computer. Examples: Screen (Monitor), Printer, Speakers, Projector.
CPU (Central Processing Unit): The "brain" of the computer that carries out instructions of a computer program. It consists of:
Control Unit (CU): Manages and coordinates all operations within the CPU and the rest of the computer system, fetching instructions from memory and directing the flow of data.
ALU (Arithmetic Logic Unit): Performs arithmetic operations (e.g., addition, subtraction) and logical operations (e.g., AND, OR, NOT).
Registers: Small, high-speed storage locations within the CPU used to hold data currently being processed.
Cache Memory: Small, very fast memory located directly on or near the CPU, used to store frequently accessed data and instructions to reduce access time to main memory.
PROGRAMMING LANGUAGES
Bits: The smallest unit of data in a computer, representing a binary value (0 or 1). Instructions understood by computers are ultimately represented as sequences of bits, known as machine code.
Assembly Language: A low-level programming language that uses symbolic notations (mnemonics) to represent machine code instructions. It is architecture-specific and must be translated into machine code by an assembler.
High-Level Language: Programming languages that are closer to human language, abstracting away machine-level details. They are generally portable across different computer architectures and require a compiler or interpreter to be converted into machine-executable form (e.g., C, Java, Python).
C PROGRAMMING LANGUAGE
Popularity: Widely used in industry, especially for developing operating systems (like Linux kernel), embedded systems, real-time systems, and high-performance computing due to its efficiency and direct memory access capabilities.
Influences/Standards: C has evolved through various international standards to ensure portability and introduce new features:
C89 (ANSI C): The first widely adopted standard (1989/1990).
C99: Introduced features like
_Bool(boolean type),long long int, variable-length arrays,restrictkeyword, and inline functions (1999).C11: Added features such as generic selections (
_Generic), anonymous unions and structs, alignment specifications, and improved Unicode support (2011).
ECLC Cycle (Edit, Compile, Link, Execute): The typical workflow for developing C programs:
Edit: Write the source code in a text editor (e.g.,
source.c).Preprocess: The preprocessor handles directives like
#includeand#define, expanding macros and including header files, generating an intermediate.ifile.Compile: The compiler translates the preprocessed source code into assembly code, then into object code (e.g.,
object.oorobject.obj), which contains machine instructions.Link: The linker combines the object code with necessary library functions (e.g.,
stdio.hforprintf) and other object files to create a single executable program (e.g.,a.outorprogram.exe).Execute: The operating system loads the executable program into memory and runs it.
gcc: The GNU Compiler Collection is the primary and most common compiler suite used for C programs, supporting C, C++, Objective-C, Fortran, Ada, and Go.
MEMORY LAYOUT OF A C PROGRAM
A running C program typically occupies distinct memory segments:
Text Segment (.text): Stores the executable machine code instructions. It is typically read-only to prevent accidental modification and protect against buffer overflows.
Data Segment (.data): Stores initialized global and static variables. These variables have a fixed size and initial values specified at compile time. It is a read/write segment.
BSS Segment (.bss - Block Started by Symbol): Stores uninitialized global and static variables. The operating system initializes these variables to zero before the program starts execution. It is a read/write segment and contributes to the program's size in memory, not in the executable file.
Heap Segment: Used for dynamic memory allocation during program runtime (e.g., using
malloc(),calloc(),realloc()). Memory requested from the heap is managed by the programmer and must be explicitlyfree()d to prevent memory leaks. The heap grows upwards.Stack Segment: Used for automatic variables (local variables within functions), function parameters, return addresses, and saved register states during function calls. It operates on a Last-In, First-Out (LIFO) principle and grows downwards.
STRUCTURE OF A C PROGRAM
A typical C program includes several components:
Preprocessor statements: Directives like
#include(for including header files) and#define(for defining macros/constants) that are processed before compilation.Global declarations: Variables and functions declared outside of any function, making them accessible throughout the entire program.
Function prototypes (declarations): Statements that declare a function's name, return type, and parameters before its actual definition. This allows the compiler to check for correct function usage.
main()function: The entry point of every C program. Execution always begins here. It can optionally take command-line arguments (int main(int argc, char *argv[])).Function definitions: The actual implementation blocks of code for functions declared earlier or implicitly used.
C PROGRAMMING ELEMENTS
Comments: Used to clarify code for human readers, ignored by the compiler.
Single-line comments: Start with
//and continue to the end of the line.Multi-line comments: Enclosed between
/*and*/.
Declarations: Must be made before variables or functions are used, specifying their data type and identifier. Data types (
int,char,float,double,void) determine the kind of values a variable can hold and the amount of memory it occupies.Functions: Modular blocks of code designed to perform a specific task. Every C program must include a
main()function, which serves as the primary entry point for execution.
C PREPROCESSOR
The C preprocessor is a program that performs transformations on the source code before compilation. Common directives include:
#define: Used to create symbolic constants (macros) or function-like macros, which involve simple text substitution. Example:#define PI 3.14159.#include: Used to insert the content of another file (header file) into the current source file.#include <filename.h>: For standard library header files (searches in system directories).#include "filename.h": For user-defined header files (searches in current directory first).
Conditional compilation directives like
#ifdef,#ifndef,#endif,#if,#else,#elifallow different parts of code to be compiled based on certain conditions.
INPUT/OUTPUT FUNCTIONS
printf(): Used for formatted standard output (typically to the console). It takes a format string and a variable number of arguments.Common format specifiers include:
%d(integer),%f(floating-point),%c(character),%s(string),%x(hexadecimal),%p(pointer address).Allows for specifying field width, precision, and alignment (e.g.,
%.2ffor two decimal places).
scanf(): Used for formatted standard input (typically from the keyboard). It reads input according to a format string and stores values into variables.Important: Requires the memory address (using the
&operator) of the variables where the input data should be stored (e.g.,scanf("%d", &num);).Returns the number of items successfully read.
ESCAPE SEQUENCES
Special character combinations used within string literals in C to represent non-printing characters or to escape certain characters.
\n: Newline character (moves cursor to the beginning of the next line).\t: Horizontal tab.\b: Backspace.\r: Carriage return (moves cursor to the beginning of the current line).\\: Backslash character.\": Double quotation mark.\': Single quotation mark.\0: Null character (marks the end of a string).
SUMMARY
This document details the fundamental concepts of computer architecture, the hierarchy of programming languages, the specifics of the C programming language (including its compilation cycle, memory organization, and basic syntax elements), and essential input/output operations and escape sequences inherent to C programming.