A INDUSTRIAL TRAINING REPORT
ON
“C and C++”
Submitted in the fulfillment of the requirement for the award of the degree of
Bachelor of Technology
In
Electronics and Communication
Submitted to: Submitted By:
Shikha Bhardwaj Prince
UIET, Kurukshetra University, Roll no:
Kurukshetra 252201133
Class: ECE-B
Preference
Learn C and C++ from Internshala I had the privilege of
participating in the "Learn C and C++" training program
offeredonline
week by Internshala. This 8- a comprehensive and
program provided
enriching
experience in the world of C and C++ programming.
The program began with a strong foundation, covering essential
topics such as syntax, data types, and control structures. This
provided a solid footing for diving into more advanced concepts.
I had the opportunity to explore intricate aspects of C and C++,
including memory management, pointers, file handling, and
dynamic memory allocation.
These sessions expanded my understanding of these languages.
One of the program's standout features was the hands-on
experience gained through real-world projects.
I particularly enjoyed working on a Cricket game application that
implemented object-oriented programming (OOP) concepts. This
practical experience was both enjoyable and educational.
Throughout the program, there was a strong emphasis on coding
practices, including clean and optimized coding.
Regular assessments, which included quizzes, challenges, and
project presentations, helped gauge our progress and provided
valuable feedback. The program fostered a sense of community
and collaboration. Networking opportunities and peer learning
were actively encouraged, creating a conducive learning
environment.
As a result of this training program, I gained proficiency in C and
C++, improved my problem-solving skills, and enhanced my soft
skills. I feel well-prepared for future software development
opportunities and am grateful for the valuable experience
provided by Internshala.
Acknowledgment
I would like to express my heartfelt gratitude to Internshala for
providing me with the opportunity to participate in the "Learn C
and C++" training program.
This training has been a pivotal experience in my journey to
acquire proficiency in programming with C and C++. I extend
my sincere thanks to the instructors and trainers, Sriyank
Siddhartha and Aditya Sood, for their exceptional guidance and
teaching throughout
expertise the program.
and dedication Their
significantly contributed to my
learning
experience.
I am also thankful to my fellow participants for their collaboration
and support during the training. The interactive learning
environment and the opportunity to network with peers
enhanced my understanding of C and C++ programming.
I want to acknowledge the efforts of the entire Internshala team
for their role in organizing this training program. The accessible
platform, mobile- friendly interface, and placement assistance
have made this learning journey both convenient and rewarding.
Lastly, I would like to express my appreciation for the financial
support provided through the "CITY10" coupon code, which
made this training program even more accessible and
affordable.
This training has equipped me with valuable skills and
knowledge in C and C++ programming, and I look forward to
applying these skills in my academic and professional pursuits.
Thank you once again to Internshala and all those involved in
making this training program a success.
Index
Sr.No Content Page No.
1. Front Page 1
2. Preference 2
3. Certificate and Acknowledgment 3-4
4. Index 5
5. Basic Of C and its Type and 6-12
Operators and Condition, Loop
6. Function and Array and C Vs C+ 12-24
+
7. Dynamic Memory Allocation, 24-31
OOPS Conclusion and
Reference page
What is C?
C is a general-purpose, procedural, high-level programming
language used in the development of computer software and
applications, system programming, games, web development,
and more. C language was developed by Dennis M. Ritchie at
the Bell Telephone Laboratories in 1972. It is a powerful and
flexible language which was first developed for the programming
of the UNIX operating System. C is one of the most widely used
programming language. C programming language is known for
its simplicity and efficiency. It is the best choice to start with
programming as it gives you a foundational understanding of
programming.
C Hello World Program
To begin with, the “Hello World” program is the first step towards
learning any programming language and also one of the
simplest programs you will learn. All one needs to do is display
the message “Hello World” on the screen. Let’s look at the
program and try to understand the terminologies involved in it.
C Program to Print “Hello World”
The following C program displays “Hello World” in the output.
// Simple C program to display "Hello World"
// Header file for input output
functions #include <stdio.h>
// main function -
// where the execution of
program begins
int main()
{
// prints hello
world
printf("Hello
World");
return 0;
}
Output
Hello World
C Comments
The comments in C are human-readable explanations or notes in
the source code of a C program. A comment makes the program
easier to read and understand. These are the statements that
are not executed by the compiler or an interpreter.
It is considered to be a good practice to document our code using
comments.
When and Why to use Comments in C programming?
1. A person reading a large code will be bemused if comments
are not provided about details of the program.
2. C Comments are a way to make a code more readable by
providing more descriptions.
3. C Comments can include a description of an algorithm to
make code understandable.
4. C Comments can be used to prevent the execution of some
parts of the code.
Types of comments in C
In C there are two types of comments in C language:
5. Single-line comment
What are Keywords?
Keywords are predefined or reserved words that have special
meanings to the compiler. These are part of the syntax and
cannot be used as identifiers in the program. A list of keywords
in C or reserved words in the C programming language are
mentioned below:
What is a variable in C?
A variable in C is a memory location with some name that helps
store some form of data and retrieves it when required. We can
store different types of data in the variable and reuse the same
variable for storing some other data any number of times.
C Variable Syntax
The syntax to declare a variable in C specifies the name and the
type of the variable.
data_type variable_name = value; // defining single variable
or
data_type variable_name1, variable_name2; // defining multiple variable
Here,
data_type: Type of data that a variable can store.
variable_name: Name of the variable given by the
user.
value: value assigned to the variable by the user.
Example
int var; // integer variable
char a; // character variable
float fff; // float variables
Data Types
Data types in C are fundamental building blocks that define the
type of data a variable can hold. They are essential for efficient
memory management and ensuring that operations on variables
are performed correctly. Let's discuss some common data types
in C:
1.int: This is used to store integer values. It typically uses 4 bytes
of memory on most systems and can hold both positive and
negative whole numbers.
2.char: This data type is used to store a single character. It usually
occupies 1 byte of memory and can represent characters like
letters, digits, or symbols.
3.float: Float is used to store floating-point numbers, which are
numbers with a decimal point. It usually takes 4 bytes of
memory and is suitable for representing real numbers with
decimal places.
4. double: Double is similar to float but uses double the memory
(typically
8 bytes). It provides higher precision and is often used when
more accurate floating-point calculations are required.
5.short: Short is used to store small integers. It usually takes 2
bytes of memory and can represent a smaller range of integers
compared to the 'int' data type.
6.long: Long is used to store larger integers. It usually takes 4 or 8
bytes of memory (depending on the system) and can represent
a broader range of integers compared to 'int'.
7.unsigned: This modifier is often used with integer data types to
indicate that the variable can only store non-negative values.
For example, 'unsigned int' can only hold positive integers.
8.long long: This is an extension of the 'long' data type,
introduced to allow even larger integer values. It typically takes
8 bytes of memory.
9._Bool: This data type can hold only two values - 0 or 1,
representing false and true, respectively. It usually occupies 1
byte of memory.
10.void: Void is a special data type that is used to indicate that a
Basic Input and Output in C
C language has standard libraries that allow input and output in
a program. The stdio.h or standard input output library in C that
has methods for input and output.
scanf()
The scanf() method, in C, reads the value from the console as
per the type specified. Syntax:
scanf(“%X”, &variableOfXType);
where %X is the format specifier in C. It is a way to tell the
compiler what type of data is in a variable and & is the address
operator in C, which tells the compiler to change the real value
of this variable, stored at this address in the memory.
printf()
The printf() method, in C, prints the value passed as the
parameter to it, on the console screen. Syntax:
printf(“%X”, variableOfXType);
where %X is the format specifier in C. It is a way to tell the
compiler what type of data is in a variable and & is the address
operator in C, which tells the compiler to change the real value
of this variable, stored at this address in the memory.
Operators in C
C Operators are symbols that represent operations to be
performed on one or more operands. C provides a wide range
be classified which
of operators, into different
can categories based on their
functionality.
Operators are used for performing operations on variables
and values.
Types of Operators in C
C has many built-in operators and can be classified into 6
types:
1. Arithmetic Operators
2. Relational Operators
3. Logical Operators
4. Bitwise Operators
5. Assignment Operators
6. Other Operators
i.sizeof operator
ii.Comma Operator
iii.Conditional Operator
iv. dot (.) and arrow (->)
Operators
v. Cast Operator
vi. &,* Operator
Types of Conditional Statements in
C/C+
+
C – Loops
Loops in programming are used to repeat a block of code until
the specified condition is met. A loop statement allows
programmers to execute a statement or group of statements
multiple times without repetition of code.
C Functions
A function in C is a set of statements that when called perform
some specific task. It is the basic building block of a C program
that provides modularity and code reusability. The programming
statements of a function are enclosed within { } braces,
having certain meanings and performing certain operations.
They are also called subroutines or procedures in other
languages.
Syntax of Functions in C
The syntax of function can be divided into 3 aspects:
Function
Declaration
Function
Definition
Function Declarations
Function Calls
In a function declaration, we must provide the function name, its
return type, and the number and type of its parameters. A
function declaration tells the compiler that there is a function
with the given name defined somewhere else in the program.
Function Definition
The function definition consists of actual statements which are
executed when the function is called (i.e. when the program
control comes to the function).
A C function is generally defined and declared in a single step
because the function definition always starts with the function
declaration so we do not need to declare it explicitly.
Function Call
A function call is a statement that instructs the compiler to
execute the function. We use the function name and parameters
in the function call.
In the below example, the first sum function is called and
10,30 are passed to the sum function. After the function call
sum of a and b is returned and control is also returned back to
the main function of the program.
Types of Functions
There are two types of functions in C:
I. Library Functions
II. User Defined Functions
Library Function
A library function is also referred to as a “built-in function”. A
compiler package already exists that contains these functions,
each of which has a specific meaning and is included in the
package. Built-in functions have the advantage of being directly
usable without being defined, whereas user-defined functions
must be declared and defined before being used.
For Example:
pow(), sqrt(), strcmp(), strcpy() etc.
User Defined Function
Function that the programmer creates are known as User-
s Defined or “tailor-made functions”. User-defined
function functions can be and modified according to the need
s
Whenever ofwe
thewrite
programmer.
a function that is case-specific and is not
defined
improve in
d
any header file, we need to declare and define our own
functions according to the syntax.
Passing Parameters to Functions
The data passed when the function is being invoked is known as
the Actual parameters. In the below program, 10 and 30 are
known as actual parameters. Formal Parameters are the variable
and the data type as mentioned in the function declaration. In
the below program, a and b are known as formal parameter
We can pass arguments to the C function in two
ways:
1. Pass by Value
2. Pass by Reference
Pass by Value
Parameter passing in this method copies values from actual
parameters into formal function parameters. As a result, any
changes made inside the functions do not reflect in the caller’s
parameters.
Pass by Reference
The caller’s actual parameters and the function’s actual
parameters refer to the same locations, so any changes
made inside the function are reflected in the caller’s actual
parameters.
What is Array in C?
An array in C is a fixed-size collection of similar data items stored
in contiguous memory locations. It can be used to store the
collection of primitive data types such as int, char, float, etc.,
and also derived and user-defined data types such as pointers,
structures, etc.
Types of Array in C
There are two types of arrays based on the number of
dimensions it has. They are as follows:
i. One Dimensional Arrays (1D Array)
ii. Multidimensional Arrays
Strings in C
A String in C programming is a sequence of characters
terminated with a null character ‘\0’. The C String is stored as an
array of characters. The difference between a character array
and a C string is the string is terminated with a unique character
‘\0’.
C String Declaration Syntax
Declaring a string in C is as simple as declaring a one-
dimensional array. Below is the basic syntax for declaring a
string.
char string_name[size];
Similarities between C and C++ are:
Both the languages have a similar syntax.
Code structure of both the languages are same.
The compilation of both the languages is similar.
They share the same basic syntax. Nearly all of C’s operators
and keywords are also present in C++ and do the same
thing.
C++ has a slightly extended grammar than C, but the basic
grammar is the same.
Basic memory model of both is very close to the
hardware.Same notions of stack, heap, file-scope and static
variables are present in both the languages.
C C++
C was developed by Dennis
C++ was developed by
Ritchie between the year 1969
Bjarne Stroustrup in
and 1973 at AT&T Bell Labs.
1979.
C does no support C++
polymorphism, encapsulation, supports polymorphism,
and inheritance which means encapsulation, and inheritance
that C does not support object because it is an object oriented
oriented programming. programming language.
C is (mostly) a subset of C++. C++ is (mostly) a superset of C.
Number of keywords in C: Number of keywords in C++:
* C90: 32 * C++98: 63
* C99: 37 * C++11: 73
* C11: 44 * C++17: 73
* C23: 59 * C++20: 81
C++ is known as hybrid
For the development of code, language because C++
C supports procedural supports
programming. both procedural and object
oriented
programming paradigms.
Data and functions are separated
Data and functions are
in C because it is a procedural
encapsulated together in form
programming language.
of an object in C++.
C C++
Data is hidden by the
C does not support information Encapsulation to ensure that
hiding. data structures and operators
are used as intended.
Built-in data types is supported in C. Built-in & user-defined data
types is supported in
C++.
C is a function driven language C++ is an object driven
because C is a procedural language because it is an
programming language. object oriented
programming.
Function and operator Function and operator
overloading is not overloading is
supported in C. supported by C++.
C is a function-driven language. C++ is an object-driven language
Functions in C are not defined Functions can be used
inside structures. inside a structure in
C++.
Namespace features are not Namespace is used by C++,
present inside the C. which avoid name
collisions.
Standard IO header is stdio.h. Standard IO header is iostream.h.
Reference variables are not Reference variables are
supported by C. supported by C+
+.
Virtual and friend functions Virtual and friend functions
are not supported by are supported by
C. C++.
C does not support inheritance. C++ supports inheritance.
Instead of focusing on data, C C++ focuses on data instead of
focuses on method or focusing on method or
process. procedure.
C
C++ provides new operator for
provides malloc() and calloc() memory allocation and delete
functions for dynamic memory operator for memory de-
allocation, allocation.
and free() for memory de-allocation.
C C++
Direct support for exception Exception handling is
handling is not supported by C+
supported by C. +.
scanf() and printf() functions are cin and cout are used for
used for input/output in input/output in
C. C++.
C structures don’t have C ++ structures have access
access modifiers. modifiers.
C follows the top-down approach C++ follows the Bottom-up approach
Strict type checking in done in C+
There is no strict type checking +. So many programs that run
in C programming well in C compiler will result in
language. many warnings and errors under
C++ compiler.
C does not support overloading C++ does support overloading
Type punning with unions is
Type punning with unions is allows
undefined behavior (except in
(C99 and later)
very specific circumstances)
Named initializers may appear Named initializers must match the
out of order data layout of the struct
File extension is “.c” File extension is “.cpp” or “.c++”
or “.cc” or “.cxx”
Meta-programming: macros + Meta-programming: templates
_Generic() (macros are still supported but
discouraged)
There are 32 keywords in the C There are 97 keywords in the C++
Dynamic Memory Allocation
in C using malloc(), calloc(), free() and
realloc()
Since C is a structured language, it has some fixed rules for
programming. One of them includes changing the size of an
array. An array is a collection of items stored at contiguous
memory locations.
In C, `malloc()`, `calloc()`, `free()`, and `realloc()` are functions used for
dynamic memory allocation and management. They are
essential for working with data structures of varying sizes at
runtime. Let's discuss each of these functions:
1. malloc(): (Memory Allocation):
- Prototype: void* malloc(size_t size);
-Purpose: `malloc()` is used to allocate a block of memory of the
specified size in bytes. It returns a pointer to the first byte of the
allocated memory block. This memory is uninitialized, meaning it
contains garbage values.
- Example:
int* arr = (int*)malloc(5 * sizeof(int));
2. calloc(): (Contiguous Allocation):
-Prototype: void* calloc(size_t num_elements, size_t
element_size);
- Purpose: `calloc()` is used to allocate a block of memory for
an array of elements, each of a specified size. It initializes the
memory to zero.
- Example:
int* arr = (int*)calloc(5, sizeof(int));
3. free(): (Memory Deallocation):
- Prototype: void free(void* ptr);
- Purpose: `free()`is used to release the memory allocated
using
`malloc()` or `calloc()`. It deallocates the memory and makes it
available for reuse.
- Example:
free(arr);
4. realloc(): (Reallocate Memory):
- Prototype: void* realloc(void* ptr, size_t new_size);
-Purpose: `realloc()` is used to change the size of a previously
allocated memory block. It can be used to make the block larger
or smaller. If the size is increased, the new memory is
uninitialized, and if it's decreased, excess data is truncated.
- Example:
int* resized_arr = (int*)realloc(arr, 10 * sizeof(int));
C++ What is OOP?
OOP stands for Object-Oriented Programming.
Procedural programming is about writing procedures or functions
that perform operations on the data, while object-oriented
programming is about creating objects that contain both data
and functions.
There are some basic concepts that act as the building blocks of
OOPs.
Classes & Objects
Abstraction
Encapsulation
Inheritance
Polymorphism
Classes and Objects?
A class is a template for objects, and an object is an instance of
a class. When the individual objects are created, they inherit all
the variables and functions from the class.
Abstraction:
Abstraction is a fundamental OOP concept that focuses on
displaying the essential features of an object while hiding the
unnecessary or complex details. It allows you to define the
interface of a class or object without revealing its internal
implementation.
Abstraction helps in managing complexity by providing a high-
level view of an object's functionality. It allows you to work with
objects at a conceptual level, making the code more
understandable and maintainable.
Encapsulation:
Encapsulation is the concept of bundling data (attributes) and
methods (functions) that operate on that data into a single unit
called a class. It enforces data hiding, meaning that the internal
state of an object is not directly accessible from outside the
class.
Access to the data is controlled through public methods
(getters and setters), which ensures data integrity and security.
Inheritance:
Inheritance is a mechanism in which one class (the child or
derived class) inherits the properties and behaviors of another
class (the parent or base class). This allows for code reuse and
the creation of a hierarchy of classes.
Inheritance promotes code reusability and enables the creation
of specialized classes based on existing ones.
Types Of Inheritance
C++ supports five types of inheritance:
Single inheritance
Multiple inheritance
Hierarchical inheritance
Multilevel inheritance
Hybrid inheritance
Polymorphism:
Polymorphism means "many forms" and is a key concept in OOP.
It allows objects of different classes to be treated as objects of a
common base class through a shared interface.
Polymorphism enables flexibility and extensibility in code by
allowing you to write code that can work with objects of different
types without knowing their specific types at compile time. This
is often achieved through function overriding and interfaces in
C++.
OOP concepts in C++ provide a powerful way to design and organize
code. They help improve code modularity, reusability, and maintainability.
By representing real-world entities as objects with associated behaviors,
OOP makes it easier to model and solve complex problems.
Conclusion
Powerful and versatile languages: C, C++, and OOP can be used to
develop a wide variety of applications, from operating systems
and embedded systems to games and high-performance
computing software.
High degree of control: C gives programmers a high degree of
control over memory and system resources, which is essential
for developing low-latency, high-performance applications
.
Object-oriented programming: OOP makes code more modular,
reusable, and maintainable. High demand in the job market: C,
C++, and OOP are in high demand in the job market, so learning
these languages can give you a competitive advantage.
Learning C, C++, and OOP gives me a powerful foundation for building a
wide variety of applications, from simple games to complex operating
systems.
Projects that can i make in future: Text-based adventure game,
calculator, Sudoku solver, Web server, Game of Life, file
compression program, Operating system kernel, game engine,
high-performance computing library
Reference
1.https://bard.google.com/chat/d3759b145494e
8a8
2.https://www.javatpoint.com/cpp-inheritance
3.https://www.geeksforgeeks.org/c-plus-plus/?re
f=shm
4.https://www.geeksforgeeks.org/c-progra
mming-
language/?ref=shm
5.https://www.w3schools.com/c/c_variables.
php