Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

Report Mayank

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 26

INDUSTRIAL TRAINING REPORT

on

“C Programming”
Done at
“ABES Engineering college”
by
Mayank Kumar (1603231103)

Submitted to
Department of Electronics & Communication Engineering
in partial fulfilment of the requirements for the Degree of
Bachelor of Technology
in
Electronics & Communication Engineering

ABES Engineering College, Ghaziabad


Dr. A.P.J. Abdul Kalam Technical University, Lucknow
August, 2019

ACKNOWLEDGEMENT

It has indeed been a great privilege for me to have team MFE,


ABES Engineering College Ghaziabad , to give such an
opportunity. They bring and conduct NEC Technologies drive for campus
recruitment for training of students. Their c o n s t a n t guidance and
encouragement are the motive force behind this training work. I take
this opportunity to express my utmost gratitude to them. I am also
indebted to them for their timely and valuable advice.
I am highly grateful to Prof. ( D r . ) S . K . S i n g h , ( H O D - E C E ) ,
ABES Engineering college, Ghaziabad for providing necessary facilities
and encouraging me during the course of training.
I am t h an kfu l to all technical and non-teaching staff of the
Department of Electronics & Communication Engineering for their
constant assistance and co-operation.

MAYANK KUMAR
Roll No. 1603231103
B. Tech (VII SEM)
Department of Electronics &
Communication Engineering
ABES Engineering College, Ghaziabad

Contents
• Introduction
• C Data Type
• Operators
• Loop
• Function
• Array
• String
• Structure
• Type Casting
• Recursion
• Preprocessor
• Programming Question

1.Introduction

• When you learn to program in a high-level language like C


(although C is fairly low level, as high-level languages go),
the idea is to avoid worrying too much about the
hardware. You want the ability to represent mathematical
abstractions, such as sets, etc. and have high level
language features like threads, higher-order functions,
exceptions.

• High-level languages, for the most part, try to make you


as unaware of the hardware as possible. Clearly, this isn't
entirely true, because efficiency is still a major
consideration for some programming languages.

• C is a procedural programming language. It was initially


developed by Dennis Ritchie between 1969 and 1973. It
was mainly developed as a system programming language
to write an operating system. The main features of C
language include low-level access to memory, a simple set
of keywords, and clean style, these features make C
language suitable for system programming like an
operating system or compiler development.
Many later languages have borrowed syntax/features
directly or indirectly from C language. Like syntax of Java,
PHP, JavaScript and many other languages are mainly
based on C language. C++ is nearly a superset of C
language (There are few programs that may compile in C,
but not in C++).
• C Data Type:

Data types in c refer to an extensive system 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
Types & Description:
• Basic Types: They are arithmetic types and are further
classified into: (a) integer types and (b)floating-point types
• 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
• Void Type: The type specifier void indicates that no value
is available.
• Derived types: They include (a) Pointer types, (b) Array
types, (c) Structure types, (d) Union types and (e) Function
types.
Integer Types
Type Storage size Value range
char 1 byte -128 to 127 or 0 to 255
unsigned char 1 byte 0 to 255
signed char 1 byte -128 to 127
-32,768 to 32,767 or -2,147,483,648 to
int 2 or 4 bytes
2,147,483,647
unsigned int 2 or 4 bytes 0 to 65,535 or 0 to 4,294,967,295
short 2 bytes -32,768 to 32,767
unsigned short 2 bytes 0 to 65,535
-9223372036854775808 to
long 8 bytes
9223372036854775807
unsigned long 8 bytes 0 to 18446744073709551615

Floating-Point Types

Type Storage size Value range Precision


6 decimal
float 4 byte 1.2E-38 to 3.4E+38
places
15
double 8 byte 2.3E-308 to 1.7E+308 decimal
places
19
long double 10 byte 3.4E-4932 to 1.1E+4932 decimal
places
• Operators:

An operator is a symbol that tells the compiler to perform specific mathematical or


logical functions. C language is rich in built-in operators and provides the
following types of operators −
• Arithmetic Operators
• Relational Operators
• Logical Operators
• Bitwise Operators
• Assignment Operators
• Misc Operator

Operators Precedence in C
Operator precedence determines the grouping of terms in an expression and decides how an
expression is evaluated. Certain operators have higher precedence than others; for example, the
multiplication operator has a higher precedence than the addition operator.
Category Operator Associativity
Postfix () [] -> . ++ - - Left to right
Unary + - ! ~ ++ - - (type)* & sizeof Right to left
Multiplicative */% Left to right
Additive +- Left to right
Shift << >> Left to right
Relational < <= > >= Left to right
Equality == != Left to right
Bitwise AND & Left to right
Bitwise XOR ^ Left to right
Bitwise OR | Left to right
Logical AND && Left to right
Logical OR || Left to right
Conditional ?: Right to left
= += -= *= /= %=>>= <<= &= ^=
Assignment Right to left
|=
Comma , Left to right

• Loop:
A loop statement allows us to execute a statement or group of statements
multiple times.
C programming language provides the following types of loops to handle looping
requirements.
Loop Type & Description:
• while loop: Repeats a statement or group of statements while a given
condition is true. It tests the condition before executing the loop body.
• for loop: Executes a sequence of statements multiple times and abbreviates
the code that manages the loop variable.
• dowhile loop: It is more like a while statement, except that it tests the
condition at the end of the loop body.
• nested loops: You can use one or more loops inside any other while, for,
or do..while loop.
Loop Control Statements:
• break statement: Terminates the loop or switch statement and transfers
execution to the statement immediately following the loop or switch.
• Continue statement: Causes the loop to skip the remainder of its body
and immediately retest its condition prior to reiterating
• Goto statement: Transfers control to the labeled statement

5. Function:
A function is a group of statements that together perform a task. Every C program
has at least one function, which is main(), and all the most trivial programs can
define additional functions.
You can divide up your code into separate functions. How you divide up your code
among different functions is up to you, but logically the division is such that each
function performs a specific task.
A function declaration tells the compiler about a function's name, return type, and
parameters. A function definition provides the actual body of the function.
The C standard library provides numerous built-in functions that your program can
call. For example, strcat() to concatenate two strings, memcpy() to copy one
memory location to another location, and many more functions.
Defining a Function
The general form of a function definition in C programming language is as follows

return_type function_name( parameter list ) {
body of the function
}
A function definition in C programming consists of a function header and a
function body. Here are all the parts of a function −
• Return Type − A function may return a value. The return_type is the
data type of the value the function returns. Some functions perform the
desired operations without returning a value. In this case, the return_type is
the keyword void.
• Function Name − This is the actual name of the function. The function
name and the parameter list together constitute the function signature.
• Parameters − A parameter is like a placeholder. When a function is
invoked, you pass a value to the parameter. This value is referred to as actual
parameter or argument. The parameter list refers to the type, order, and
number of the parameters of a function. Parameters are optional; that is, a
function may contain no parameters.
• Function Body − The function body contains a collection of statements
that define what the function does.
Function Declarations
A function declaration tells the compiler about a function name and how to call
the function. The actual body of the function can be defined separately.
Calling a Function
While creating a C function, you give a definition of what the function has to do.
To use a function, you will have to call that function to perform the defined task.
6. 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
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 −
type arrayName [ arraySize ];

Accessing Array Elements


An element is accessed by indexing the array name. This is done by placing the
index of the element within square brackets after the name of the array. For
example −
double salary = balance[9];
7. Strings
Strings are actually one-dimensional array of characters terminated by a null
character '\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."
char greeting[6] = {'H', 'e', 'l', 'l', 'o', '\0'};
If you follow the rule of array initialization then you can write the above statement
as follows −
char greeting[] = "Hello";
Following is the memory presentation of the above defined string in C/C++ −

String functions:

• strcpy(s1, s2): Copies string s2 into string s1.


• strcat(s1, s2): Concatenates string s2 onto the end of string s1.
• strlen(s1): Returns the length of string s1.
• strcmp(s1, s2): Returns 0 if s1 and s2 are the same; less than 0 if s1<s2;
greater than 0 if s1>s2
• strchr(s1, ch): Returns a pointer to the first occurrence of character ch in
string s1
• strstr(s1, s2): Returns a pointer to the first occurrence of string s2 in
string s1.
8.Structure:
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. Suppose you want to keep track of your
books in a library. You might want to track the following attributes about each
book −
• Title
• Author
• Subject
• Book ID
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 −
struct [structure tag] {

member definition;
member definition;
...
member definition;
} [one or more structure variables];
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. Here is the way you would declare the
Book structure −
struct Books {
char title[50];
char author[50];
char subject[100];
int book_id;
} book;
9. Type Casting:
ype casting is a way to convert a variable from one data type to another data type.
For example, if you want to store a 'long' value into a simple integer then you can
type cast 'long' to 'int'. You can convert the values from one type to another
explicitly using the cast operator as follows −
(type_name) expression

Example:
Float to int: float x=3.14;
Int a = (int)x;

Usual Arithmetic Conversion


The usual arithmetic conversions are implicitly performed to cast their values to
a common type. The compiler first performs integer promotion; if the operands
still have different types, then they are converted to the type that appears highest in
the following hierarchy –
10. Recursion: Recursion is the process of repeating items in a self-
similar way. In programming languages, if a program allows you to call a
function inside the same function, then it is called a recursive call of the
function.
The C programming language supports recursion, i.e., a function to call itself. But
while using recursion, programmers need to be careful to define an exit condition
from the function, otherwise it will go into an infinite loop.
Recursive functions are very useful to solve many mathematical problems, such as
calculating the factorial of a number, generating Fibonacci series, etc
Example:
#include <stdio.h>

int fibonacci(int i) {

if(i == 0) {
return 0;
}

if(i == 1) {
return 1;
}
return fibonacci(i-1) + fibonacci(i-2);
}

int main() {

int i;

for (i = 0; i < 10; i++) {


printf("%d\t\n", fibonacci(i));
}

return 0;
}
11. Preprocessor: The C Preprocessor is not a part of the compiler, but
is a separate step in the compilation process. In simple terms, a C Preprocessor is
just a text substitution tool and it instructs the compiler to do required pre-
processing before the actual compilation.

The following section lists down all the important preprocessor directives –
• #define: Substitutes a preprocessor macro.
• #include: Inserts a particular header from another file.
• #undef: Undefines a preprocessor macro.
• #if: Tests if a compile time condition is true.
• #else: The alternative for #if
Predefined Macros
ANSI C defines a number of macros. Although each one is available for use in
programming, the predefined macros should not be directly modified.
• __DATE__: The current date as a character literal in "MMM DD YYYY"

format.

• __TIME__: The current time as a character literal in "HH:MM:SS" format.

• __FILE__: This contains the current filename as a string literal.

• __LINE__: This contains the current line number as a decimal constant.

12.Programming Question

Online Shopping Program Using C

#include<stdio.h>
#include<string.h>
main()
{
static int totalCost;
int i,j,choice,c=1,a[9],cost[9];
for(i=0;i<9;i++)
a[i]=0;

char str[100];
char items[9][100]={"Sandisk 16 GB",
"Logitech Mouse",
"Pendrve 16 GB",
"Adidas",
"Nike",
"Leecooper",
"Mi Note 3",
"Nokia 3",
"Samsung"
};
printf("Please Enter Your Name\n");
gets(str);
printf("Hello %s, Welcome to our Online Shopping.\n",str);
do{
//C is 1 by default
if(c==1){
printf("Enter\n1 - Computer Accessories\n2 - Shoes\n3 -
Mobiles\nAny other number to exit\n");
scanf("%d",&choice);
switch(choice)
{
case 1:
{
int accessoriesChoice;
printf("Enter\n1 - Sandisk 16 GB - Rs.355\n2 - Logitech
Mouse- Rs.500\n3 - Pendrive 16 GB - Rs.550\nAny other number
to exit\n");
scanf("%d",&accessoriesChoice);
cost[0]=355;
cost[1]=500;
cost[2]=550;
switch(accessoriesChoice)
{
case 1:
{
int num;
printf("You chose Sandisk 16GB with Rs.355.Are you sure to
buy.If 'Yes' Enter 1 else any number\n");
scanf("%d",&num);
if(num==1)
{
a[0]++;
totalCost+=355;
}
printf("Your Cost in Cart is %d\n",totalCost);
break;
}
case 2:
{
int num;
printf("You chose Logitech Mouse with Rs.500.Are you sure to
buy.If 'Yes' Enter 1 else any number\n");
scanf("%d",&num);
if(num==1)
{
a[1]++;
totalCost+=500;
}
printf("Your Cost in Cart is %d\n",totalCost);
break;
}
case 3:
{
int num;
printf("You chose Pendrive 16GB with Rs.550.Are you sure to
buy.If 'Yes' Enter 1 else any number\n");
scanf("%d",&num);
if(num==1)
{
a[2]++;
totalCost+=550;
}
printf("Your Cost in Cart is %d\n",totalCost);
break;
}
default:{
printf("Exit from Computer Accesories\n");
break;
}
}
break;
}
case 2:
{
int shoesChoice;
printf("Enter\n1 - Adidas - Rs.3550\n2 - Nike - Rs.5000\n3 -
Leecooper - Rs.2800\nAny other number to exit\n");
scanf("%d",&shoesChoice);
cost[3]=3550;
cost[4]=5000;
cost[5]=2800;
switch(shoesChoice)
{
case 1:
{
int num;
printf("You chose Adidas Shoes for Rs.3550.Are you sure to
buy.If 'Yes' Enter 1 else any number\n");
scanf("%d",&num);
if(num==1)
{
a[3]++;
totalCost+=3550;
}
printf("Your Cost in Cart is %d\n",totalCost);
break;
}
case 2:
{
int num;
printf("You chose Nike Shoes for Rs.5000.Are you sure to
buy.If 'Yes' Enter 1 else any number\n");
scanf("%d",&num);
if(num==1)
{
a[4]++;
totalCost+=5000;
}
printf("Your Cost in Cart is %d\n",totalCost);
break;
}
case 3:
{
int num;
printf("You chose Leecooper Shoes for Rs.2800.Are you sure
to buy.If 'Yes' Enter 1 else any number\n");
scanf("%d",&num);
if(num==1)
{
a[5]++;
totalCost+=2800;
}
printf("Your Cost in Cart is %d\n",totalCost);
break;
}
default:{
printf("Exit from Shoes Category\n");
break;
}
}
break;
}
case 3:
{
int mobileChoice;
printf("Enter\n1 - Mi Note 3 - Rs.11000\n2 - Nokia 3 -
Rs.9866\n3 - Samsung - Rs.12800\nAny other number to
exit\n");
scanf("%d",&mobileChoice);
cost[6]=11000;
cost[7]=9866;
cost[8]=12800;
switch(mobileChoice)
{
case 1:
{
int num;
printf("You chose to buy Mi Note 3 for Rs.11000.Are you sure
to buy.If 'Yes' Enter 1 else any number\n");
scanf("%d",&num);
if(num==1)
{
a[6]++;
totalCost+=11000;
}
printf("Your Cost in Cart is %d\n",totalCost);
break;
}
case 2:
{
int num;
printf("You chose to buy Nokia 3 for Rs.9866.Are you sure to
buy.If 'Yes' Enter 1 else any number\n");
scanf("%d",&num);
if(num==1)
{
a[7]++;
totalCost+=9866;
}
printf("Your Cost in Cart is %d\n",totalCost);
break;
}
case 3:
{
int num;
printf("You chose to buy Samsung for Rs.12800.Are you sure
to buy.If 'Yes' Enter 1 else any number\n");
scanf("%d",&num);
if(num==1)
{
a[8]++;
totalCost+=12800;
}
printf("Your Cost in Cart is %d\n",totalCost);
break;
}
default:{
printf("Exit from Mobile Category\n");
break;
}
}
break;
}
default:
{
printf("Enter Valid Categories Choice\n");
break;
}
}
printf("%s's cart\n",str);
printf("Id\tItems\t\t\tQuantity\t\t\tCost\n");
for(i=0;i<9;i++)
{
if(a[i]!=0)
{
printf("%d\t%s\t\t%d\t\t\t%d\n",i,items[i],a[i],(cost[i]*a[i]));
}
}
printf("Total Cost\t\t\t\t\t%d\n",totalCost);
printf("If you wish to buy anything more Enter\n1 to Add
Item\n2 to Delete Items \nAny other number to Exit\n");
scanf("%d",&c);
}
if(c==2)
{
int id;
printf("Enter id to delete item\n");
scanf("%d",&id);
if(id<9&&id>0){
totalCost=totalCost-(cost[id]*a[id]);
a[id]=0;
}
else{
printf("Enter Valid id\n");
}
printf("Revised Items \n");
printf("Id\tItems\t\t\tQuantity\t\tCost\n");
for(i=0;i<9;i++)
{
if(a[i]!=0)
{
printf("%d\t%s\t\t%d\t\t%d\n",i,items[i],a[i],(cost[i]*a[i]));
}
}
printf("Total Cost\t\t\t\t\t%d\n",totalCost);
printf("If you wish to buy anything more Enter\n1 to Add
Item\n2 to Delete Items \nAny other number to Exit\n");
scanf("%d",&c);
}

}while(c==1 || c==2);
printf("Your Final Cost is %d\n",totalCost);
printf("Thanks %s for Choosing Us and Visit us again.\n",str);

You might also like