Computer Science, asked by vishal5040, 1 year ago

Explain type cast and size of operator in C language with example.

Answers

Answered by diyachak72
0

Answer:

Explanation:

We can use typecast operator to convert the type of value returned by a pointer deference expression to another suitable type. For example, if pa is a pointer to a variable a of type int, we can use typecast as (double) *pa to convert the integer value of *pa to type double.

 

The C language also allows us to use typecast to convert the type of a pointer, as in (char *) pa. This converts the pointer pa, which is assumed to be a pointer to int, to char *, i. e., a character pointer. Such typecasts are essential and are commonly used while dealing with void pointers. However, it should be used very carefully in other situations as it may lead to erroneous situations. For example, the expression *pa interprets the contents of two (or four) consecutive memory locations pointed to by pointer pa as an integer, whereas the expression * ( (char *) pa) interprets the contents of only one byte pointed to by pa, i. e., some part of integer value, as a character. Nevertheless, there are situations where this kind of conversion is useful, as illustrated in Example.

The sizeof operator, when used with a pointer variable, as in sizeof (pa), gives us the size of a pointer variable, i. e., memory required for its storage in bytes.

Example of function to print binary representation of numbers

Consider that we wish to print binary representations of numbers of different data types (int, short int, float, long double, etc.). One approach to do this is to write a separate function for each data type. However, this is tedious and time-consuming. Instead, we can write a single function named print_binary the as shown below.

I KNOW THIS MUCH ONLY

Similar questions