0% found this document useful (0 votes)
43 views24 pages

EOI-1 Saurabh C - Programming

Uploaded by

0126ds201025
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
43 views24 pages

EOI-1 Saurabh C - Programming

Uploaded by

0126ds201025
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 24

Internship

On

C Programming Language
At

Oriental College of Technology


Submitted in Partial fulfilment for the Award of the degree of

Bachelor of Technology
In
“DATA SCIENCE”

Submitted to

RAJIV GANDHI PROUDYOGIKI VISHWAVIDHYALAYA, BHOPAL (M.P)

Submitted by

Saurabh Tiwari (0126CD201054


) Under the guidance of:
Asst. Prof. Umesh Joshi

ORIENTAL COLLEGE OF TECHNOLOGY, BHOPAL

Department of Data Science


Approved by AICTE New Delhi & Govt. of MP
Affiliated to Rajiv Gandhi Proudyogiki Vishwavidhyalaya, Bhopal
December 2021
Phone No.-0755-2529015, 2529016
Fax: 0755-2529472
E-mail: [email protected]
Website: http://www.oriental.ac.in/oct-bhopal/

ORIENTAL COLLEGE OF TECHNOLOGY, BHOPAL


Approved by AICTE, New Delhi & Govt. of M.P. Affiliated to Rajiv Gandhi Proudyogiki Vishwavidyalaya, Bhopal
Oriental Campus, Raisen Road, Bhopal-462021 (MP) INDIA

DEPARTMENT OF DATA SCIENCE

CANDIDATE’S DECLARATION

I hereby declare that the Internship report on C Programming Language which


is being presented here for the partial fulfilment of the requirement of Degree
of “Bachelor of Technology” has been carried out at Oriental College of
Technology . The technical information provided in this report is presented
with due permission of the authorities from the providing internship
organization.

Signature of Student
Name of Student:Saurabh
Tiwari
Registration No.:
0126CD201054
Phone No.-0755-2529015, 2529016
Fax: 0755-2529472
E-mail: [email protected]
Website: http://www.oriental.ac.in/oct-bhopal/

ORIENTAL COLLEGE OF TECHNOLOGY, BHOPAL


Approved by AICTE, New Delhi & Govt. of M.P. Affiliated to Rajiv Gandhi Proudyogiki Vishwavidyalaya, Bhopal
Oriental Campus, Raisen Road, Bhopal-462021 (MP) INDIA

DEPARTMENT OF DATA SCIENCE

CERIFICATE OF INSTITUTE

This is to certify that Mr. / Ms. Saurabh Tiwari of B. Tech. Data Science Department
Enrolment No. 0126CD201054 has completed / partially completed / not completed his /
her Internship during the academic year 2021-2021 as partial fulfilment of the Bachelor of
Technology in Data Science.

Asst. Prof. Umesh Joshi Dr. L. K. Vishwamitra


Coordinator Head of Department, Data Science
ACKNOWLEDGEMENT

It gives me immense pleasure to express my deepest sense of gratitude and sincere thanks to
my supervisors (Mr. Kuldeep Mishra), Training Head, Department of Training, Oriental
College of Technology for their valuable guidance encouragement and help during internship.

Saurabh Tiwari

0126CD201054
CHAPTER -1
INTRODUCTIONOF
C
C is a procedural programming language. It was initially developed by Dennis Ritchiein the
year 19702. It was mainly developed as a system programming language to writean
operatingsystem.

 Structure of a C program
After the above discussion,we can formally as sess the structure of a C program .
Bystructure, it is mean that any program can be written in this structure only. Writing a
Cprograminany other structure will hence lead to a Compilation Error.

C First Program-
#include<stdio.h>
int main
{
Print (“Hello
World”); Return 0;
}

1 - Structure of a C program
After the above discussion, we can formally assess the structure of a C
program. By structure, it is meant that any program can be written in
this structure only.
Writing a C program in any other structure will hence lead to a
Compilation Error. The structure of a C program is as follows:
Features of c programming
#include<stdio.h>
Header
Main( ) int main()
{

Variable Declaration Int a=10;

Body Printf(“%d” ,a);

Return Return 0;
}

• C is a robust language with a rich set of built-in functions andoperators.

• Programs written in C are efficient andfast.


• C is highly portable; programs once written in C can be run on other machines
with minor or nomodification.

• C is a collection of C library functions; we can also create our function and add it to
the Clibrary.

• C is easilyextensible

Advantages ofC

C - Program Structure And Basic Syntex


Chapter=>2
Programming structure

Before we study the basic building blocks of the C programming language, let us look
at a bare minimum C program structure so that we can take it as a reference in the upcoming
chapters.

Hello World Example

A C program basically consists of the following parts −

 Preprocessor Commands

 Functions

 Variables

 Statements & Expressions

 Comments

Let us look at a simple code that would print the words "Hello World" –

#include <stdio.h>

int main()

Printf(“Hello world”);

Return 0;

Let us take a look at the various parts of the above program −

 The first line of the program #include <stdio.h> is a preprocessor command,


which tells a C compiler to include stdio.h file before going to actual compilation.

 The next line int main() is the main function where the program execution begins.

 The next line /*...*/ will be ignored by the compiler and it has been put to add
additional comments in the program. So such lines are called comments in
the program.
Basic syntax

You have seen the basic structure of a C program, so it will be easy to understand other
basic building blocks of the C programming language.

Tokens in C

A C program consists of various tokens and a token is either a keyword, an identifier, a


constant, a string literal, or a symbol. For example, the following C statement consists of
five tokens –

Printf(“Hello world /n ”);

 The individual tokens are

– Printf (“Hello world”);

Semicolons

In a C program, the semicolon is a statement terminator. That is, each individual


statement must be ended with a semicolon. It indicates the end of one logical entity.

 Given below are two different statements

– Printf(“ Hello world ”);

Return 0;

Comments

Comments are like helping text in your C program and they are ignored by the
compiler. They start with /* and terminate with the characters */ as shown below –

 /* My first programming in C*/

You cannot have comments within comments and they do not occur within a string or
character literals.

Identifiers

A C identifier is a name used to identify a variable, function, or any other user-


defined item. An identifier starts with a letter A to Z, a to z, or an underscore '_' followed
by zero or more letters, underscores, and digits (0 to 9).
CHAPTER-3

Iteration And Methods In


C

Iteration

Iteration is the process where a set of instructions or statements is executed repeatedly for
a specified number of time or until a condition is met. These statements also alter the
control flow of the program and thus can also be classified as control statements in C
Programming Language.

Iteration statements are most commonly know as loops.

A loop in C consists of two parts, a body of a loop and a control statement. The control
statement is a combination of some conditions that direct the body of the loop to execute
until the specified condition becomes false. The purpose of the C loop is to repeat the same
code a number of times.

Also the repetition process in C is done by using loop control instruction. There are three
types of looping statements:

 For Loop
 While Loop
 Do-while loop

A loop basically consists of three parts:

initialization, test expression, increment/decrement or update value. For the different type
of loops, these expressions might be present at different stages of the loop.

 Types of Loops in C

Depending upon the position of a control statement in a program, looping statement in

C is classified into two types:

1. Entry controlled loop


In an entry control loop in C, a condition is checked before executing the body of a loop.
It is also called as a pre-checking loop.
Example: For loop and While loop.

 Exit controlled loop


In an exit controlled loop, a condition is checked after executing the body of a loop. It is
also called as a post-checking loop.

Example: do-while loop.

 For loop
For loop is the most commonly used looping technique. The reason why it is so
popular is because it has all the three parts of the loop: initialization, test
expression, update expression in the same line.

 The syntax of a for loop is:

for(initialization; test expression; update expression)


{
//body of loop
}
 Initialisation Expression: In this expression we have to initialize the loop counter
to some value.
Example: int i=1;
 Test Expression: Here we tests the condition .If the condition evaluates to true
Then we will execute the body of the loop and go to update expression otherwise
we will exit from the loop.

Example: i<=10;
 Update Expression: After executing loop body this expression
increments/decrements the loop variable by some value.
Example: i++/i--;
Equivalent flow chart showing for loop:
Types of for loops

For loops can also be of different types, let’s have a look at some of them:

 Infinite for loop

An infinite for loop is the one which goes on repeatedly for infinite times. This can happen
in two cases:

 When the loop has no

expressions Example:for(;;)

printf("CodinGeek");

 When the test condition never becomes false.

Example:for(i=1;i<=5;)

printf("CodinGeek");

 Empty for loop

An empty for loop is the one which has got no body. In simple words, it contains no
statements or expressions in its body. It can be used for time-consuming purposes. Each
iteration takes its own time for compilation and execution. Usually, it is too small to be
of any significant importance. Here’s the syntax of an empty for loop:

for(int i=1;i<=5;i++)

{}

 Nested for loop


In the previous tutorial of if-else control statements we saw what nested if is.
Similarly, nested for loop refers to the process of having one loop inside another loop.
We can have multiple loops inside one another. The body of one ‘for’ loop contains
the other and so on. The syntax of a nested for loop is as follows(using two for loops):

for(initialization; test; update)

for(initialization;test;update)//using another variable

//body of the inner loop

//body of outer loop(might or might not be present)

 While loop

A while loop allows a part of the code to be executed multiple times depending upon a given
boolean condition. It can be viewed as a repeating if statement. The while loop is mostly
used in the case where the number of iterations is not known in advance.

The syntax of while loop in c language is given below:

while(condition)

//code to be executed

Flow chart:
CHAPTER-4
Arrays and Strings in C Programming Language

An array is defined as the collection of similar type of data items stored at contiguous
memory locations. Arrays are the derived data type in C programming language which can
store the primitive type of data such as int, char, double, float, etc. It also has the capability
to store the collection of derived data types, such as pointers, structure, etc. The array is the
simplest data structure where each data element can be randomly accessed by using its index
number.

C array is beneficial if you have to store similar elements. For example, if we want to store
the marks of a student in 6 subjects, then we don't need to define different variables for the
marks in the different

1) Code Optimization: Less code to the access the data.

2) Ease of traversing: By using the for loop, we can retrieve the elements of an array easily.

3) Ease of sorting: To sort the elements of the array, we need a few lines of code only.

4) Random Access: We can access any element randomly using the array.

4.1.3 Disadvantage of C Array

• Fixed Size: Whatever size, we define at the time of declaration of the array, we can't
exceed the limit. So, it doesn't grow the size dynamically like LinkedList which we will
learn later.

4.1.4 Declaration of C Array

We can declare an array in the c language in the following way.

• data_type array_name[array_size];

Now, let us see the example to declare the array.

• int marks[5];
.2.1 How to declare a string?

Here's how you can declare strings:

char s[5];

String Declaration in C

Here, we have declared a string of 5 characters.

4.2.2 How to initialize strings?

You can initialize strings in a number of ways.

char c[] = "abcd";

char c[50] = "abcd";

char c[] = {'a', 'b', 'c', 'd', '\0'};

char c[5] = {'a', 'b', 'c', 'd', '\0'};

String Initialization in C

Let's take another

example:

char c[5] = "abcde";

Here, we are trying to assign 6 characters (the last character is '\0') to a char array having 5
characters. This is bad and you should never do this.

4.2.3 Assigning Values to Strings

Arrays and strings are second-class citizens in C; they do not support the assignment operator
once it is declared. For example,
CHAPTER-5

Pointer

Pointer :

• is a variable that contains address of another variable. It means it is a variable but


this variable contains address or memory address of any other variable.

• can be of type int, char, array, function, or any other pointer.


• Its size depends on architecture.
• Pointers in C Language can be declared using *(asterisk symbol).

#include<stdio.h>

Int main()

Int x=5;

Int *a =&x; Printf(“%d\

n”,&x );

Printf(“%d”,a );

Return 0;

So, pointers are nothing just a variable which stores the address of other variables and by
using pointers we can access other variables too and can even manipulate them.

Now let’s see about some of the operators which we use with Pointers :

Address of Operator (&) :

· It is a unary operator.

· Operand must be the name of the

variable. &operator gives address no. Of

variable.

· & is also known as “Referencing

Operator”. Indirection Operator :


Chapters-6

Header Files and Exception Handlings

C language has numerous libraries that include predefined functions to make programming
easier. In C language, header files contain the set of predefined standard library functions.
Your request to use a header file in your program by including it with the C preprocessing
directive “#include”. All the header file have a ‘.h’ an extension. By including a header file,
we can use its contents in our program.

A header file contains:

Function definitions

Data type

definitions Macros

It offers the above features by importing them into the program with the help of a
preprocessor directive “#include”. These preprocessor directives are used for instructing
compiler that these files need to be processed before compilation.

In C program should necessarily contain the header file which stands for standard input and
output used to take input with the help of scanf() and printf() function respectively.

• Pre-existing header files: Files which are already available in C/C++ compiler we
just need to import them.

• User-defined header files: These files are defined by the user and can be
imported using “#include”.

• -existing header files: Files which are already available in C/C++ compiler we
just need to import them.

• -defined header files: These files are defined by the user and can be imported
using “#include”.

Syntax:

#include <filename.h>

Or

#include “filename.h”
CHAPTER-7

Structure And Union In C

7.1 Structures

Structure is a user-defined datatype in C language which allows us to combine data of


different types together. Structure helps to construct a complex data type which is more
meaningful. It is somewhat similar to an Array, but an array holds data of similar type only.
But structure on the other hand, can store data of any type, which is practical more useful.

For example: If I have to write a program to store Student information, which will have
Student's name, age, branch, permanent address, father's name etc, which included string
values, integer values etc, how can I use arrays for this problem, I will require
something which can hold data of different types together.

In structure, data is stored in form of records.

7.1.1 Defining a structure

struct keyword is used to define a structure. struct defines a new data type which is a
collection of primary and derived data types.

Syntax:

struct [structure_tag]

{ //member variable 1

//member variable 2

//member variable 3...

}[structure_variables];

As you can see in the syntax above, we start with the struct keyword, then it's optional to
provide your structure a name, we suggest you to give it a name, then inside the curly
braces, we have to mention all the member variables, which are nothing but normal C
language variables of different types like int, float, array etc.

After the closing curly brace, we can specify one or more structure variables, again this is
optional.
7.1.2 Structure Initialization

Like a variable of any other datatype, structure variable can also be initialized at compile
time.

struct Patient

float height;

int weight;

int age;

};

struct Patient p1 = { 180.75 , 73, 23 };

//initialization or,

struct Patient p1;

p1.height = 180.75; //initialization of each member

separately p1.weight = 73;

p1.age = 23;

7.1.3 Array of Structure

We can also declare an array of structure variables. in which each element of the array will
represent a structure variable. Example : struct employee emp[5];

The below program defines an array emp of size 5. Each element of the array emp is
of type Employee.

#include<stdio.h>
struct Employee
{
char ename[10];
int sal;
};
struct Employee emp[5];
int i, j;
void ask()
{
for(i = 0; i < 3; i++)
{
printf("\nEnter %dst Employee record:\n", i+1);
printf("\nEmployee name:\t");
scanf("%s", emp[i].ename); printf("\
nEnter Salary:\t"); scanf("%d",
&emp[i].sal);
}
printf("\nDisplaying Employee record:\n");
for(i = 0; i < 3; i++)
{
printf("\nEmployee name is %s", emp[i].ename);
printf("\nSlary is %d", emp[i].sal);
}
}
void main()
{
ask();
}

7.1.4 Nested Structures

Nesting of structures, is also permitted in C language. Nested structures means, that one
structure has another stucture as member variable.

.1.7 Structure as Function Arguments

We can pass a structure as a function argument just like we pass any other variable or
an array as a function argument.

7.2 Unions

Unions are conceptually similar to structures. The syntax to declare/define a union is


also similar to that of a structure. The only differences is in terms of storage. In structure
each member has its own storage location, whereas all members of union uses a single
shared memory location which is equal to the size of its largest data member.
CHAPTER-8

Dynamic Memory Allocation And File Handling in C

8.1 Dynamic Memeory allocation

The process of allocating memory at runtime is known as dynamic memory allocation.


Library routines known as memory management functions are used for allocating and
freeing memory during execution of a program. These functions are defined
in stdlib.h header file.

Function Description

allocates requested size of bytes and returns a void pointer pointing to the
malloc()
first byte of the allocated space

allocates space for an array of elements, initialize them to zero and then returns
calloc()
a void pointer to the memory

free releases previously allocated memory

realloc modify the size of previously allocated space

Memory Allocation Process

Global variables, static variables and program instructions get their memory
in permanent storage area whereas local variables are stored in a memory area called Stack.

The memory space between these two region is known as Heap area. This region is used for
dynamic memory allocation during execution of the program. The size of heap keep change

8.1.1 Allocating block of Memory


malloc() function is used for allocating block of memory at runtime. This function reserves a
block of memory of the given size and returns a pointer of type void. This means that we can
assign it to any type of pointer using typecasting. If it fails to allocate enough space as
specified, it returns a NULL pointer.

Syntax:

void* malloc(byte-size)

Time for an Example: malloc()

int *x;

x = (int*)malloc(50 * sizeof(int)); //memory space allocated to variable x

free(x); //releases the memory allocated to variable x

calloc() is another memory allocation function that is used for allocating memory at
runtime. calloc function is normally used for allocating memory to derived data types such
as arrays and structures. If it fails to allocate enough space as specified, it returns
a NULL pointer.

Syntax:

void *calloc(number of items, element-size)

Copy

Time for an Example: calloc()

struct employee

char *name;

int salary;

};

typedef struct employee emp;

emp *e1;

e1 = (emp*)calloc(30,sizeof(emp));

realloc() changes memory size that is already allocated dynamically to a variable.

Syntax:
void* realloc(pointer, new-size)

Copy

Time for an Example: realloc()

int *x;

x = (int*)malloc(50 * sizeof(int));

x = (int*)realloc(x,100); //allocated a new memory to variable x

8.1.3 Diffrence between malloc() and calloc()

#Program to represent Dynamic Memory Allocation(using calloc())

#include <stdio.h>
#include <stdlib.h>
int main()
{
int i, n;
int *element;
printf("Enter total number of elements: ");
scanf("%d", &n);
/*
returns a void pointer(which is type-casted to int*)
pointing to the first block of the allocated space
*/
element = (int*) calloc(n,sizeof(int));
/
If it fails to allocate enough space as specified, it
returns a NULL pointer.
*/
if(element == NULL)
{
printf("Error.Not enough space available");
exit(0);
}
for(i = 0; i < n; i++)
{
/*
storing elements from the
user in the allocated space
*/
scanf("%d", element+i);
}
for(i = 1; i < n; i++)
{
if(*element > *(element+i))
{
*element = *(element+i);
}
}
printf("Smallest element is %d", *element);
return 0;
}

Output
Enter total number of elements:
542153
Smallest element is 1

8.2 File Handling

A file represents a sequence of bytes on the disk where a group of related data is stored. File
is created for permanent storage of data. It is a ready made structure.

In C language, we use a structure pointer of file type to declare a file.


Conclusion

As my internship in C programming comes to a close, I reflect on the invaluable lessons learned


and skills acquired throughout this enriching journey. Over the course of my internship, I have
delved deep into the intricacies of C programming, honing my ability to write efficient, structured
code and tackle complex problems with confidence.

Working alongside experienced professionals, I have gained firsthand exposure to real-world


development projects, learning the importance of collaboration, attention to detail, and adherence to
best practices. From mastering the fundamentals of data types and control structures to exploring
advanced concepts such as memory management and file handling, each challenge has propelled
my growth as a programmer.

You might also like