Tuesday, December 23, 2014

String of a C programs



12. Program
#include 
#include 
#include 
union Data
{
   int i;
   float f;
   char  str[20];
};
 
int main( )
{
 Clrscr();
   union Data data;        
 
   printf( "Memory size occupied by data : %d\n", sizeof(data));
 
   return 0;
   getch();
}

Output
          Memory size occupied by data: 20
 
13. Program
#include 
#include 
#include 
union Data
{
   int i;
   float f;
   char  str[20];
};
 
int main( )
{
   union Data data;        
 
   data.i = 10;
   data.f = 220.5;
   strcpy( data.str, "C Programming");
 
   printf( "data.i : %d\n", data.i);
   printf( "data.f : %f\n", data.f);
   printf( "data.str : %s\n", data.str);
 
   return 0;
getch();
}

Output
data.i : 1917853763
data.f : 4122360580327794860452759994368.000000
data.str : C Programming

14. Program
#include 
#include 
#include 
union Data
{
   int i;
   float f;
   char  str[20];
};
 
int main( )
{
 clrscr();
   union Data data;        
 
   data.i = 10;
   printf( "data.i : %d\n", data.i);
   
   data.f = 220.5;
   printf( "data.f : %f\n", data.f);
   
   strcpy( data.str, "C Programming");
   printf( "data.str : %s\n", data.str);
 
   return 0;
   getch();
}

Output
data.i : 10
data.f : 220.500000
data.str : C Programming


15. Program
#include 
#include 
#include 
struct
{
  unsigned int widthValidated;
  unsigned int heightValidated;
} status1;
 
struct
{
  unsigned int widthValidated : 1;
  unsigned int heightValidated : 1;
} status2;
 
int main( )
            {
            clrscr();
            printf( "Memory size occupied by status1 : %d\n", sizeof(status1)); 
            printf( "Memory size occupied by status2 : %d\n", sizeof(status2));
 
   return 0;
getch();
}

Output
Memory size occupied by status1 : 8
Memory size occupied by status2 : 4

16. Program
#include 
#include 
#include 
 
struct
{
  unsigned int age : 3;
} Age;
 
int main( )
{
   Age.age = 4;
   printf( "Sizeof( Age ) : %d\n", sizeof(Age) );
   printf( "Age.age : %d\n", Age.age );
 
   Age.age = 7;
   printf( "Age.age : %d\n", Age.age );
 
   Age.age = 8;
   printf( "Age.age : %d\n", Age.age );
 
   return 0;
   getch();
}
 
Output
        
        Sizeof( Age ) : 4
            Age.age : 4
            Age.age : 7
            Age.age : 0


No comments:

Post a Comment