EOI-1 Saurabh C - Programming
EOI-1 Saurabh C - Programming
On
C Programming Language
At
Bachelor of Technology
In
“DATA SCIENCE”
Submitted to
Submitted by
CANDIDATE’S DECLARATION
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/
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.
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()
{
Return Return 0;
}
• 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
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.
Preprocessor Commands
Functions
Variables
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;
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
Semicolons
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 –
You cannot have comments within comments and they do not occur within a string or
character literals.
Identifiers
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.
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
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
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.
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:
An infinite for loop is the one which goes on repeatedly for infinite times. This can happen
in two cases:
expressions Example:for(;;)
printf("CodinGeek");
Example:for(i=1;i<=5;)
printf("CodinGeek");
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++)
{}
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.
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
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.
• 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.
• data_type array_name[array_size];
• int marks[5];
.2.1 How to declare a string?
char s[5];
String Declaration in C
String Initialization in C
example:
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.
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 :
#include<stdio.h>
Int main()
Int x=5;
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 :
· It is a unary operator.
variable.
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.
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
7.1 Structures
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.
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
}[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;
};
//initialization or,
p1.age = 23;
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();
}
Nesting of structures, is also permitted in C language. Nested structures means, that one
structure has another stucture as member variable.
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
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
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
Syntax:
void* malloc(byte-size)
int *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:
Copy
struct employee
char *name;
int salary;
};
emp *e1;
e1 = (emp*)calloc(30,sizeof(emp));
Syntax:
void* realloc(pointer, new-size)
Copy
int *x;
x = (int*)malloc(50 * sizeof(int));
#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
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.