Paper DSC 203:PROGRAMMING WITH C & C++
UNIT-I: INTRODUCTION TO C LANGUAGE, VARIABLES, DATA TYPES AND OPERATORS INTRODUCTION: TYPES OF LANGUAGES- HISTORY OF C LANGUAGE – BASIC TRUCTURE –PROGRAMMING RULES – FLOW CHARTS-ALGORITHMS–COMMONLY USED LIBRARY FUNCTIONS - EXECUTING THE C PROGRAM - PRE-PROCESSORS IN “C”- KEYWORDS & IDENTIFIERS – CONSTANTS – VARIABLES: RULES FOR DEFINING VARIABLES - SCOPE AND LIFE OF A VARIABLE–- DATA TYPES - TYPE CONVERSION - FORMATTED INPUT AND OUTPUT OPERATIONS. OPERATORS: INTRODUCTION – ARITHMETIC – RELATIONAL – LOGICAL – ASSIGNMENT - CONDITIONAL - SPECIAL - BITWISE - INCREMENT / DECREMENT OPERATOR.
UNIT-II: WORKING WITH CONTROL STATEMENTS, LOOPS CONDITIONAL STATEMENTS: INTRODUCTION - IF STATEMENTS - IF-ELSE STATEMENTS – NESTED IF-ELSE – BREAKSTATEMENT-CONTINUE STATEMENT-GO TO STATEMENT-SWITCH STATEMENTS. LOOPING STATEMENTS: INTRODUCTION-WHILE STATEMENTS – DO-WHILE STATEMENTS - FOR STATEMENTS-NESTED LOOP STATEMENTS.
UNIT-IV: POINTERS, STRUCTURES AND UNIONS POINTERS: FEATURES OF POINTERS- DECLARATION OF POINTERS-ARITHMETIC OPERATIONS WITH POINTERS STRUCTURES: FEATURES OF STRUCTURES - DECLARING AND INITIALIZATION OF STRUCTURES –STRUCTURE WITHIN STRUCTURE- ARRAY OF STRUCTURES- ENUMERATED DATA TYPE-UNIONS-DEFINITION AND ADVANTAGES OF UNIONS COMPARISON BETWEEN STRUCTURE & UNIONS.
UNIT-V: OBJECT ORIENTED CONCEPTS USING C++ OBJECT ORIENTED PROGRAMMING: INTRODUCTION TO OBJECT ORIENTED PROGRAMMING - STRUCTURE OF C++ – SIMPLE PROGRAMOF C++– STORAGE CLASSES-SIMILARITIES AND DIFFERENCES BETWEEN C & C++ - DATA MEMBERS-MEMBER FUNCTIONS - OBJECT ORIENTED CONCEPTS-CLASS-OBJECT-INHERITANCE- OLYMORPHISM- ENCAPSULATION-ABSTRACTION.
UNIT-IV:
POINTERS,
STRUCTURES AND UNIONS
Pointers:
1.Define
Pointer ? Explain with example?
Ans: A pointer is
a special type of variable that can hold the memory address of another variable.
Pointer does
not hold a value but it holds the address of another variable. A Pointer can
access a variable indirectly by using the address of
the variable.
Address
of operator:
In C
&(ampersand)is known as an address of operator. It can be useful to know
the location of a variable.
For example:
int i = 3;
The address
of i can be printed by using the following statement:
printf(“\n Address of i = %u”, &i);
Indirection
Operator and Dereferencing
In C the
pointer operator ‘*’ is called the ‘value
at address’ operator. It returns the value stored at a particular address.
The
value at address operator is also called indirection operator or dereference operator.
Pointers features and uses:
§ Call
a function by address ( Call by Reference).
§ Return
more than one value from a Function indirectly.
§ Manipulate
arrays more easily.
§ Create
complex data structures, such as linked lists and binary trees.
§ Compile faster, more efficient code.
§ Pointers save
memory space.
§ Pointers are used
for file handling.
§ Pointers are used
to allocate memory dynamically.
§ Provides an
alternative way to access array elements.
Declaring
a Pointer:
The syntax
for declaring a pointer variable is
datatype * pointer_variable;
Example:
char *ptr;
Initializing
and Assigning Values to Pointers
A pointer
may be initialized to an address or NULL, or 0.
Example:
#include <stdio.h>
#include<conio.h>
int main()
{
int a=5, b=10;
int *p;
p = &a;
printf(“\n a=%d b=%d *p=%d”, a, b,*p);
getche();
return 0;
}
Output
a=5 b=10 *p=5
2.Explain Pointer Arithmetic
operations in C?
Ans:
Arithmetic operations with pointers
Following
arithmetic operations are possible on the pointer in C language:
o
Increment
o
Decrement
o
Addition
o Subtraction
Incrementing Pointer in C
If we increment a pointer by 1, the pointer will start pointing to the immediate next location. This is somewhat different from the general arithmetic since the value of the pointer will get increased by the size of the data type to which the pointer is pointing.
The Rule to increment the pointer is given below:
new_address= current_address + i * size_of(data type)
#include<stdio.h>
int main()
{
int num=50;
int *p; //pointer to int
p=&num //stores the address of number variable
printf("Address of p is %u \n",p);
p=p+1;
printf("After increment: Address of p is %u \n",p);
return 0;
}
Output Address of p is 3214864300
After increment: Address of p is 3214864304
Decrementing Pointer in C
Like
increment, we can decrement a pointer variable. If we decrement a pointer, it
will start pointing to the previous location. The formula of decrementing the
pointer is given below:
new_address= current_address i * size_of(data type)
#include <stdio.h>
void main()
{
int number=50;
int *p;
//pointer to int
p=&number;
//stores the address of number variable
printf("Address of p variable is %u \n",p);
p=p-1;
printf("After decrement: Address of p is %u \n",p); // P will now point to the immediate previous location.
}
Output Address of p is
3214864300
After decrement: Address of p is 3214864296
C Pointer Addition
We can add a
value to the pointer variable. The formula of adding value to pointer is given
below:
new_address= current_address + (number * size_of(data type))
#include<stdio.h>
int main(){
int number=50;
int *p; //pointer to int
p=&number;
//stores the address of number variable
printf("Address of p variable is %u \n",p);
p=p+3; //adding 3 to pointer variable
printf("After adding 3: Address of p variable is %u \n",p);
return 0;
}
Output
Address
of p variable is 3214864300
After adding 3: Address of p variable is 3214864312
C Pointer Subtraction
Like pointer
addition, we can subtract a value from the pointer variable. Subtracting any
number from a pointer will give an address. The formula of subtracting value
from the pointer variable is given below:
new_address= current_address (number * size_of(data type))
#include<stdio.h>
int main(){
int number=50;
int *p; //pointer to int
p=&number;
//stores the address of number variable
printf("Address of p variable is %u \n",p);
p=p-3; //subtracting 3 from pointer variable
printf("After subtracting 3: Address of p variable is %u \n",p);
return 0;
}
Output Address
of p variable is 3214864300
After subtracting 3: Address of p variable is 3214864288
Illegal
arithmetic with pointers
There are various operations which
can not be performed on pointers.
Address + Address = illegal
Address * Address = illegal
Address % Address = illegal
Address / Address = illegal
Address & Address = illegal
Address ^ Address = illegal
Address | Address = illegal
~Address = illegal
--O--
Structures
3.What is
Structure ? Explain with example?
Ans:
A structure is
a collection of variables under a single name. These variables can be of
different types A structure is declared by using the keyword struct.
Features of Structures
·
A structure can be defined as a user-defined data type
that is capable of holding heterogeneous data of basic data type.
·
Usually structure type declaration appears at the top
of the source code file, before any variables or functions are defined.
·
A structure is declared by using the keyword struct
followed by an optional structure tag followed by the body of the structure.
·
The closing brace in the structure type declaration
must be followed by a semicolon.
·
It is possible to create an array of structures.
·
One structure can be nested within another structure.
·
Like an ordinary variable, a structure variable can
also be passed to a function.
· We can have a pointer pointing to a struct. Such pointers are known as ‘structure pointers’.
The general format to declare a structure is:
struct <structure_tag_name >
{
<data_type member_name1>;
<data_type member_name2>;
. . . …. ….. ….;
}<structure_variable1>,<structure_variable2>
,...;
Example:
struct student
{
int sno;
char sname[10];
float marks;
} s1,s2;
Accessing the
Members of a Structure
The members of
a structure can be accessed by using the ‘.’ the ‘dot operator’. It has the
following form:
<
structure_variable >.< member_name > ;
Example:
S1.sno=101;
S1.sname=”Ritchie”;
S1.marks=625;
S2.sno=201;
S2.sname=”Bobbage”;
S2.marks=654;
Initialization of Structures
By default a
structure can also be initialized by the system. By default the integer and
float data type members are initialized with zero. Char and string type members
can be initialized with ‘\0’.
A structure can
be intialized by using the following format:
struct
<structure_tag_name>
{
<data_type
member_name1>;
<data_type
member_name2>;
}<structure_variable1>
= {constant1,constant2, .
.};
OR
struct
<structure_tag_name> <structure_variable> =
{constant1,constant2,..};
For example: The following program
illustrates the structures:
#include<stdio.h>
#include<conio.h>
struct student
{
int sno, s1,
s2, s3, total;
char sname[15];
float avg;
};
main()
{
struct student
student1;
clrscr();
printf("Enter
Student Number:");
scanf("%d",&student1.sno);
printf("Enter
Student Name:");
scanf("%s",&student1.sname);
printf("Enter
Marks in Subject1:");
scanf("%d",
&student1.s1);
printf("Enter
Marks in Subject2:");
scanf("%d",
&student1.s2);
printf("Enter
Marks in Subject3:");
scanf("%d",
&student1.s3);
student1.total=student1.s1+student1.s2+
student1.s3;
student1.avg =
student1.total/3;
printf("\n\n
Student Number = %d", student1.sno);
printf("\n
Student Name = %s", student1.sname);
printf("\n
Marks in Subject1 =%d", student1.s1);
printf("\n
Marks in Subject2 =%d", student1.s2);
printf("\n
Marks in Subject3 =%d", student1.s3);
printf("\n
Total Marks = %d", student1.total);
printf("\n
Average Marks = %f", student1.avg);
getche();
return 0;
}
--O--
Arrays of
Structures
4.Describe the
Concept of Arrays of Structures?
Ans: Declaring a structure variable as an array of objects
is known as Array of Structures. Each element in an array of structures
contains the members of the structure.
An array of
structures can be created by using the following format:
struct
<structure_tag_name >
{
<data_type
member_name1>;
<data_type
member_name2>;
. . .
}<structure_variable>[index];
struct
<structure_tag_name>
<structure_variable>[index];
This will declare an array of structures |
struct student
{
int sno, s1,
s2, s3, total;
char sname[15];
float avg;
}student1[3];
Write a C program to illustrate an
array structures
#include
<stdio.h>
struct student
{
int sno, s1,
s2, s3, total;
char sname[15];
float avg;
}student1[3];
int main()
{
int i;
clrscr();
printf("Enter
10 Students information:\n");
for(i=0;i<10;i++)
{
printf("Enter
Student Number: ");
scanf("%d",
&student1[i].sno);
printf("Enter
Student Name: ");
scanf("%s",
student1[i].sname);
printf("Enter
Marks in Subjct1: ");
scanf("%d",
&student1[i].s1);
printf("Enter
Marks in Subjct2: ");
scanf("%d",
&student1[i].s2);
printf("Enter
Marks in Subjct3: ");
scanf("%d",
&student1[i].s3);
student1[i].total
= student1[i].s1+ student1[i].s2+ student1[i].s3;
student1[i].avg
= student1[i].total/3;
}
printf("Student
Information:\n");
printf("\n
sno \t name \t s1 \t s2 \t s3 \t total \t avg :\n");
for(i=0;i<10;i++)
{
printf("\n%d
\t %s \t %d \t %d \t %d \t %d \t%f \n", student1[i].sno,
student1[i].sname, student1[i].s1, student1[i].s2, student1[i].s3,
student1[i].total,student1[i].avg);
}
getche();
return 0;
}
--O--
Structure
within Structure
A structure within a structure means
nesting of structures. In such cases, the dot operator in conjunction with the structure
variables are used to access the members of the innermost as well as the
outermost structures.
#include
<stdio.h>
struct outer /*
declaration of outer structure */
{
int out1; /*
member of outer structure */
float out2; /* member of outer
structure */
struct inner /*
declaration of inner structure */
{
int in1; /* member of inner structure */
fl oat in2; /*
member of inner structure */
}invar; /* structure_variable
of inner structure*/
};
int main()
{
struct outer
outvar; /*
declaring structure_variable of outer */
outvar.out1= 2;
/*
assigning values to members */
outvar.out2=
10.57; /* assigning values to members */
outvar.invar.in1=
2* outvar.out1;
outvar.invar.in2=
outvar.out2 + 3.65;
printf(“ out1=%d, out2=%6.2f, in1=%d, in2=%6.2f”, outvar.out1, outvar.out2,outvar.invar.in1,outvar.invar.in2);
return 0;
}
Output: out1=2,
out2= 10.57, in1=4, in2= 14.22
Enumerated
data type
4.Explain
Enumerated data type with example?
Ans:
Enumeration data types are the user
defined data type. The members of a enum can represent a set of
integer constants. The keyword enum can be used to declare an
enumeration type.
Syntax to declare a Enum:
enum tag_name
{
member1,
member2,
…,
memberN
}variable1,...,variableX;
By default, the
member1 is given the value 0. Member2 is given the value 1 and so on.
Example: enum
days {Mon, Tues, Wed, Thurs, Fri, Sat,Sun};
Here, the
values ‘Mon,...,Sun’ are assigned to a
variable of type enum days.
#include <stdio.h>
enum days{Mon, Tues, Wed, Thurs, Fri, Sat, Sun };
int main()
{
enum days start, end;
start= Tues; /* means start=1 */
end= Sat; /* means end=5 */
printf(“\n start = %d, end = %d”, start,end);
getche();
return 0;
}
Output start = 1, end = 5
--O---
Unions
5.Explain the
Concept of union in c language?
Ans:
A union is a a
collection of variables under a single name. The members of a union can
share the same storage. The amount of storage allocated to a union is sufficient
to hold its largest member.
A Union can
maintain only one member at a time in that storage. A union can be declared by
using the keyword "union".
Declaring a
Union and its Members: The general
format for declaring a union is :
union tag_name
{
data type member1;
data typemember2;
.
data type memberN;
}variable1,variable2,variable3,…,variableX;
Example:
union emp
{
int eno;
char ename[10];
float salary;
} e1;
Accessing and
Initializing the Members of a Union
The members of a union can be accessed by using the ‘.’ the ‘dot operator’. It has the following form:
< union_variable
>.< member_name > ;
Example: e1.eno=101;
e1.ename=”Ritchie”;
e1.salary=25000;
Write a program
that illustrates the unions.
union emp
{
ine eno;
char ename[10];
float salary;
}e1;
main()
{
clrcsr();
printf("Enter
Employee Number:");
scanf("%d",&e1.eno);
printf("\n\t
Employee Number = %d", e1.eno);
printf("Enter
Employee Name:");
scanf("%s",e1.ename);
printf("\n\t
Employee Name = %s", e1.ename);
printf("Enter
Employee Salary:");
scanf("%f",e1.salary);
printf("\n\t
Employee Salary = %f", e1.salary);
getche();
return 0;
}
--O—
Structure versus Union
6.Explain
Difference
between Structure and union
Ans:
Memory
allocation: The amount of memory required for a structure is equal
to the sum of the size of all the members. For a union, the amount of the
memory required is same as that of the largest member.
#include
<stdio.h>
#include
<stdlib.h>
#include
<string.h>
struct S
{
int i;
char ch;
float d;
};
union U
{
int i;
char ch;
double d;
};
int main()
{
printf(“\n Size
of the structure is %d”, sizeof(struct S));
printf(“\n Size
of the union is %d”, sizeof(union U));
return 0;
}
Output Size of the
structure is 7
Size of the union is 4
Member access: A structure
member can be accessed at any point of time. But in Unions, we can access only
one member at any given time. Because at any moment of time, only one union
member will have a meaningful value.
The other
members have garbage values.
Example:
#include
<stdio.h>
#include
<stdlib.h>
#include
<string.h>
struct S
{
int i;
char ch;
float d;
};
union U
{
int i;
char ch;
float d;
};
int main()
{
struct S
a={10,‘A’,3.1415};
union U b={10};
printf(“\n
a.i=%d a.ch=%c a.d=%lf”, a.i,a.ch,a.d);
printf(“\n
b.i=%d b.ch=%c b.d=%lf”, b.i,b.ch,b.d);
b.ch=‘B’;
printf(“\n
b.i=%d b.ch=%c b.d=%lf”, b.i,b.ch,b.d);
b.d=5.12345;
printf(“\n
b.i=%d b.ch=%c b.d=%lf”, b.i,b.ch,b.d);
return 0;
}
Output
a.i=10 a.ch=A a.d=3.141500
b.i=10 b.ch= b.d=0.000000
b.i=66 b.ch=B b.d=0.000000
b.i=-1388133430 b.ch= b.d=5.123450
No comments:
Post a Comment