WalzoneInterview Prep
📞 Interviewing soon? Practice with a realistic AI mock phone interview — it calls you, then scores you. First 15 min FREE →

C · Intermediate · question 22 of 100

Explain the concept of type casting in C and how to perform it.?

📕 Buy this interview preparation book: 100 C questions & answers — PDF + EPUB for $5

In C programming language, type casting (also known as type conversion) is the process of converting one data type to another data type. Type casting can be performed in two ways: implicit casting and explicit casting.

Implicit Casting: Implicit casting is performed automatically by the compiler when a value of one data type is assigned to a variable of another data type. For example, if an integer value is assigned to a float variable, the integer value is automatically converted to a float value.

    int x = 10;
    float y = x; // Implicit casting from int to float

In this example, the integer value 10 is assigned to the variable x, and then the value of x is assigned to the variable y, which is of type float. The compiler automatically performs an implicit cast from the integer data type to the float data type.

Explicit Casting: Explicit casting is performed by the programmer using a cast operator to convert a value of one data type to another data type. The cast operator is represented by parentheses enclosing the target data type, followed by the value to be converted.

    float x = 3.14;
    int y = (int)x; // Explicit casting from float to int

In this example, the float value 3.14 is assigned to the variable x, and then the value of x is cast to an integer using the explicit cast operator (int). The resulting integer value is assigned to the variable y.

Explicit casting can also be used to convert between pointer types, such as when converting a void pointer to another pointer type.

    int x = 10;
    void *ptr = &x; // Assigning address of x to a void pointer
    int *p = (int*)ptr; // Explicit casting from void pointer to int pointer

In this example, the address of the variable x is assigned to a void pointer variable ptr. Since a void pointer can point to any data type, an explicit cast is needed to convert it to an integer pointer p. The explicit cast operator (int*) is used to cast the void pointer to an integer pointer.

In summary, type casting is the process of converting one data type to another data type in C programming language. Implicit casting is performed automatically by the compiler, while explicit casting is performed manually using the cast operator. Type casting can be used to convert between different data types and pointer types.

Reading is step one. Saying it out loud is the interview. Our AI interviewer calls your phone and runs a realistic C interview — then scores it.
📞 Practice C — free 15 min
📕 Buy this interview preparation book: 100 C questions & answers — PDF + EPUB for $5

All 100 C questions · All topics