Computer Concept & Programming In C - Unit II - 12

Q.14 Write short notes on mixed operands and type conversions.
Realted Questions -
Discuss Type conversion in C.                                           (AKTU. 2012 - 13)    
Ans. Mixed Operands And Type Conversions: -
       When you mix different types together in one expression, the compiler automatically carries out the proper conversion of types. For example, when adding a float to an integer, the result will be of the float type. In fact, when the compiler meets different types it raises all of them to the same type (temporarily) before evaluating the expression. So if one expression contains char, int, float and double then all types are converted to double before any manipulation of the operands.
The declared types are not changed for always, they regain their original type (the declared type) right after the operation. Examples of mixed operands and type conversion. The following program adds two values.
One stored in int a and other in float b the result is stored in variable float c .
# include <stdio.h>
# include <conio.h>
void main ( )
{
int a = 300;
float b = 50.25;
float c;
   c = a + b;
   printf (“\n %f,” c);
getch ( );
}
The output of this program is 350.250000
Now look at this example you declare C as intC and not floatC. The fraction part will be truncated.
# include <stdio.h>
# include <conio.h>
void main ( )
{
int a = 300;
float b = 50.25;
int c;
c = a + b;
printf (“\n%d”, c);
getch ( );
}
The output of the program is 350.

Q.15  Write a program in 'C' to receive an integer and find its octal  equivalent */                                                                           (AKTU. 2008 - 09)
Ans.
 /* Remember if a decimal number is to be printed in the
    decimal, octal, hexadecimal we use the format specifiers
    %d / %ld, %o / %lo, %x / % lx respectively.             */
#include<stdio.h>
#include<conio.h>
void main()
{
 long integer;
 clrscr();
 printf("\nEnter the integer in base 10 (DECIMAL INTEGER)\n");
 scanf("%ld", &integer);
 printf("\n The equivalent integer in the base 8 (OCTAL INTEGER)");
 printf("is  %lo",integer);
 printf("\nPress any key to exit.");
 getch();
}

Q.16. Write the output of the following code with explanation:
main ( )
{
char*str1 = “abcd”;
char str2[] = “abcd”;
printf(“%d%d%d”,sizeof(str1),sizeof(str2),sizeof(“abcd”));
}                                                                                                 (AKTU. 2011 - 12)
Ans. The output is 555 because abcd are 4 characters which take 4 byte of memory + 1 byte for back slash zero \0 or null character which terminates the string. size of ( ) operator returns size  occupied in bytes so (4 + 1) = 5 byte.