Bpops103 Mod5azdocuments - in
Bpops103 Mod5azdocuments - in
struct employee
{
int emp_no;
char name[20];
int age;
float emp_sal;
};
1
Initialization of structure variables :-
struct employee
{
int emp_no;
char name[20];
int age;
float emp_sal;
} emp1={65421, “Hari”,29,25000.00};
The order of values enclosed in the braces must match the order of members in the structure
definition.
/* C program to read and print the details of employee using structures –static allocation
of variables */
#include<stdio.h>
#include<string.h>
struct employee
{
int emp_no;
char empname[20];
int age;
float emp_sal;
};
void main()
{
struct employee emp1;
emp1.emp_no = 65421;
strcpy(emp1.empname,"Hari");
emp1.age=29;
emp1.emp_sal=25000.00;
printf("Employee Number=%d\n",emp1.emp_no);
printf("Employee Name=%s\n",emp1.empname);
printf("Employee Age=%d\n",emp1.age);
printf("Employee Salary=%f\n",emp1.emp_sal);
}
/* C program to read and print the details of employee using structures –run time allocation
of variables */
#include<stdio.h>
#include<string.h>
struct employee
{
int emp_no;
char empname[20];
int age;
float emp_sal;
};
2
void main()
{
struct employee emp1;
printf("Enter Employee
Number:");
scanf("%d",&emp1.emp_no);
printf("Enter Employee Name:");
scanf("%s",emp1.empname);
printf("Enter Employee age:");
scanf("%d",&emp1.age);
printf("Enter Employee salary:");
scanf("%f",&emp1.emp_sal);
printf("Employee Number=%d\n",emp1.emp_no);
printf("Employee Name=%s\n",emp1.empname);
printf("Employee Age=%d\n",emp1.age);
printf("Employee Salary=%f\n",emp1.emp_sal);
}
#include<stdio.h>
struct complex
{
int real;
int img;
};
void main()
{
struct complex a,b,c;
printf("Enter the First Complex Number\n");
printf("Enter the real part:");
scanf("%d",&a.real);
printf("Enter the imaginary part:");
scanf("%d",&a.img);
printf("Enter the Second Complex Number\n");
printf("Enter the real part:");
scanf("%d",&b.real);
printf("Enter the imaginary part:");
scanf("%d",&b.img);
c.real=a.real+b.real;
c.img=a.img+b.img;
if(c.img>=0)
printf("Sum of two Complex Number = %d+i%d\n",c.real,c.img);
else
printf("Sum of two Complex Number = %d %di\n",c.real,c.img);
}
3
Array of Structures :- if we want to store the data of 100 employees we would require 100
structure variables from emp1 to emp100 which is definitely impractical the better approach
would be to use the array of structures.
4
Nested Structures :- A nested structure is a structure that contains another structure as its
member.
Syntax :-
struct structure_name1
{
datatype member 1;
datatype member 2;
};
struct structure_name2
{
datatype member 1;
datatype member 2;
struct structure_name1 var1;
};
struct student
{
int rollno;
char sname[20];
struct stud_dob date;
};
void main()
{
struct student s;
printf("Enter Student Rollno :");
scanf("%d",&s.rollno);
printf("Enter Student Name :");
scanf("%s",s.sname);
printf("Enter date of birth as day month year:"); scanf("%d%d
%d",&s.date.day,&s.date.month,&s.date.year); printf("Student Details
are\n");
printf("Student Rollno = %d\n",s.rollno);
printf("Student Name=%s",s.sname);
printf("Student DOB= %d-%d-%d\n", s.date.day, s.date.month, s.date.year);
}
5
Structures and Functions :- Structures can be passed to functions and returned from it.
Passing structures to functions can be done in the following ways
– Passing individual members
– Passing entire structure or structure variable.
Passing Entire structure or structure variable :-while invoking the function instead of
passing the individual members the entire structure, i.e. the structure variable is passed as a
parameter to the function.
#include<stdio.h>
struct addition
{
int a;
int b;
};
void add(struct addition sum);
void main()
{
struct addition sum;
printf(" Enter two numbers\n");
scanf("%d%d",&sum.a,&sum.b);
add(sum);
}
6
void add(struct addition sum)
{
int res;
res=sum.a+sum.b;
printf("Resultant=%d\n",res);
}
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.
Syntax for declaring a union is same as that of declaring a structure except the keyword struct.
union union_name
{ datatype
field_name; datatype
field_name;
// more variables
};
Difference between Structure and Union
Structure Union
The Keyword struct is used to define The keyword union is used to
Keyword
the Structure define a union.
when a variable is associated witha
When a variable is associated with a
union, the compiler allocates the
structure, the compiler allocates the
memory by considering the size of
Size memory for each member. The size
the largest memory. So, size of
of structure is greater than or equal to
union is equal to the size of largest
the sum of sizes of its members.
member.
Each member within a structure is
Memory allocated is shared by
Memory assigned unique storage area of
location. individual members of union.
Altering the value of a member will Altering the value of any of the
Value
not affect other members of the member will alter other member
Altering
structure. values.
Accessing Individual member can be accessed at Only one member can be accessed
members a time. at a time.
Initialization Several members of a structure can Only the first member of a union
of Members initialize at once. can be initialized.
7
Pointers
Pointer: - A pointer is a variable which holds address of another variable or a memory-location.
Advantages of Pointers:
Pointers are more efficient in handling arrays and data tables.
Pointers can be used to return multiple values from a function.
Pointers allow ‘C’ to support dynamic memory management.
Pointers provide when efficient tool for manipulating dynamic data structures such
as stack, queue etc.
Pointers reduce length and complexity of programs.
They increase execution speed and this reduces program execution time
Declaration and Initialization of pointers :- The operators used to represent pointers are
– Address Operator (&)
– Indirection Operator (*)
Syntax :-
ptr_data_type *ptr_var_name;
ptr_var_name = &var_name;
– where var_name is a variable whose address is to be stored in the pointer.
Example :-
int a=10;
int *ptr;
then
ptr = &a;
*ptr = a;
Here ptr is a pointer holding the address of variable ‘a’
and *ptr holds the value of the variable a.
#include<stdio.h>
void main()
{
int *ptr;
int x=22;
printf("Addres of x= %d\n",&x);
printf("Content of x= %d\n",x);
ptr=&x;
printf("Addres of pointer= %d\n",ptr);
printf("Content of pointer= %d\n",*ptr);
8
}
Double Pointer: When a pointer holds the address of another pointer then such type of pointer
is known as pointer-to-pointer or double pointer.
• Here the first pointer is used to store the address of the variable
• The second pointer is used to store the address of the first pointer.
the declaration is similar to that of the pointer the only difference is we place an additional
*before the name of the pointer.
9
#include<stdio.h>
void main()
{
int
var=777;
int *ptr2;
int ** ptr1;
ptr2=&var;
ptr1=&ptr2;
printf(“value of var=%d\n”,var);
printf(“value of var using single pointer=%d\n”,*ptr2);
printf(“value of var using double pointer=%d\n”,**ptr1);
}
OUTPUT
value of var=777
value of var using single pointer=777
value of var using double pointer=777
Preprocessor Directives
Preprocessor Directives:-Preprocessor is a program which is invoked by the compiler before
the compilation of the user written program
–The declaration of preprocessor statements always begin with # symbol
–These statements are usually placed before the main( ) function.
Types of Preprocessors:-
–Macros
–File Inclusion
–Conditional compilation
–Other directives
Macros:-These are the piece of code in the program which is given some name.
• Whenever the name is encountered by the compiler in the program , the compiler replaces
the name with the actual piece of code.
• The “#define” directive is used to define a macro.
10
# define pi 3.142
void main()
{
float r,area;
printf("Enter the value of radius\n");
scanf("%f",&r);
area=pi*r*r;
printf("Area of circle = %f\n",area);
}
/*C Program that finds the addition of two squared numbers, by defining macro for
Square(x).*/
#include <stdio.h>
#define square(x) (x*x)
void main( )
{
int n1, n2, sum;
printf(“Enter two
numbers”); scanf(“%d%d”,
&n1, &n2);
sum = square(n1) + square(n2);
printf(“Sum of two squared numbers = %d”, sum);
}
File Inclusion:-This type of preprocessor directive tells the compiler to include a file in the
source code program
These files contains the definition of the predefined functions like printf( ), scanf( ), etc.
Different functions are declared in different header files.
11
Conditional Compilation:-these are the type of directives that helps to compile a specific
portion of the program or to skip a specific part of the program based on some conditions.
Some of the conditional compilation directive are
– #ifdef, #ifndef, #if, #else
Other Directives:-Apart from the above directive there are two more directives which are not
commonly used
#undef directive:-this directive is used to undefined a value which was declare using the
#define directive
12