What will be the data type returned for the following C function? #include <stdio.h> int func() { return (double)(char)5.0; }
Question
What will be the data type returned for the following C function? #include <stdio.h> int func() { return (double)(char)5.0; }
Solution
The data type returned by the function will be int.
Here's the step by step explanation:
-
The function is declared with int as the return type. This means that, regardless of the operations inside the function, it should return an integer.
-
Inside the function, there is a return statement with a complex expression. Let's break it down:
- 5.0 is a double.
- (char)5.0 casts the double to a char. In C, a char is actually a very small integer, so this will truncate the double 5.0 to the integer 5.
- (double)(char)5.0 casts the char back to a double. However, since the char was 5 (an integer), the resulting double is also 5.0.
-
Despite the final result of the expression being a double, the function will still return an int because of its declaration. This means that the double 5.0 will be truncated again to the integer 5 before being returned.
So, the function returns an int.
Similar Questions
What will be the data type returned for the following C function? #include <stdio.h> int func() { return (double)(char)5.0; }
Explain different datatypes in C
What are the basic data types supported in C?
What is the type of data that "c" is pointing to?123456789#include <stdio.h>int main() { int a = 10; double b = 3.14; void *c; c = &a; return 0;}Marks : 1Negative Marks : 0Answer hereintvoiddoubleUnknown type
t the correct answerThe size_t data type is commonly used as the return type for which library function in C?
Upgrade your grade with Knowee
Get personalized homework help. Review tough concepts in more detail, or go deeper into your topic by exploring other relevant questions.