Lesson 4: Casting data types


E-mail this post



Remember me (?)



All personal information that you provide here will be governed by the Privacy Policy of Blogger.com. More...



Someone's going to assume I'm workaholic since it has passed less then twelve hours since my last programming post, but what the heck, I assume you are willing to learn since you're here - so here's fourth lesson for all of you. This lesson is about transformation (casting) of integers and real data types, and explanations why this job is necessary in C language for compiler to understand the syntax. It's a short one, but with compressed value.

DECLARATION OF INTEGER (int) TYPE:



CASTING DATA TYPES:

Result type of arithmetic phrase depends of operand types inside it. When a phrase is consisted of various data types, result type is set by defined casting rules. Rules for casting various data types in C language are oriented to higher data type. There are two ways of casting data types in C:

· automatic (implicit)

· given (explicit)


Implicit Casting (automatic transformation) works in a way that a variable (operand) of data type that is smaller in length (than data type of second variable) (operand), transforming internally to variable of data type with longer number length. It may sound mixed up, but here is an example:


short int -> int -> unsigned int ->long int -> unsigned long int -> float -> double -> long double



This table shows standard automatic casting of one type of operand to another type of operand in binary phrases (according to MSDN Library for MS Visual C++ 6.0):


Operand Type

Automatic Casting

One of operands is long double.

Second operand transforms to long double.

Previous rule doesn’t apply, and one of operands is double.

Second operand transforms to double.

Previous rule doesn’t apply, and one of operands is float.

Second operand transforms to float.

Previous rules don’t apply, and none of operands is real type operand.

Integer casting is made by following rules:

  • If one of operands is unsigned long, then another operand transforms to unsigned long.
  • If previous rule doesn’t apply, and if one of operands is long, and other is unsigned int, then both operands transform to unsigned long.
  • If previous rule doesn’t apply, and if one of operands is long, then another operand is transformed to long.
  • If previous rule doesn’t apply, and if one of operands is unsigned int, then another operand transforms to unsigned int.
  • If previous rules do not apply, then both operands transform to int.


Important:
rules for integer and real data type casting can differ from one translator programs to another.



Exmple:

int a;

unsigned long b;

float f, g;

double d;

g = a + f; // a transforms to float

d = a + b; // a and b transform to unsigned long, adding

// is produced in unsigned long domain and then

// the result type unsigned long is transformed

// to double


Example:

long lm = LONG_MAX, l; // LONG_MAX = 2147483647

unsigned long um = ULONG_MAX; // ULONG_MAX = 4294967295

float f;

l = lm + 1; // result: -2147483648 (fill over to neg. field)

f = lm + 1; // result: -2147483648.000000 (result is made

// in long domain)

l = um + 1; // result: 0 (fill over)

f = um + 1; // result: 0.000000 (result is made in

// unsigned long domain)



Explicit Casting (given transformation) of data types has higher priority then automatic transformation. General declaration of explicit (given) cast (cast operator):

(data_type) operand

Operand can be variable or phrase.


Example:

a = (int) c;

b = (double)d + c;



Technorati Tags: , , , , , ,



4 Responses to “Lesson 4: Casting data types”

  1. Anonymous Anonymous 

    If you continue this way, in a month or two you will be able to merge all into a book.
    Great work.

  2. Anonymous Anonymous 

    I am corn-fused, I have been interested in learning a programming language for quite some time now but, never found the time to actually sit down and try and learn a language till 2 days ago. Before coming to this website I downloaded Code::Block ver. 1.0rc2 as an IDE to use. I have learned about loops and the famous(I guess) "Hello World". I thought I was doing ok but after reading the last couple chapters and then rereading them to try and understand everything you have put out...How do I say this tactfully?????? Do I really need to go back and learn this stuff completely inorder to Program?

  3. Anonymous Anonymous 

    its amazing. atlast i found a gud tutorial for C

  4. Anonymous Anonymous 

    SHOW LOTS OF INFO! SHOW US WHAT WE ARE LOOKING FOR!

Leave a Reply

      Convert to boldConvert to italicConvert to link

 


German Flag Spanish Flag French Flag Italian Flag Portuguese Flag Japanese Flag Korean Flag Chinese Flag British Flag


This Website is optimized for Firefox. Users browsing with Internet Explorer may encounter problems while viewing pages.


C++ Maniac



Learn C



Additional



#include



Learn Converting



Appendix


Links


Previous posts




Daily Lessons for programming in Visual Studio, using C code.