Wednesday, January 1, 2014

struct bit fields questions in c

struct bit fields questions in c


struct bit fields questions answers and explanation in c programming language.
(1) What will be output of c code?
#include<stdio.h>
int main() {
    struct employee {
         unsigned id: 8;
         unsigned sex:1;
         unsigned age:7;
    }; struct employee emp1={203,1,23};
    printf("%d\t%d\t%d",emp1.id,emp1.sex,emp1.age);
   
    return 0;
}
Output: 203 1 23
Explanation:
We can access the data member of bit field's structure in same way as normal structure.
How bit data or fields in a structure are stored in the memory:
Minimum size of structure which has at least one bit type member is two byte i.e. 16 bit. This is called word size of microprocessor. Word size depends on microprocessor. Turbo c is based on 8086 microprocessor which word size is two byte.


Bits are filled from right to left direction. In the above question 8 bit is used for id, 1 bit for sex and 7 bit for age. 
(2) What will be output of c code?
#include<stdio.h>
int main() {
    struct bitfield {
         unsigned a:5;
         unsigned c:5;
         unsigned b:6;
    }bit;
    char *p;
    struct bitfield *ptr,bit1={1,3,3};
    p=&bit1;
    p++;
   
    printf("%d",*p);
    return 0;
}
Output: 12
Explanation:
Binary value of 1 is 00001 (in 5 bit)
Binary value of 3 is 00011 (in 5 bit)
Binary value of 3 is 000011 (in 6 bit)
In memory it can be represented as:

Let's assume memory address of bit1 is 500 which have assigned to char pointer p. Since char is one byte data type so p++ will be 501. *p means content of memory address 501 which is (00001100) and its binary equivalent value is 12. Hence output is 12. 
(3) What will be output of c code?
#include<stdio.h>
int main() {
    struct bitfield {
         signed int a:3;
         unsigned int b:13;
         unsigned int c:1;
    }; struct bitfield bit1={2,14,1};
   
    printf("%d",sizeof(bit1));
    return 0;
}
Output: 4
(4) What will be output of c code?
#include<stdio.h>
int main() {
    struct bitfield {
         unsigned a:3;
         char b;
         unsigned c:5;
         int d;
    }bit;
   
    printf("%d",sizeof(bit));
    return 0;
}
Output: 5
Note: (Actual output will be 6 due to slack byte ,So Before executing this program first go to option menu then compiler then code generation then select word alignment then press OK in trubo c 3.0) 
(5) What will be output of c code?
#include<stdio.h>
int main() {
    struct field {
         int a;
         char b;
    }bit; struct field bit1={5,'A'};
    char *p=&bit1;
    *p=45;
   
    printf("\n%d",bit1.a);
    return 0;
}
Output: 45
Nesting of structure:
Nesting of structure is possible i.e. we can declare a structure within another structure but it is necessary inner structure must declares structure variable otherwise we cannot access the data member of inner structure. For example: 
(6) What will be output of c code?
#include<stdio.h>
int main() {
    struct world {
         int a;
         char b;
         struct india { char c; float d; }p;
    }; struct world st ={1,'A','i',1.8};
   
    printf("%d\t%c\t%c\t%f",st.a,st.b,st.p.c,st.p.d);
    return 0;
}
Output: 1 A I 1.800000
Array, union, structure can be member of a structure (not same structure). 
(7) What will be output of c code?
#include<stdio.h>
int main() {
    struct india {
         char c;
         float d;
    };
    struct world {
         int a[3];
         char b;
         struct india orissa;
    };
    struct world st ={{1,2,3},'P','q',1.4};
    printf("%d\t%c\t%c\t%f",st.a[1],st.b,st.orissa.c,st.orissa.d);
    return 0;
}
Output: 2 p q 1.400000

Prime number or not using c program

Check given number is prime number or not using c program

Definition of prime number:

A natural number greater than one has not any other divisors except 1 and itself. In other word we can say which has only two divisors 1 and number itself. For example: 5

Their divisors are 1 and 5.



Note: 2 is only even prime number.



Logic for prime number in c

We will take a loop and divide number from 2 to number/2. If the number is not divisible by any of the numbers then we will print it as prime number.
Example of prime numbers : 2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97, 101, 103, 107, 109, 113, 127, 131, 137, 139, 149, 151, 157, 163, 167, 173, 179, 181, 191, 193, 197, 199 etc.
Code 1:
1. C program to determine prime number
2. Determining if a number is prime in c
3. C program to find given number is prime or not
#include<stdio.h>
int main(){
    int num,i,count=0;
    printf("Enter a number: ");
    scanf("%d",&num);
    for(i=2;i<=num/2;i++){
        if(num%i==0){
         count++;
            break;
        }
    }
   if(count==0 && num!= 1)
        printf("%d is a prime number",num);
   else
      printf("%d is not a prime number",num);
   return 0;
}
Sample output:
Enter a number: 5
5 is a prime number
Code 2:
1. C program for prime numbers between 1 to 100
2. How to find prime numbers from 1 to 100 in c
3. How to print prime numbers from 1 to 100 in c
#include<stdio.h>
int main(){
    int num,i,count;
  
    for(num = 1;num<=100;num++){
         count = 0;
         for(i=2;i<=num/2;i++){
             if(num%i==0){
                 count++;
                 break;
             }
        }
        
         if(count==0 && num!= 1)
             printf("%d ",num);
    }
  
   return 0;
}
Output:
2 3 5 7 11 13 17 19 23 29 31 37 41 43 47 53 59 61 67 71 73 79 83 89 97
Code 3:
1. C program for prime numbers between 1 to n
2. C program to find prime numbers up to n
3. C program to list prime numbers
4. Write a c program to generate n prime numbers
5. C program to find n prime numbers
#include<stdio.h>
int main(){
    int num,i,count,n;
    printf("Enter max range: ");
    scanf("%d",&n);
    for(num = 1;num<=n;num++){
         count = 0;
         for(i=2;i<=num/2;i++){
             if(num%i==0){
                 count++;
                 break;
             }
        }
        
         if(count==0 && num!= 1)
             printf("%d ",num);
    }
  
   return 0;
}
Sample output:
Enter max range: 50
2 3 5 7 11 13 17 19 23 29 31 37 41 43 47
  
Code 4:
1. C program to find prime numbers using while loop
2. Wap to find prime numbers in c
3. Write a c program to generate prime number
4. How to get prime numbers in c
#include<stdio.h>
int main(){
   int num,i,count,min,max;
printf("Enter min range: ");
    scanf("%d",&min);
    printf("Enter max range: ");
    scanf("%d",&max);
    num = min;
    while(num<=max){
         count = 0;
         i=2;
         while(i<=num/2){
             if(num%i==0){
                 count++;
                 break;
             }
             i++;
        }
        
         if(count==0 && num!= 1)
             printf("%d ",num);
         num++;
    }
  
   return 0;
}
Sample output:
Enter min range: 50
Enter max range: 100
53 59 61 67 71 73 79 83 89 97
Code 5:
1. How to find out prime numbers in c programming
2. Display prime numbers in c
3. C program to find prime numbers between two numbers
4. C code to display prime numbers within a range
#include<stdio.h>
int main(){
    int num,i,count,min,max;
     printf("Enter min range: ");
     scanf("%d",&min);
    printf("Enter max range: ");
    scanf("%d",&max);
    for(num = min;num<=max;num++){
         count = 0;
         for(i=2;i<=num/2;i++){
             if(num%i==0){
                 count++;
                 break;
             }
        }
        
         if(count==0 && num!= 1)
             printf("%d ",num);
    }
  
   return 0;
}
Sample output:
Enter min range: 10
Enter max range: 50
11 13 17 19 23 29 31 37 41 43 47
Code 6:
1. Sum of prime numbers from 1 to 100 in c
#include<stdio.h>
int main(){
    int num,i,count,sum=0;
    for(num = 1;num<=100;num++){
         count = 0;
         for(i=2;i<=num/2;i++){
             if(num%i==0){
                 count++;
                 break;
             }
        }
        
         if(count==0 && num!= 1)
             sum = sum + num;
    }
    printf("Sum of prime numbers is: %d ",sum);
  
   return 0;
}
  
Output:
Sum of prime numbers is: 1060
Code 7:
1. C program to find sum of prime numbers
#include<stdio.h>
int main(){
    int num,i,count,min,max,sum=0;
     printf("Enter min range: ");
     scanf("%d",&min);
    printf("Enter max range: ");
    scanf("%d",&max);
    for(num = min;num<=max;num++){
         count = 0;
         for(i=2;i<=num/2;i++){
             if(num%i==0){
                 count++;
                 break;
             }
        }
        
         if(count==0 && num!= 1)
             sum = sum + num;
    }
    printf("Sum of prime numbers is: %d ",sum);
  
   return 0;
}
Sample output:
Enter min range: 50
Enter max range: 100

1. Write a c program to check given number is perfect number or not.
Sum of prime numbers is: 732