C & C++ Programming Notes for Freshers & Beginners
Introduction to C and C++
C Programming Language:
- Developed by Dennis Ritchie at Bell Labs in the early 1970s.
- Known for its efficiency, C is a general-purpose programming language that is widely used in system programming, operating systems, and embedded systems.
- Key Characteristics: Procedural language, low-level access to memory, a small set of keywords, and a robust set of operators.
C++ Programming Language:
- Developed by Bjarne Stroustrup in the early 1980s as an extension of C.
- C++ adds object-oriented features to C, making it suitable for complex software development.
- Key Characteristics: Supports both procedural and object-oriented programming paradigms, extensive standard library, and features like classes, inheritance, and polymorphism.
C Programming Basics
1. Basic Structure of a C Program:
c#include <stdio.h>
int main() {
// Your code goes here
printf("Hello, World!");
return 0;
}
- #include <stdio.h>: Preprocessor directive that includes the Standard Input Output library.
- int main(): The main function where the execution of the program begins.
- printf(): A function used to print text to the console.
- return 0; Indicates that the program executed successfully.
2. Data Types:
- Basic Data Types:
int,float,double,char. - Modifiers:
signed,unsigned,short,long.
3. Variables and Constants:
- Variables: Containers for storing data values.
- Declaration:
int num; - Initialization:
num = 10;orint num = 10; - Constants:
const int MAX = 100;
4. Operators:
- Arithmetic Operators:
+,-,*,/,% - Relational Operators:
==,!=,>,<,>=,<= - Logical Operators:
&&,||,! - Assignment Operators:
=,+=,-=,*=,/= - Increment/Decrement Operators:
++,--
5. Control Structures:
- If-Else Statement:c
if (condition) { // Code to execute if condition is true } else { // Code to execute if condition is false } - Switch Case:c
switch (expression) { case value1: // Code break; case value2: // Code break; default: // Default code } - Loops:
- For Loop:c
for (initialization; condition; increment) { // Code to execute repeatedly } - While Loop:c
while (condition) { // Code to execute as long as condition is true } - Do-While Loop:c
do { // Code } while (condition);
- For Loop:
6. Functions:
- Function Declaration:c
return_type function_name(parameter_list); - Function Definition:c
return_type function_name(parameter_list) { // Function body return value; } - Example:c
int add(int a, int b) { return a + b; }
C++ Programming Basics
1. Introduction to Object-Oriented Programming (OOP):
- Class: Blueprint for creating objects.
- Object: Instance of a class.
- Encapsulation: Bundling data and methods that operate on the data within one unit, a class.
- Inheritance: Mechanism by which one class can inherit attributes and methods from another class.
- Polymorphism: Ability to take many forms; methods can behave differently based on the object they are acting upon.
- Abstraction: Hiding the complex implementation details and showing only the necessary features.
2. Basic Structure of a C++ Program:
cpp#include <iostream>
using namespace std;
int main() {
// Your code goes here
cout << "Hello, World!";
return 0;
}
- #include <iostream>: Preprocessor directive that includes the Input-Output stream library.
- using namespace std; Allows the program to use all the entities in the
stdnamespace. - cout: Used to output (print) text to the console.
- cin: Used to take input from the user.
3. Classes and Objects:
- Class Declaration:cpp
class ClassName { public: // Access specifier // Attributes // Methods }; - Object Creation:cpp
ClassName objectName; - Example:cpp
class Car { public: string brand; string model; int year; void honk() { cout << "Beep Beep!" << endl; } }; int main() { Car myCar; myCar.brand = "Toyota"; myCar.model = "Corolla"; myCar.year = 2020; myCar.honk(); return 0; }
4. Constructors and Destructors:
- Constructor: A special function that is automatically called when an object is created.cpp
class MyClass { public: MyClass() { // Constructor cout << "Object created!" << endl; } }; - Destructor: A special function called when an object is destroyed.cpp
class MyClass { public: ~MyClass() { // Destructor cout << "Object destroyed!" << endl; } };
5. Inheritance:
- Syntax:cpp
class DerivedClass : accessSpecifier BaseClass { // Class members }; - Example:cpp
class Vehicle { public: string brand = "Ford"; void honk() { cout << "Beep Beep!" << endl; } }; class Car : public Vehicle { public: string model = "Mustang"; }; int main() { Car myCar; myCar.honk(); cout << myCar.brand + " " + myCar.model << endl; return 0; }
6. Polymorphism:
- Compile-time Polymorphism (Function Overloading):cpp
class Math { public: int add(int a, int b) { return a + b; } int add(int a, int b, int c) { return a + b + c; } }; - Run-time Polymorphism (Virtual Functions):cpp
class Animal { public: virtual void sound() { cout << "Animal sound!" << endl; } }; class Dog : public Animal { public: void sound() override { cout << "Woof Woof!" << endl; } };
7. Standard Template Library (STL):
- Containers: Predefined data structures like
vector,list,deque,set,map. - Iterators: Objects that point to elements within containers.
- Algorithms: Functions that perform operations like searching, sorting, counting, etc., on data.
Example:
cpp#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
int main() {
vector<int> v = {4, 2, 5, 1, 3};
sort(v.begin(), v.end());
for (int i : v) {
cout << i << " ";
}
return 0;
}
Conclusion
Understanding the basics of C and C++ programming lays a strong foundation for more advanced concepts in software development. As a beginner, focus on grasping the fundamental syntax, control structures, and the concepts of object-oriented programming. Practice regularly by writing simple programs and gradually challenge yourself with more complex projects. Happy coding!

