C Programming : Type casting in C introduction, examples and interview questions

devkumar082001@gmail.com
C Programming : Type casting in C introduction, examples

Type Casting In C

  • Type casting in C means conversion of  a variable from one type to another type.
  • The type casting in C can be done by using the "type" keyword before the variable.

 
Syntax:
(type_name)expression;

It is recommended to cast the small data type to bigger data type for preventing the data loss.



Example of Type casting :

Conversion of smaller data type to longer data type and longer data type to smaller data type to understand the data loss concept.

 Type casting in C  examples

 Type casting in C introduction, examples  2

 In the example 2 we can see the actual data is 1.23 but after type casting into smaller data type i.e. int the data get lost as our output is 1.



Types of Type Conversion in C :



There are mainly two types of type conversion in C :


  1. Implicit type conversion
  2. Explicit type conversion

➣Implicit type conversion :  

  • In this type of conversion the automatic conversion of operands takes place by the compilers.The compiler converts the all operands into data type of largest operand.
  • Implicit conversion mainly focuses on "lower data type to higher data type".
Example:

ixplicit type casting example

In this example each variable gets converted into the longest variable data type i.e. "double".



➣Explicit type conversion :  

  • In this type of conversion the programmer forcefully changes the data type of variable to another data type by using the "type" keyword. 
  • The main focus of this conversion is low to high data type because there is a chance of losing data while converting the data type of a variable from higher data type to lower data type.

Example :

Explicit type conversion  example



  Interview Questions On Type Casting                 

1. What is the output of this C code?
#include
void main()
{
float x = 0.1;
if (x == 0.1)
printf("Sanfoundry");
else
printf("Advanced C Classes");
}
a) Advanced C Classes
b) Sanfoundry
c) Run time error
d) Compile time error


 Answer : a

2. Comment on the output of this C code?

#include
void main()
{
float x = 0.1;
printf("%d, ", x);
printf("%f", x);
}
a) 0.100000, junk value
b) Junk value, 0.100000
c) 0, 0.100000
d) 0, 0.999999

 Answer : b

3. What is the output of this C code?
(7 and 8 are entered)

#include
void main()
{
float x;
int y;
printf("enter two numbers \n", x);
scanf("%f %f", &x, &y);
printf("%f, %d", x, y);
}
a) 7.000000, 7
b) Run time error
c) 7.000000, junk
d) Varies

Answer : c

4. What is the output of this C code?

#include
void main()
{
double x = 123828749.66;
int y = x;
printf("%d\n", y);
printf("%lf\n", y);
}
a) 0, 0.0
b) 123828749, 123828749.66
c) 12382874, 12382874.0
d) 123828749, 0.000000

Answer : d

5. What is the output of this C code?

#include
void main()
{
int x = 97;
char y = x;
printf("%c\n", y);
}
a) a
b) b
c) 97
d) Run time error

Answer : a

6. When double is converted to float, the value is?
a) Truncated
b) Rounded
c) Depends on the compiler
d) Depends on the standard

Answer : c

7. What is the output of this C code?

#include
int main()
{
unsigned int i = 23;
signed char c = -23;
if (i > c)
printf("Yes\n");
else 

if (i < c) printf("No\n");  

a) Yes 
b) No 
c) Depends on the compiler 
d) Depends on the operating system 

Answer : b

8. What is the output of this C code? #include 

int main()
{
int i = 23;
char c = -23;
if (i < c) 
 
printf("Yes\n");
else 
printf("No\n"); 
 }
 a) Yes 
b) No 
c) Depends on the compiler 
d) Depends on the standard 

Answer : b 

9. function tolower(c) defined in library works for
a) Ascii character set
b) Unicode character set
c) Ascii and utf-8 but not EBSIDIC character set
d) Any character set

Answer : d

10. What is the output of the below code considering size of short int is 2, char is 1 and int is 4 bytes?

#include
int main()
{
short int i = 20;
char c = 97;
printf("%d, %d, %d\n", sizeof(i), sizeof(c), sizeof(c + i));
return 0;
}
a) 2, 1, 2
b) 2, 1, 1
c) 2, 1, 4
d) 2, 2, 8

Answer : c

11. Which type conversion is NOT accepted?
a) From char to int
b) From float to char pointer
c) From negative int to char
d) From double to char

Answer : b

12. What will be the data type of the result of the following operation?
(float)a * (int)b / (long)c * (double)d
a) int
b) long
c) float
d) double

Answer : d

13. Which of the following type-casting have chances for wrap around?
a) From int to float
b) From int to char
c) From char to short

d) From char to int
Answer : b

14. Which of the following typecasting is accepted by C?
a) Widening conversions
b) Narrowing conversions
c) Both
d) None of the mentioned

Answer : c

15. When do you need to use type-conversions?
a) The value to be stored is beyond the max limit
b) The value to be stored is in a form not supported by that data type
c) To reduce the memory in use, relevant to the value
d) All of the mentioned

Answer : d

16. How type casting plays an important role in C?



Previous                                                                                   Next


For any query related to the content mail to devkumar082001@gmail.com

No comments:

Powered by Blogger.