C and Data Structures Report
C and Data Structures Report
KURUKSHETRA
First I would like to thank ORBIT Computer Educations, HYDERABAD for giving me an opportunity as to enroll
in the well planned course. I surely have learnt a lot from here. I encouraged brilliant practical learning through real
time testing and coding, which is impossible to achieve through books. I hope that everything I have learnt here will
be helpful in future.
I take this opportunity to express my deep sense of gratitude to Mr. Bala Krishna for his immense support and
guidance. He has helped me throughout and acted as our guide. I would like to thank him for having immense
patience while teaching me about algorithms and giving his precious time to me.
I would also like to thank all my friends who helped me throughout the course by giving their valuable time and
guidelines to complete the course, and so many other countless people of ORBIT Team for helping me during my
course.
ANIL JARUPULA
11610576
IT-5
Purpose
Logic Building
Coding in its most essential form is nothing but problem solving. It deals with figuring out logical solutions and
implementing them optimally. The process of learning to code, transforms a person into an analytical thinker with
strong mathematical and logical skills.
Career Transformation
Having been a standalone field for a long time, the software industry has now come neck to neck with most of the
other industries by close coordination and easy integration. From the smallest light bulb to the most massive
aircrafts, every industry is now implementing the use of computer programming for the convenience of the end user.
A good command over coding can thus open a wide variety of employment opportunities.
Global Impact
Computer programming these days is also being used to address some of the biggest issues that humankind is facing
with like climate change, healthcare, education and many more. More than being a developer or a designer,
knowledge of coding can also help you become the harbinger of the change that you want to see.
Computational Thinking
Computational Thinking is the ability to communicate your thoughts in a structured and logical way. This type of
thought process is like the step by step instructions that are coded into a computer. It is also the process of thinking
up and then solving problems in a methodical way that could be replicated by a machine. Software engineers,
computer programmers, and logistics specialists use this method of thinking to solve problems. Computational
thinking is a combination of advanced mathematics, algorithm development and logic. The development of
computational thinking involves considering a problem and breaking it down into single-action steps. Each of these
steps is handled in the most efficient way possible. There is also an element of abstraction in computationalthinking,
which allows your child to move from a specific solution to a specific problem and generalize it to other situations.
The result is a paradigm shift in thinking about how the world works.
About ORBIT
ORBIT is a privately-held company based in Hyderabad, India. Since 2008, we have been helping IT professionals
get better at what they do by providing an extensive range of training services on emerging technologies. Always
pushing the envelope, we are constantly exploring new fields of knowledge as well as new training methodologies to
better serve our clients.The company is led by a team of experts , with accumulated experience of tens of years of
software development, architectural design and project management. The team has provided most authentic,
comprehensive and high quality training services to good number of companies in recent years, ranging from small
start-ups to large enterprises.
We pride ourselves by standing by our commitment to help IT professionals get to the next level, by being in tune
with our customers' actual needs, and by always delivering on what we promise, all while having fun doing it.
Always pushing the envelope, we are constantly exploring new fields of knowledge as well as new training
methodologies to better serve our clients.
ORBIT trains with the aim of creating "Innovative Software Engineers" who can solove problems with ease. Some
of his time will be given to consulting engaments of corporates. It likes to teach Problem solving & Cutting edge
technology courses that impacts practical development/design/architecturing..
CONTENT
• INTRODUCTION
• Introduction of c
• Contents in C
• Integer Types
• Loops:C
• Arrays
• Pointers in c
• Strings in c
• Structures in c
• Union
• Arrays
• Linked List
• Stacks
• Queue
• Sorting techniques
• Searching techniques
• INTRODUCTION
• INTRODUCTION OF C :
C is a general-purpose, high-level language that was originally developed by Dennis M. Ritchie to develop the
UNIX operating system at Bell Labs. C was originally first implemented on the DEC PDP-11 computer in 1972.
In 1978, Brian Kernighan and Dennis Ritchie produced the first publicly available description of C, now known as
the K&R standard.
The UNIX operating system, the C compiler, and essentially all UNIX application programs have been written in
C. C has now become a widely used professional language for various reasons −
• Easy to learn
• Structured language
Facts about C :
• C was invented to write an operating system called UNIX.
• C is a successor of B language which was introduced around the early 1970s.
• The language was formalized in 1988 by the American National Standard Institute (ANSI).
• Today C is the most widely used and popular System Programming Language.
Today's most popular Linux OS and RDBMS MySQL have been written in C.
Why to use C ?
C was initially used for system development work, particularly the programs that make-up the operating system. C
was adopted as a system development language because it produces code that runs nearly as fast as the code written
in assembly language. Some examples of the use of C might be −
• Operating Systems
• Language Compilers
• Assemblers
• Text Editors
C Programs
A C program can vary from 3 lines to millions of lines and it should be written into one or more text files with
extension ".c"; for example, hello.c.
• Preprocessor Commands
• Functions
• Variables
• Comments
#include <stdio.h>
int main() {
return 0;
}
• 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 intmain() is the main function where the program execution begins.
• The next line printf(...) is another function available in C which causes the message "Hello World" to be
displayed on the screen.
• The next line return 0; terminates the main() function and returns the value 0.
Output:Hello World .
A data structure is a way to store and organize data in order to facilitate access and modifications. More
precisely, a data structure is a collection of data values, the relationships among them, and the functions or
operations that can be applied to the data. No single data structure works well for all purposes, and so it is
important to know the strengths and limitations of several of them.
• Usage
Different kinds of data structures are suited to different kinds of applications, and some are highly
specialized to specific tasks. For example, relational databases commonly use B-tree indexes for data
retrieval while compiler implementations usually use hash tables to look up identifiers.
Data structures provide a means to manage large amounts of data efficiently for uses such as
large databases and internet indexing services. Usually, efficient data structures are key to designing
efficient algorithms. Some formal design methods and programming languages emphasize data structures,
rather than algorithms, as the key organizing factor in software design. Data structures can be used to
organize the storage and retrieval of information stored in both main memory and secondary memory.
• Implementation
Data structures are generally based on the ability of a computer to fetch and store data at any place
in its memory, specified by a pointer—a bit string, representing a memory address, that can be itself stored
in memory and manipulated by the program. Thus, the array and record data structures are based on
computing the addresses of data items with arithmetic operations; while the linked data structures are based
on storing addresses of data items within the structure itself. Many data structures use both principles,
sometimes combined in non-trivial ways
• DATA TYPES IN C:
Data types in c are used for declaring variables or functions of different types. The type of a variable
determines how much space it occupies in storage and how the bit pattern stored is interpreted.
1 Basic Types:
They are arithmetic types and are further classified into: (a) integer types and (b) floating-point
types.
2 Enumerated types
They are again arithmetic types and they are used to define variables that can only assign certain
discrete integer values throughout the program.
4 Derived types
They include (a) Pointer types, (b) Array types, (c) Structure types, (d) Union types and (e)
Function types.
• Integer Types :
The following table provides the details of standard integer types with their storage sizes and value ranges –
To get the exact size of a type or a variable on a particular platform, you can use the sizeof operator. The
expressions size of(type) yields the storage size of the object or type in bytes.
• Floating-Point Types :
The following table provide the details of standard floating-point types with storage sizes and value ranges and their
precision –
• LOOPS: C
when a block of code needs to be executed several number of times. In general, statements are executed
sequentially: The first statement in a function is executed first, followed by the second, and so on.
Programming languages provide various control structures that allow for more complicated execution paths.
A loop statement allows us to execute a statement or group of statements multiple times. Given below is the
general form of a loop statement in most of the programming languages −
C programming language provides the following types of loops to handle looping requirements.
1 while loop: Repeats a statement or group of statements while a given condition is true. It tests the
condition before executing the loop body.
2 for loop: Executes a sequence of statements multiple times and abbreviates the code that manages
the loop variable.
3 do...while loop: It is more like a while statement, except that it tests the condition at the end of the
loop body.
4 nested loops: You can use one or more loops inside any other while, for, or do..while loop.
1 break statement: Terminates the loop or switch statement and transfers execution to the statement
immediately following the loop or switch.
2 continuestatement:Causes the loop to skip the remainder of its body and immediately retest its
condition prior to reiterating.
#include <stdio.h>
int main () {
for( ; ; ) {
} return 0;
When the conditional expression is absent, it is assumed to be true. You may have an initialization and increment
expression, but C programmers more commonly use the for(;;) construct to signify an infinite loop.
• ARRAYS:
Arrays a kind of data structure that can store a fixed-size sequential collection of elements of the same type. An
array is used to store a collection of data, but it is often more useful to think of an array as a collection of variables
of the same type.
Instead of declaring individual variables, such as number0, number1, ..., and number99, you declare one array
variable such as numbers and use numbers[0], numbers[1], and ..., numbers[99] to represent individual variables. A
specific element in an array is accessed by an index.
All arrays consist of contiguous memory locations. The lowest address corresponds to the first element and the
highest address to the last element.
Declaring Arrays in C :
To declare an array in C, a programmer specifies the type of the elements and the number of elements required by
an array as follows –
typearrayName [ arraySize ];
This is called a single-dimensional array. The arraySize must be an integer constant greater than zero and type can
be any valid C data type. For example, to declare a 10-element array called A of type int, use this statement –
IntA[10]
Initializing Arrays :
You can initialize an array in C either one by one or using a single statement as follows −
int A[5]={100,20,30,40,50};
The number of values between braces { } cannot be larger than the number of elements that we declare for the
array between square brackets [ ].
Int b=A[5];
The above statement will take the 5th element from the array and assign the value to b variable.
• POINTERS in C:
Pointers in C are easy and fun to learn. Some C programming tasks are performed more easily with pointers, and
other tasks, such as dynamic memory allocation, cannot be performed without using pointers. So it becomes
necessary to learn pointers to become a perfect C programmer. Let's start learning them in simple and easy steps.
As you know, every variable is a memory location and every memory location has its address defined which can be
accessed using ampersand (&) operator, which denotes an address in memory. Consider the following example,
which prints the address of the variables defined −
#include <stdio.h>
int main () {
int var1;
char var2[10];
return 0;
When the above code is compiled and executed, it produces the following result-
type *var-name;
Here, type is the pointer's base type; it must be a valid C data type and var-name is the name of the pointer
variable. The asterisk * used to declare a pointer is the same asterisk used for multiplication. However, in this
statement the asterisk is being used to designate a variable as a pointer
The actual data type of the value of all pointers, whether integer, float, character, or otherwise, is the same, a long
hexadecimal number that represents a memory address. The only difference between pointers of different data
types is the data type of the variable or constant that the pointer points to.
NULL Pointers
It is always a good practice to assign a NULL value to a pointer variable in case you do not have an exact address
to be assigned. This is done at the time of variable declaration. A pointer that is assigned NULL is called
a nullpointer.
The NULL pointer is a constant with a value of zero defined in several standard libraries. Consider the following
program −
#include <stdio.h>
int main () {
int *ptr = NULL;
return 0;
When the above code is compiled and executed, it produces the following result –the value of ptr is 0.
In most of the operating systems, programs are not permitted to access memory at address 0 because that memory
is reserved by the operating system. However, the memory address 0 has special significance; it signals that the
pointer is not intended to point to an accessible memory location. But by convention, if a pointer contains the null
(zero) value, it is assumed to point to nothing.
• STRINGS in C:
Strings are actually one-dimensional array of characters terminated by a nullcharacter '\0'. Thus a null-terminated
string contains the characters that comprise the string followed by a null.
The following declaration and initialization create a string consisting of the word "Hello". To hold the null
character at the end of the array, the size of the character array containing the string is one more than the number of
characters in the word "Hello."
If you follow the rule of array initialization then you can write the above statement as follows −
Actually, you do not place the null character at the end of a string constant. The C compiler automatically places
the '\0' at the end of the string when it initializes the array. Let us try to print the above mentioned string −
#include <stdio.h>
int main () {
return 0;
When the above code is compiled and executed, it produces the following result
4 strcmp(s1, s2); Returns 0 if s1 and s2 are the same; less than 0 if s1<s2; greater than 0 if s1>s2.
5 strchr(s1, ch); Returns a pointer to the first occurrence of character ch in string s1.
6 strstr(s1, s2); Returns a pointer to the first occurrence of string s2 in string s1.
• STRUCTURES in C:
Arrays allow to define type of variables that can hold several data items of the same kind. Similarly structure is
another user defined data type available in C that allows to combine data items of different kinds.Structures are
used to represent a record.
Defining a Structure :
To define a structure, you must use the struct statement. The struct statement defines a new data type, with more
than one member. The format of the struct statement is as follows −
member definition;
member definition;
...
member definition;
The structure tag is optional and each member definition is a normal variable definition, such as int i; or float f; or
any other valid variable definition. At the end of the structure's definition, before the final semicolon, you can
specify one or more structure variables but it is optional.
• UNION:
A union is a special data type available in C that allows to store different data types in the same memory location.
You can define a union with many members, but only one member can contain a value at any given time. Unions
provide an efficient way of using the same memory location for multiple-purpose.
Defining a Union :
To define a union, you must use the union statement in the same way as you did while defining a structure. The
union statement defines a new data type with more than one member for your program. The format of the union
statement is as follows −
member definition;
member definition;
...
member definition;
The union tag is optional and each member definition is a normal variable definition, such as int i; or float f; or any
other valid variable definition. At the end of the union's definition, before the final semicolon, you can specify one
or more union variables but it is optional.
Applications:
• Linked List: Linked list is a linear collection of data elements, in which linear order is not given by
their physical placement in memory. Instead, each element points to the next. It is a data
structure consisting of a group of nodes which together represent a sequence.
Applications:
• Linked Lists can also be used to implement Graphs. (Adjacency list representation of Graph).
• Implementing Hash Tables :- Each Bucket of the hash table can itself be a linked list. (Open chain
hashing.
• Stack :A stack is an abstract data type that serves as a collection of elements, with two principal
operations:
• push, which adds an element to the collection, and
• pop, which removes the most recently added element that was not yet removed.
The order in which elements come off a stack gives rise to its alternative name, LIFO (last in, first out).
Applications :
• Expression evaluation
2.3. Queue : A queue is a particular kind of ADT in which the entities in the collection are
kept in order and the principle (or only) operations on the collection are the addition of entities to
the rear terminal position, known as enqueue, and removal of entities from the front terminal
position, known as dequeue. This makes the queue a FIFO data structure
Applications :
• When a resource is shared among multiple consumers. Examples include CPU scheduling, Disk
Scheduling..
• When data is transferred asynchronously (data not necessarily received at same rate as sent)
between two processes. Examples include IO Buffers, pipes, file IO, etc.
• Sorting techniques ;
• Internal or external
• E.g ;quick sort, selection sort, bubble sort, insertion sort
• 0th element compared with all elements and swapped if its found to be greater
• 0th element compared with 1st element and swapped it its found to be greater
• Then first element is compare with 2nd and the process continues
• Searching
• No of comparisions can be reduced by using a sentinel value at the end of the list
• Checks center element in the list ,if not a match then list is divided into two halves ,left and right