CS3353-UNIT II (1)
CS3353-UNIT II (1)
C PROGRAMMING -
ADVANCED FEATURES
C 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.
For example, an entity Student may have its name
(string), roll number (int), marks (float).
✔Construct individual arrays for storing names, roll
numbers, and marks.
✔Use a special data structure to store the collection of
different data types.
Defining a Structure
* The ,struct keyword is used to define the
structure.
struct structure_name
{
data_type member1;
data_type member2;
…..
data_type memeberN;
};
In structure, data is stored in form of
records.
Example:
struct employee
{ int id;
char name[20];
float salary;
};
struct is the keyword; employee is the name of the
structure; id, name, and salary are the members or fields
of the structure.
Declaring structure
*variable
We can declare a variable for the structure so that we can access the member
of the structure easily. There are two ways to declare structure variable:
1. By struct keyword within main() function
2. By declaring a variable at the time of defining the structure.
* 1st way:
To declare the structure variable by struct keyword. It should be declared
within the main function.
struct employee struct Student
{ int id; {
char name[50]; char name[25];
float salary; int age;
}; char branch[10];
//F for female and M for male
char gender;
};
struct Student S1, S2;
* 2nd way:
To declare variable at the time of defining the
structure.
struct employee struct Student
{ int id; {
char name[50]; char name[25];
float salary; int age;
}e1,e2; char branch[10];
//F for female and M for
male
char gender;
}S1, S2;
If number of variables are not fixed, use the 1st
approach. It provides you the flexibility to declare the
structure variable many times.
If no. of variables are fixed, use 2nd approach. It
saves your code to declare a variable in main() function.
Accessing Structure
Members
* To access any member of a structure, we use the
member access operator (.)
* You would use the keyword struct to define
variables of structure type.
/* book 2 specification */
Example strcpy( Book2.title, "Telecom Billing");
#include <stdio.h> strcpy( Book2.author, "Zara Ali");
#include <string.h> strcpy( Book2.subject, "Telecom Billing
struct Books { char title[50]; Tutorial");
char author[50]; Book2.book_id = 6495700;
char subject[100]; /* print Book1 info */
int book_id; }; printf( "Book 1 title : %s\n", Book1.title);
int main( ) printf( "Book 1 author : %s\n",
Book1.author);
{
printf( "Book 1 subject : %s\n",
struct Books Book1; Book1.subject);
/* Declare Book1 of type Book */ printf( "Book 1 book_id : %d\n",
struct Books Book2; Book1.book_id);
/* Declare Book2 of type Book */ /* print Book2 info */
/* book 1 specification */ printf( "Book 2 title : %s\n", Book2.title);
strcpy( Book1.title, "C printf( "Book 2 author : %s\n",
Programming"); Book2.author);
strcpy( Book1.author, "Nuha Ali"); printf( "Book 2 subject : %s\n",
Book2.subject);
strcpy( Book1.subject, "C
printf( "Book 2 book_id : %d\n",
Programming Tutorial"); Book2.book_id);
Book1.book_id = 6495407; return 0;
Structure Initialization
* A variable of any other datatype, structure variable can also
be initialized at compile time.
struct Patient
{
float height;
int weight;
int age;
};
struct Patient p1 = { 180.75 , 73, 23 }; //initialization
Or
struct Patient p1;
p1.height = 180.75; //initialization of each member separately
p1.weight = 73;
p1.age = 23;
Nested Structures
* If a structures contains one or more structures as its members, it is
known as a nested structure.
Struct date
{
Int day;
Char month[10];
Int year;
};
This structure date may be used as a member in another structure
as given bellow:
Struct employee
{
Char name[30];
Int age; char sex;
Struct date dob; /*inner structure variable definition */
};
ARRAY OF STRUCTURES
* A group of structures may be organized in an array
resulting in an array of structures. Each element in the
array is a structure . C allows declaring an array of
structures.
Struct employee
{
Char name[25];
Int age;
Char sex;
Struct date dob;
};
Struct employee emp[5];
Defines an array of 5 structures. Each structure variable
emp[0],emp[1],…emp[4] contains structure as its value
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.
DEFINING A UNION:
✔To define a union, you must use the union keyword
✔The union statement defines a new data type with more than one
member for your program. union [union tag]
{
member definition;
member definition;
...
member definition;
} [one or more union variables];
#include <stdio.h>
#include <string.h>
union Data
{
int i;
float f;
char str[20];
};
int main( )
{
union Data data;
printf( "Memory size occupied by data : %d\n", sizeof(data));
return 0;
}
Memory size occupied by data: 20
Accessing Union Members
*(*nums) nums[0][0] 16
*(*nums + 1) nums[0][1] 18
*(*nums + 2) nums[0][2] 20
*(*(nums + 1) + 1) nums[1][1] 26
*(*(nums + 1) + 2) nums[1][2] 27
POINTER TO FUNCTION
* The general syntax for a pointer to a function
return_type(*pointer_name)(list of parameter)
For example,
void(*p)(float int);
This declaration of a pointer to a function returns void and
takes the two formal arguments of a float and int types
after declaring a pointer to a function the address of the
function must be assigned to a pointer aa follows
p=&function_name;
P-pointer variable
Through pointer to function is called as follows:
a=(*p)(x,y);
#include <stdio.h>
int add(int,int); return 0;
int main() }
{ int add(int a,int b)
int a,b; {
int (*ip)(int,int); int c=a+b;
int result; return c;
printf("Enter the values of a and b : "); }
OUTPUT:
scanf("%d %d",&a,&b); Enter the values of a and
ip=add; b : 4 55
result=(*ip)(a,b); Value after addition is :
59
printf("Value after addition is : %d",res
ult);
FILE HANDLING
STREAMS IN C
* C can handle files as Stream-oriented data (Text)
files and System oriented data (Binary) files.
#define MAX_ARRAY_LENGTH 20
This directive tells the CPP to replace instances of
MAX_ARRAY_LENGTH with 20. Use #define for constants to
increase readability.
#include <stdio.h>
#include "myheader.h"
stdio.h from System Libraries and add the text to the current
source file.
The next line tells CPP to get myheader.h from the local
directory and add the content to the current source file.
#undef FILE_SIZE
#define FILE_SIZE 42
The CPP to undefine existing FILE_SIZE and define it as 42.
Macros in C Language
* The macro in C language is known as the piece of code
which can be replaced by the macro value
* The macro is defined with the help of #define
preprocessor directive and the macro doesn’t end with
a semicolon(;)
* Macro is just a name given to certain values or
expressions it doesn't point to any memory location.
* The syntax of the macro is