Report Mayank
Report Mayank
Report Mayank
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
ACKNOWLEDGEMENT
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
Floating-Point Types
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 ];
String functions:
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;
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;
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.
12.Programming Question
#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);