Monday 11 March 2013

Nesting of structure


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 can not access the data member of inner structure.
Example:

void main()
{
struct world
{
int a;
char b;
struct india
{
char c;
float d;
}p;
};
struct world st ={1,'A','i',1.8};
clrscr();
printf("%d\t%c\t%c\t%f",st.a,st.b,st.p.c,st.p.d);
getch();
}
Output: 1 A i 1.800000


Blog Author: Vijay Kumar

Go to: Java Aptitude

Structure data member access


void main()
{
struct employee
{
unsigned id: 8;
unsigned sex:1;
unsigned age:7;
};
struct employee emp1={203,1,23};
clrscr();
printf("%d\t%d\t%d",emp1.id,emp1.sex,emp1.age);
getch();
}

Output: 203 1 23
We can access the data member in same way, how bit data is stored in the memory:

Minimum size of structure which data member in bit 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 in from right to left direction. 8 bit for id, 1 bit for sex and 7 bit for age.


Blog Author: Vijay Kumar

Go to: Java Aptitude

Corresponding Value of Signed Unsigned


    #include<stdio.h>
    int main()
{
    int a=-5;
    unsigned int b=-5u;
    if(a==b)
         printf("Avatar");
    else
         printf("Alien");
    return 0;
}
Output: Avatar
Explanation: int a=-5;
Here variable a is by default signed int.
unsigned int b=-5u;
Constant -5u will convert into unsigned int. Its corresponding unsigned int value will be :
65536 – 5 + 1= 65532
So, b = 65532

In any binary operation of dissimilar data type for example: a == b
Lower data type operand always automatically type casted into the operand of higher data type before performing the operation and result will be higher data type.
In c signed int is higher data type than unsigned int. So variable ‘b’ will automatically type casted into signed int.
So corresponding signed value of 65532 is -5
Hence, a==b


Blog Author: Vijay Kumar

Go to: Java Aptitude

Slove (a+ !a + ~a + ++a)


    #include<stdio.h>
    int main()
{
    char a=250;
    int expr;
    expr= a+ !a + ~a + ++a;
    printf("%d",expr);
    return 0;
}
Output: -6
Explanation: char a = 250;
250 is beyond the range of signed char. Its corresponding cyclic value is: -6 (-128 to 127), 0 to 127 is occupied in first 1 to 250, then it remains 122. Now this 122 is occupied by -128 to -7 and remains -6.

So, a = -6

Consider on the expression:
expr= a+ !a + ~a + ++a;
Operator! , ~ and ++ have equal precedence. And it associative is right to left.
So, First ++ operator will perform the operation. So value a will -5
Now,
Expr = -5 + !-5 + ~-5 + -5
= -5 + !-5 + 4 – 5       

//(to solve ~-5 first make binary of 5 then subtract 1 and convert back to decimal.)

= -5 + 0 + 4 -5
= -6


Blog Author: Vijay Kumar

Go to: Java Aptitude

enum Example


#include<stdio.h>
const enum Alpha{
      X,
      Y=5,
      Z=Y+1,
}p=10;
int main(){
    enum Alpha a,b;
    a= X;
    b= Z;
    printf("%d",a+b-p); 
    return 0; 
}
Output: -4
Explanation: Default value of enum constant X is zero and
Z = Y + 1 = 5 + 1 = 6
So, a + b – p
=0 + 6 -10 = -4


Blog Author: Vijay Kumar

Go to: Java Aptitude

sizeof(signed) and sizeof(unsigned)


    #include<stdio.h>
    int main()
{
    int a= sizeof(signed) +sizeof(unsigned);
    int b=sizeof(const)+sizeof(volatile);
    printf("%d",a+++b);
    return 0;
}
Output: 8
Explanation: Default data type of signed, unsigned, const and volatile is int. So, a = 4 and b =4
Now, a+++b
= a++ + b
= 4 + 4  //due to post increment operator.
=8


Blog Author: Vijay Kumar

Go to: Java Aptitude

Signed Unsigned Logic


    #include<stdio.h>
    int main()
{
    signed x;
    unsigned y;
    x = 10 +- 10u + 10u +- 10;
    y = x;
    if(x==y)
         printf("%d %d",x,y);
    else if(x!=y)
         printf("%u  %u",x,y);
    return 0;
}
Output: 0  0
Explanation: x = 10 +- 10u + 10u +- 10;
10: It is signed integer constant.
10u: It is unsigned integer constant.
X: It is signed integer variable.
Lower data type operand always automatically type casted into the operand of higher data type before performing the operation and result will be higher data type. Signed is higher data type than unsigned int. So our expression is:
x = 10 + (-10u) + 10u + (-10);
  = 10 + -10 + 10 + (-10);
  = 0


Blog Author: Vijay Kumar

Go to: Java Aptitude

Ever tried this (i++ + ++i)


void main()
{
int i=5;
printf("%d",i++ + ++i);
}


output: 12


Explanationall functions pass the value from right to left therefore i++ + ++i in this sequence first of all solve the ++i and then solve the i++ first increment by one i.e. 6 and then it solve i++ i.e. 5
printf("%d",i++ + ++i);
printf("%d",i++ + 6); // here i++ is 6 because i is assigned 6.
printf("%d",6 + 6); // after this operation again i will be incremented and becomes 7.
printf("%d",12);

add an extra line printf("%d",i); to check the value of i. it is 7.
if it is ("%d", ++i + ++i);
then output will be 13.


Blog Author: Vijay Kumar

Go to: Java Aptitude

Default type of short, static, unsigned, const


Consider on following declaration:
(i)        short i=10;
(ii)      static i=10;
(iii)    unsigned i=10;
(iv)      const i=10;
Choose correct one:

Explanation: All are correct. Default data type of above all declaration is int.


Blog Author: Vijay Kumar

Go to: Java Aptitude

More about sizeof()


More about sizeof()

    #include<stdio.h>
    int main()
{
    double num=5.2;
    int  var=5;
    printf("%d\t",sizeof(!num));
    printf("%d\t",sizeof(var=15/2));
    printf("%d",var);
    return 0;
}

Output: 2       2          5

Explanation: sizeof(Expr)  operator always returns the an integer value which represents the size of the final value. Consider on the following expression: !num
=!5.2
=0
0 is int type integer constant and it size is 2
Consider on the following expression:
var = 15/2
=> var = 7
=> 7
7 is int type integer constant.
Any expression which is evaluated inside the sizeof operator its scope always will be within the sizeof operator. So value of variable var will remain 5 in the printf statement.


Blog Author: Vijay Kumar

Go to: Java Aptitude

sizeof() Explannation


sizeof() Explannation 

Sizeof prints out 6 for:
printf("%d\n", sizeof("abcde"));
But it prints out 4 for:
char* str = "abcde";
printf("%d\n", sizeof(str));
why?

Explanation:
The string literal "abcde" is a character array. It is 6 bytes long, including the null terminator.
A variable of type char* is a pointer to a character. Its size is the size of a pointer, which on 32-bit systems is 4 bytes.
Because here
printf("%d\n", sizeof("abcde"));
is a string, with considering NULL its 6 byte long.
and
char* str = "abcde";
printf("%d\n", sizeof(str));
is a pointer that requires 32bits hence 4 bytes :-)


Blog Author: Vijay Kumar

Go to: Java Aptitude

sizeof()


    #include<stdio.h>
    int main()
{
    printf("%d\t",sizeof(6.5));
    printf("%d\t",sizeof(90000));
    printf("%d",sizeof('A'));
    return 0;
}
Output: 8         4          1

Explanation: 

6.5 is of double type by default, to make it float, it must be written as 6.5f. 
90000 is of long type by default.
'A' is a char type. 


Blog Author: Vijay Kumar

Go to: Java Aptitude