Presentation 1
Presentation 1
Presentation 1
PROGRAMMING
Comparison between C and C++ Programming
Languages
1.Programming Paradigm:
•C: C is a procedural programming language. It emphasizes functions and
procedures as the primary organizational unit of code. Data and functions are
separate entities.
•C++: C++ is a multi-paradigm language that supports procedural, object-
oriented, and generic programming paradigms. It introduces classes and
objects as fundamental concepts, allowing for the implementation of object-
oriented designs.
2.Object-Oriented Programming (OOP):
•C: C does not support object-oriented programming directly. It lacks features
such as classes, objects, inheritance, and polymorphism (allows object to be
treated as instance).
•C++: C++ is designed for object-oriented programming. It introduces classes,
which are user-defined types that encapsulate data and functions. Inheritance,
polymorphism, and encapsulation are core concepts in C++ OOP.
3.Standard Libraries:
•C: Standard C libraries provide functionality for basic operations such as
input/output (stdio.h), memory allocation (stdlib.h), string manipulation
(string.h), etc.
•C++: In addition to C libraries, C++ includes the Standard Template Library
(STL), which provides generic classes and functions for containers (like
vectors, lists, maps), algorithms (sorting, searching), and utilities (smart
pointers, iterators).
4.Memory Management:
•C: Memory management in C is primarily done manually using functions
like malloc, calloc, realloc, and free. C does not have built-in support for
automatic memory management.
•C++: C++ introduces the concept of destructors and automatic memory
management through RAII (Resource Acquisition Is Initialization) using
constructors and destructors. Smart pointers (std::unique_ptr,
std::shared_ptr) help manage dynamic memory more safely.
5.Function Overloading:
•C: C does not support function overloading. Functions must
have unique names even if they perform similar tasks with
different input parameters.
•C++: C++ supports function overloading, allowing multiple
functions with the same name but different parameter lists. This
promotes code reusability and readability.
6.Reference Variables:
•C: C uses pointers for indirect addressing and passing by
reference.
•C++: C++ introduces reference variables (&) as aliases to
existing variables. References are often used for function
parameters and are safer than pointers in some scenarios.
7.File Handling:
•C: C provides file handling functions (fopen, fclose, fread,
fwrite, etc.) for basic file operations.
•C++: C++ introduces file stream classes (ifstream, ofstream,
fstream) that offer a more object-oriented approach to file
handling, making file operations more intuitive and type-safe.
8.Compatibility:
•C and C++: C++ is largely compatible with C code. Most C
code can be compiled and used within a C++ program. However,
C++ introduces additional keywords, features, and syntax that are
not valid in pure C.
•Influence: C has influenced many modern programming
languages such as C++, Java, and Python, making it a
fundamental language for software development.
•Applications: Widely used for system programming,
embedded systems, operating systems, and high-performance
computing due to its efficiency and low-level capabilities.
Basic Syntax and Structure
Hello World Program: Introduce the classic "Hello World" program in C to demonstrate
basic syntax and program structure.
#include <stdio.h>
int main() {
printf("Hello, World!\n");
return 0;
}
Key Components of the above code:
float: Used for floating-point numbers (real numbers). It has a size of 4 bytes.
EXAMPLE: float price = 12.99;
double: Used for double-precision floating-point numbers with higher precision than
float. It has a size of 8 bytes.
2. Modifiers:
• short: Modifies the range of int to be smaller (typically 2 bytes).
EXAMPLE: short int num = 100;
• long: Modifies the range of int to be larger (typically 4 bytes or 8 bytes depending on
the system).
EXAMPLE: long int bigNum = 1000000;
• unsigned: Used to allow only non-negative values for a data type, effectively doubling
the positive range.
EXAMPLE: unsigned int count = 500;
3. Derived Data Types:
• Arrays: A collection of similar data elements. Arrays can be of any data type, including
user-defined types (structures).
EXAMPLE: int numbers[5] = {1, 2, 3, 4, 5};
Pointers: Variables that store memory addresses. They can point to variables, arrays, or
functions.
EXAMPLE: int *ptr;
ptr = &age; // ptr now points to the memory location of age
points to the memory location of
Structures: User-defined data type that allows storing multiple variables of different
data types under one name.
EXAMPLE:
struct Person {
char name[50];
int age;
float salary;
};
struct Person employee;
Unions: Similar to structures but share the same memory location for all members. Only
one member can hold a value at any given time.
EXAMPLE: union Data {
int num;
float fnum;
};
union Data value;
;
Enums: Used to define a set of named integer constants. Enums provide better
readability and organization for related constants.
EXAMPLE: enum Color { RED, GREEN, BLUE };
enum Color selectedColor = GREEN;
GREEN;