OUTPUT

File Handling

Posted on

READING, FILE WRITE

FILE WRITE:

clip_image002

OUTPUT:

clip_image004

clip_image006

READING:

clip_image008

OUTPUT:

clip_image010

Posted By-: Vissicomp Technology Pvt. Ltd.

Website -: http://www.vissicomp.com

Advertisements

Union

Posted on Updated on

Union, Difference between structure & union

Unions are quite similar to the structures in C. Union is also a derived type as structure. Union can be defined in same manner as structures just the keyword used in defining union in union where keyword used in defining structure was struct.

union car{
  char name[50];
  int price;
};

Union variables can be created in similar manner as structure variable.

union car{
  char name[50];
  int price;
}c1, c2, *c3;
 
OR;
 
union car{
  char name[50];
  int price;
};
-------Inside Function-----------
union car c1, c2, *c3;

In both cases, union variables c1, c2 and union pointer variable c3 of type union car is created.

Accessing members of an union

The member of unions can be accessed in similar manner as that structure. Suppose, we you want to access price for union variable c1 in above example, it can be accessed as c1.price. If you want to access price for union pointer variable c3, it can be accessed as (*c3).price or as c3->price.

Difference between union and structure

Though unions are similar to structure in so many ways, the difference between them is crucial to understand. This can be demonstrated by this example:

#include <stdio.h>
union job {         //defining a union
   char name[32];
   float salary;
   int worker_no;
}u;
struct job1 {
   char name[32];
   float salary;
   int worker_no;
}s;
int main(){
   printf("size of union = %d",sizeof(u));
   printf("\nsize of structure = %d", sizeof(s));
   return 0;
}

Output

size of union = 32
size of structure = 40

There is difference in memory allocation between union and structure as suggested in above example. The amount of memory required to store a structure variables is the sum of memory size of all members.

clip_image001

But, the memory required to store a union variable is the memory required for largest element of an union.

clip_image002

What difference does it make between structure and union?

As you know, all members of structure can be accessed at any time. But, only one member of union can be accessed at a time in case of union and other members will contain garbage value.

#include <stdio.h>
union job {
   char name[32];
   float salary;
   int worker_no;
}u;
int main(){
   printf("Enter name:\n");
   scanf("%s",&u.name);
   printf("Enter salary: \n");
   scanf("%f",&u.salary);
   printf("Displaying\nName :%s\n",u.name);
   printf("Salary: %.1f",u.salary);
   return 0;
}

Output

Enter name 
Hillary
Enter salary
1234.23
Displaying
Name: f%Bary   
Salary: 1234.2

Note: You may get different garbage value of name.

Posted By-: Vissicomp Technology Pvt. Ltd.

Website -: http://www.vissicomp.com

Structure

Posted on Updated on

Structure, Structure and Pointer, Structure and Function, C Programming Unions

Structure

Structure is the collection of variables of different types under a single name for better handling. For example: You want to store the information about person about his/her name, citizenship number and salary. You can create these information separately but, better approach will be collection of these information under single name because all these information are related to person.

Structure Definition in C

Keyword struct is used for creating a structure.

Syntax of structure
struct structure_name 
{
    data_type member1;
    data_type member2;
    .
    .
    data_type memeber;
};

We can create the structure for a person as mentioned above as:

struct person
{
    char name[50];
    int cit_no;
    float salary;
};

This declaration above creates the derived data type struct person.

Structure variable declaration

When a structure is defined, it creates a user-defined type but, no storage is allocated. For the above structure of person, variable can be declared as:

struct person
{
    char name[50];
    int cit_no;
    float salary;
};
 
Inside main function:
struct person p1, p2, p[20];

Accessing members of a structure

There are two types of operators used for accessing members of a structure.

1. Member operator(.)

2. Structure pointer operator(->) (will be discussed in structure and pointers chapter)

Any member of a structure can be accessed as: structure_variable_name.member_name

Suppose, we want to access salary for variable p2. Then, it can be accessed as:

p2.salary

Example of structure

Write a C program to add two distances entered by user. Measurement of distance should be in inch and feet.(Note: 12 inches = 1 foot)

 
#include <stdio.h>
struct Distance{
    int feet;
    float inch;
}d1,d2,sum;
int main(){
    printf("1st distance\n");
    printf("Enter feet: ");
    scanf("%d",&d1.feet);  /* input of feet for structure variable d1 */
    printf("Enter inch: ");
    scanf("%f",&d1.inch);  /* input of inch for structure variable d1 */
    printf("2nd distance\n");
    printf("Enter feet: ");
    scanf("%d",&d2.feet);  /* input of feet for structure variable d2 */
    printf("Enter inch: ");
    scanf("%f",&d2.inch);  /* input of inch for structure variable d2 */
    sum.feet=d1.feet+d2.feet;
    sum.inch=d1.inch+d2.inch;
    if (sum.inch>12){  //If inch is greater than 12, changing it to feet.
        ++sum.feet;
        sum.inch=sum.inch-12;
    }
    printf("Sum of distances=%d\'-%.1f\"",sum.feet,sum.inch); 
/* printing sum of distance d1 and d2 */
    return 0;
}

Output

1st distance
Enter feet: 12
Enter inch: 7.9
2nd distance
Enter feet: 2
Enter inch: 9.8
Sum of distances= 15'-5.7"

Structure and Pointer

Pointers can be accessed along with structures. A pointer variable of structure can be created as below:

struct name {

member1;

member2;

.

.

};

——– Insidefunction ——-

struct name *ptr;

Here, the pointer variable of type struct name is created.

Structure’s member through pointer can be used in two ways:

1. Referencing pointer to another address to access memory

2. Using dynamic memory allocation

Consider an example to access structure’s member through pointer.

#include<stdio.h>
struct name{
int a;
float b;
};
int main(){
struct name *ptr,p;
ptr=&p;/* Referencing pointer to memory address of p */
printf("Enter integer: ");
scanf("%d",&(*ptr).a);
printf("Enter number: ");
scanf("%f",&(*ptr).b);
printf("Displaying: ");
printf("%d%f",(*ptr).a,(*ptr).b);
return0;
}

In this example, the pointer variable of type struct name is referenced to the address of p. Then, only the structure member through pointer can canaccessed.

Structure pointer member can also be accessed using -> operator.

(*ptr).a is same asptr->a
(*ptr).b is same asptr->b
Accessing structure member through pointer using dynamic memory allocation

To access structure member using pointers, memory can be allocated dynamically using malloc() function defined under “stdlib.h” header file.

Syntax to use malloc()
ptr=(cast-type*)malloc(byte-size)

Example to use structure’s member through pointer using malloc() function.

#include<stdio.h>
#include<stdlib.h>
struct name {
int a;
float b;
char c[30];
};
int main(){
struct name *ptr;
inti,n;
printf("Enter n: ");
scanf("%d",&n);
ptr=(struct name*)malloc(n*sizeof(struct name));
/* Above statement allocates the memory for n structures with pointer ptr pointing to base address */
for(i=0;i<n;++i){
printf("Enter string, integer and floating number  respectively:\n");
scanf("%s%d%f",&(ptr+i)->c,&(ptr+i)->a,&(ptr+i)->b);
}
printf("Displaying Infromation:\n");
for(i=0;i<n;++i)
printf("%s\t%d\t%.2f\n",(ptr+i)->c,(ptr+i)->a,(ptr+i)->b);
return0;
}

Output

Enter n:2
Enterstring, integer and floating number  respectively:
Programming
2
3.2
Enterstring, integer and floating number  respectively:
Structure
6
2.3
DisplayingInformation
Programming23.20
Structure62.30

Passing structure by value

A structure variable can be passed to the function as an argument as normal variable. If structure is passed by value, change made in structure variable in function definition does not reflect in original structure variable in calling function.

Write a C program to create a structure student, containing name and roll. Ask user the name and roll of a student in main function. Pass this structure to a function and display the information in that function.

#include <stdio.h>
struct student{
    char name[50];
    int roll;
};
void Display(struct student stu);
/* function prototype should be below to the structure declaration otherwise compiler shows error */
int main(){
    struct student s1;
    printf("Enter student's name: ");
    scanf("%s",&s1.name);
    printf("Enter roll number:");
    scanf("%d",&s1.roll);
    Display(s1);   // passing structure variable s1 as argument
    return 0;
}
void Display(struct student stu){
  printf("Output\nName: %s",stu.name);
  printf("\nRoll: %d",stu.roll);
}

Output

Enter student's name: Kevin Amla
Enter roll number: 149
Output
Name: Kevin Amla
Roll: 149
Passing structure by reference

The address location of structure variable is passed to function while passing it by reference. If structure is passed by reference, change made in structure variable in function definition reflects in original structure variable in the calling function.

Write a C program to add two distances(feet-inch system) entered by user. To solve this program, make a structure. Pass two structure variable (containing distance in feet and inch) to add function by reference and display the result in main function without returning it.

#include <stdio.h>
struct distance{
    int feet;
    float inch;
};
void Add(struct distance d1,struct distance d2, struct distance *d3); 
int main()
{
    struct distance dist1, dist2, dist3;
    printf("First distance\n");
    printf("Enter feet: ");
    scanf("%d",&dist1.feet);
    printf("Enter inch: ");
    scanf("%f",&dist1.inch);
    printf("Second distance\n");
    printf("Enter feet: ");
    scanf("%d",&dist2.feet);
    printf("Enter inch: ");
    scanf("%f",&dist2.inch);
    Add(dist1, dist2, &dist3); 
 
/*passing structure variables dist1 and dist2 by value whereas passing structure variable dist3 by reference */
    printf("\nSum of distances = %d\'-%.1f\"",dist3.feet, dist3.inch);
    return 0;
}
void Add(struct distance d1,struct distance d2, struct distance *d3) 
{
/* Adding distances d1 and d2 and storing it in d3 */
     d3->feet=d1.feet+d2.feet; 
     d3->inch=d1.inch+d2.inch;
     if (d3->inch>=12) {     /* if inch is greater or equal to 12, converting it to feet. */
         d3->inch-=12;
         ++d3->feet;
    }
}

Output

First distance
Enter feet: 12
Enter inch: 6.8
Second distance
Enter feet: 5
Enter inch: 7.5
 
Sum of distances = 18'-2.3"

Posted By-: Vissicomp Technology Pvt. Ltd.

Website -: http://www.vissicomp.com

String

Posted on Updated on

What is String & how to use it:

The string in C programming language is actually a one-dimensional array of characters which is terminated by a null character ”. 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’, ”};

If you follow the rule of array initialization then you can write the above statement as follows:

char greeting[] = “Hello”;

STRING COPY:

strcpy(s1, s2); Copies string s2 into string s1.

clip_image002

OUTPUT:

clip_image004

STRING CAT:

strcat(s1, s2); Concatenates string s2 onto the end of string s1.

clip_image006

OUTPUT:

clip_image008

STRING COMPARE:

Compares two string

clip_image010

OUTPUT:

clip_image012

STRING LENGTH:

strlen(s1); Returns the length of string s1.

clip_image014

OUTPUT:

clip_image016

STRING LOWER:

Converts string to lowercase

clip_image018

OUTPUT:

clip_image020

STRING UPPER:

Converts string to uppercase

OUTPUT:

clip_image024

Posted By-: Vissicomp Technology Pvt. Ltd.

Website -: http://www.vissicomp.com

Pointer, How to use pointer

Posted on

What Are Pointers?

A pointer is a variable whose value is the address of another variable, i.e., direct address of the memory location. Like any variable or constant, you must declare a pointer before you can use it to store any variable address. The general form of a pointer variable declaration is:

type *var-name;

type is the pointer’s base type; it must be a valid C data type and var-name is the name of the pointer variable. The asterisk * you used to declare a pointer is the same asterisk that you use for multiplication. However, in this statement the asterisk is being used to designate a variable as a pointer. Following are the valid pointer declaration:

int    *ip;    /* pointer to an integer */
double *dp;    /* pointer to a double */
float  *fp;    /* pointer to a float */
char   *ch     /* pointer to a character */

POINTER:

clip_image002

OUTPUT:

clip_image004

How to use Pointers?

There are few important operations, which we will do with the help of pointers very frequently. (a) we define a pointer variable (b) assign the address of a variable to a pointer and (c) finally access the value at the address available in the pointer variable. This is done by using unary operator * that returns the value of the variable located at the address specified by its operand. Following example makes use of these operations:

clip_image006

OUTPUT:

image

Posted By-: Vissicomp Technology Pvt. Ltd.

Website -: http://www.vissicomp.com