TODAY JAVA SCHOOL

In java school, programming, design, computer general knowledge, web application, software, web services, social media, digital marketing, oops, concept of programming language, oops feature, console media, graphics medium, first programming, c, c ++ , Java, PHP, SQL, MySQL, HTML, HTML_5, J_query, JavaScript, Bootstrap, Framework, images with logos, examples, shared and explained.

https://www.amazon.in/b?node=26373545031&linkCode=ll2&tag=1234567801cdb-21&linkId=3b9882431b00409b44141e0344b35a15&language=en_IN&ref_=as_li_ss_tl

Breaking

Showing posts with label C. Show all posts
Showing posts with label C. Show all posts

Friday, 15 May 2020

May 15, 2020

Union In C Language | Union In C In Hindi | Union In C Programming | Union In C Example

Union In C


 Union in c language is a user-defined data type that is used to store the different type of elements.

 Only one member of the union can occupy the memory. In other words, we can say that the size of the union in any instance is equal to the size of its largest element.

image of union in c language

Fig: Union In C

Advantage of union over structure


It occupies less memory because it occupies the size of the largest member only.


Disadvantage of union over structure


Only the last entered data can be stored in the union. It overwrites the data previously stored in the union.


Defining union


The union keyword is used to define the unionunion union_name .    

      

      Syntax:
{  

    data_type member1;  

    data_type member2;  

    .  

    .  

    data_type memeberN;  

};  

 

Example:

 

union employee  

{   int id;  

    char name[50];  

    float salary;  

};  

 

Program


#include <stdio.h>  

#include <string.h>  

union employee    

{   int id;    

    char name[50];    

}e1

int main( )  

{  

   

   e1.id=101;  

   strcpy(e1.name, "Pramod Dwivedi");

  

 printf( "employee 1 id : %d\n", e1.id);  

 

   printf( "employee 1 name : %s\n", e1.name);  

 

   return 0;  

 

}  


Output:


employee 1 id : 101

employee 1 name : Pramod Dwivedi

May 15, 2020

Nested Structure In C | Nested Structure In C In Hindi | Nested Structure In C Programming Example

Nested Structure in C


C provides us the feature of nesting one structure within another structure by using which, complex data types are created.

image of nested structure in c
Fig: Nested structure in c

Program

#include<stdio.h>  

struct address   

{  

    char city[20];  

    int pin;  

    char phone[14];  

};  

struct employee  

{  

    char name[20];  

    struct address add;  

};  

void main ()  

{  

    struct employee emp;  

 

    printf("Enter employee information?\n");  

 

    scanf("%s %s %d %s",emp.name,emp.add.city, &emp.add.pin, emp.add.phone);  

 

    printf("Printing the employee information....\n");  

 

    printf("name: %s\nCity: %s\nPincode: %d\nPhone: %s",emp.name,emp.add.city,emp.add.pin,emp.add.phone);  

 

}  

 

Output

Enter employee information?

Pramod       

Delhi          

201301 

9540225110

Printing the employee information.... 

name: Pramod

City: Delhi 

Pincode: 201301

Phone: 9450225110

 

The structure can be nested in the following ways.

1.    By separate structure

2.    By Embedded structure

1) Separate structure

We create two structures, but the dependent structure should be used inside the main structure as a member.

 

Example

     struct Date  

{       

      int dd;  

      int mm;  

       int yyyy;    

         };  

     struct Employee  

{            

       int id;  

      char name[20];  

      struct Date doj;  

      }emp1;  

 

2) Embedded structure


The embedded structure enables us to declare the structure inside the structure. Hence, it requires less line of codes but it can not be used in multiple data structures.


Example:

     struct Employee  

{     

    int id;  

    char name[20];  

     struct Date  

    {  

      int dd;  

      int mm;  

      int yyyy;   

    }doj;  

}   emp1;  


Accessing Nested Structure


We can access the member of the nested structure by Outer_Structure.

    e1.doj.mm  

     e1.doj.yyyy  

 

Program 

     

    #include <stdio.h>  

    #include <string.h>  

     struct Employee  

{     

     int id;  

   char name[20];  

   struct Date  

    {  

      int dd;  

      int mm;  

      int yyyy;   

    }doj;  

    }e1;    

    int main( )  

                   

      {            

     e1.id=101;  

     strcpy(e1.name, "Pramod Dwivedi");

    e1.doj.dd=08;  

    e1.doj.mm=07;  

    e1.doj.yyyy=2000;  

  

    printf( "employee id : %d\n", e1.id);  

    printf( "employee name : %s\n", e1.name);  

            printf( "employee date of joining (dd/mm/yyyy) : %d/%d/%d\n", e1.doj.d d   ,e1.doj.mm,e1.doj.yyyy);  

     return 0;  

            }             


Output:


employee id : 101

employee name : Pramod Dwivedi

employee date of joining (dd/mm/yyyy) : 08/07/2000

 


May 15, 2020

C Array Of Structure | C Array Of Structure In Hindi | C Array Of Structure Example | C Programming Array Structure

C Array of Structure


Why use an array of structures? 


An array of structres in C can be defined as the collection of multiple structures variables where each variable contains information about different entities. The array of structure in c are used to store information about multiple entities of different data types. The array of structures is also known as the collection of structures.


image of array structure in c

Fig: Array structure in c


Program of array of structure

 

#include<stdio.h>  

struct student  

{  

    char name[20];  

    int id;  

    float marks;  

};  

void main()  

{  

    struct student s1,s2,s3;  

    int dummy;  

    printf("Enter the name, id, and marks of student 1 ");  

    scanf("%s %d %f",s1.name,&s1.id,&s1.marks);  

    scanf("%c",&dummy);  

    printf("Enter the name, id, and marks of student 2 ");  

    scanf("%s %d %f",s2.name,&s2.id,&s2.marks);  

    scanf("%c",&dummy);  

    printf("Enter the name, id, and marks of student 3 ");  

    scanf("%s %d %f",s3.name,&s3.id,&s3.marks);  

    scanf("%c",&dummy);  

    printf("Printing the details....\n");  

    printf("%s %d %f\n",s1.name,s1.id,s1.marks);  

    printf("%s %d %f\n",s2.name,s2.id,s2.marks);  

    printf("%s %d %f\n",s3.name,s3.id,s3.marks);  

 

 

Output


Enter the name, id, and marks of student 1 Pramod 90 90 

Enter the name, id, and marks of student 2 Amit90 90 

Enter the name, id, and marks of student 3 Ankit 90 90      

Printing the details....       

Pramod 90 90.000000                         

Amit  90 90.000000                     

Ankit  90 90.000000