Guides:C/C Crash Course/Other types and type casting

From CoderGuide

Jump to: navigation, search

Back to TOC

Type casting, and cast conversion

Unlike some languages, such as Java, C and C++ will convert any data type to another (with the exception of pointers) without making a fuss about it, even if it is too big to fit into the, it will just lop off whatever doesn't fit, and put what remains into the smaller data type. So, if you're trying to put a 16-bit integer into a 8-bit integer (a char), only the lower 8-bits of the 16-bit integer will be stored in the 8-bit integer, the higher 8-bits will be lost.

For floating point numbers being converted to integers, the fractional part (everything to the right of the decimal) is dropped and may be rounded to the nearest whole number, and then stored in the integer. If the floating point number has a large positive exponent, then you're out of luck in trying to fit that number into your integer (but the compiler won't complain or issue any warnings if you try). You can force a data type to be treated as something else. Basically, it is a forced conversion, and we call this type-casting. It can be useful, especially when you're doing some math and you want to ensure all the numbers are being treated the same way. To do this, we specify the data type we want it to be converted to before the variable. Here is an example:

int main(){
        double fa=0.12,fb=0.006;
        int ia=1,ib=2;
        fa=(float ia)+fb;

Most of the time you won't have to use pointer arithmetic, but sometimes you can make your code a little faster and cleaner if you use it. It's just an extra tool to add to your programming toolbox.

const for Constant data

const is a keyword you can add before specifying a data type to indicate that the data in that variable can not be changed by the program. This means, you must initialize constant variables at the time you create them such as:

const float version=9.9;

You can achieve similar functionality using the #define compiler directive, which, instead of allocating space for a variable, simply replaces every occurrence of that defined label with whatever follows that statement. All #define compiler directives should appear at the beginning of your code, right after all #include compiler directives.

#define VERSION 9.9

We'll talk more about compiler directives later, but lest first concentrate on learning how to do something useful with C. The const keyword finds its use most in function declarations where you want to be sure you don't alter the variables that are passed to it.

Other Types

There are some other special identifiers: static is used like const, but it has a special meaning. When it's used for global variables, it marks that variable as being private to that object file/module. When it's used within a function, then the value of that variable will be preserved after the function finishes execution. We will discuss that more when we get into writing our own libraries and functions.

There is also typedef that allows you to create your own data types (or, at least, create your own name for them). Here is an example of typedef in action:

#include <stdio.h>
typedef unsigned char bool; /* for true/false boolean falues */
#define true 1
#define false 0
int main(){
   bool a;
   else printf("False!\n");
   /*there's nothing stopping us from doing this either:*/
   /* While we call it bool, it still is a unsigned character
      at it's heart*/
Personal tools