Type Casting and Error Handling - C Programming Professional Made easy (2015)

C Programming Professional Made easys (2015)

Chapter 9 Type Casting and Error Handling

Type Casting

One way to change a variable from a certain type of data to another is to use type casting. For instance, if you wish to store a long value into an integer, you can type cast long to int. Also, you can turn a value from a certain type to another by making use of the cast operator (type_name) expression.

The following is an example of a floating-point operation dividing two integer variables as a result of the usage of a cast operator:

#include <stdio.h>

main ( )

{

int sum = 17, count = 5;

double mean;

mean = (double) sum / count;

printf (“The mean value is : %f\n”, mean);

}

Once this code is executed, the following output is shown:

The mean value is : 3.400000

You should take note that in this case, the cast operator precedes the division process. Hence, the sum value is converted first into a type double. Eventually, it gets divided by count and yields to a double value.

Integer Promotion

The process wherein the integer values that are smaller than unsigned int or int are turned into unsigned int or int is called integer promotion. Consider the following example:

#include <stdio.h>

main ( )

{

int i = 17;

char c = ‘c’; /* The ASCII value is 99 */

int sum;

sum = i + c;

printf (“The value of sum : %d\n”, sum);

}

Once the above code is executed, the following output is shown:

The value of sum : 116

The value of the sum is 116 because the compiler does integer promotion and converts the value of c to ASCII before it performs the actual addition process.

Usual Arithmetic Convertion

Usual arithmetic convertions are performed to cast values in a common type. The compiler does integer promotion. If the operands still have different types, they are converted into the highest type in this hierarchy:

The usual arithmetic conversions are neither done for the logical operators || and && nor the assignment operators. Consider the example below:

#include <stdio.h>

main ( )

{

int i = 17;

char c = ‘c’; /* The ASCII value is 99 */

float sum;

sum = i + c;

printf (“The value of sum : %f\n”, sum);

}

Once the above code is executed, the following output is shown:

The value of sum : 116.000000

The first c is converted into an integer. However, because the final value is a double, the usual arithmetic conversion is applied and the compiler converts i and c into a float, adds them, and gets a float result.

Error Handling

In the C programming language, direct support for error handling is not provided. Nonetheless, it gives access at lower levels in the form of return values. Most C functions call return NULL or -1 in case of errors and set an error code errno.

As a programmer, you should make it a habit to set errno to 0 during the time of your program initialization. Remember that the value 0 indicates the absence of error in the program. In addition, see to it that you always check if a divisor is zero. If it is, you may encounter a runtime error.

Errno, Perror ( ), and Strerror ( )

Perror ( ) and strerror ( ) are functions that display text messages associated with errno. Perror ( ) displays the string that you pass onto it, a colon, a space, and the text of your current errno value. Strerror ( ), on the other hand, returns a pointer to the text of your current errno value.

Conclusion

Thank you again for downloading this book!

I hope this book was able to help you to understand the complex terms and language used in C. this programming method can put off a lot of users because of its seemingly complexity. However, with the right basic knowledge, soon, you will be programming more complex things with C.

The next step is to start executing these examples. Reading and understanding this book is not enough, although this will push you into the right direction. Execution will cement the knowledge and give you the skill and deeper understanding of C.

Finally, if you enjoyed this book, please take the time to share your thoughts and post a review on Amazon. We do our best to reach out to readers and provide the best value we can. Your positive review will help us achieve that. It’d be greatly appreciated!

Thank you and good luck!

thank-you

Click here to leave a review on amazon.com