Skip to content
HOME - C - What is Type casting? Implicit type casting and Explicit type casting:

What is Type casting? Implicit type casting and Explicit type casting:

type casting implicit/explicit

The conversion of the data type of the variable’s value into another data type during the program execution is called type casting. There are two standard ways to do type casting.

  •  Implicit type casting
  •  Explicit type casting

Implicit type casting

Implicit type casting is the one that can be performed automatically by the compiler during the program execution. In normal cases, the operands must be of similar data types. But If the data types of the operands are different then the values with lower data types are converted into higher data types. It is very important here to describe that the data types are categorized from higher-order to lower order. This classification of data types is based on the number of bytes that a data type takes in the memory. Different data types are arranged in the following higher to lower order.

1. Long Double
2. Double
3. Float
4. Long
5. Int
6. Char

In the above sequence Long double is of higher-level data type and the char is of lowest level data type. In nested parenthesis are applied, then the part of the expression in the innermost parenthesis is evaluated first.

Example:

int a;
double b;
a + b;

In the above piece of code, a is an integer type variable and b is a double type variable and we want to evaluate the expression a + b. In this expression, the data type of a is lower than b. Therefore, the value of a will be converted into double type implicitly by the compiler. The data type of a will remain the same. I.e, it remains int. Only the value is converted into another data type in order to evaluate the expression. This implicit type casting is all done automatically by the compiler itself.

Explicit type casting

Explicit type casting is done manually by the programmer or developer. In explicit type casting, the type cast operator is used. For example, convert one numeric data type into another numeric data type. Here is the simple syntax for the explicit type casting.

(type) expression;

Type: It specifies the data type to which the value of the expression is to be converted.
Expression: Expression includes a constant, variable, or maybe an expression whose data type is to be converted.

Example:

float a;
float b;
a % b;

In this above piece of code, we can see that both variables a and b are of float type. And once we run the expression a % b, it will generate a runtime error because the modulus operator can only work with integers. So in that case, the modulus operator can’t work with float variables. So the value of both the variables must be converted explicitly into an int data type.

(int) a % (int) b;

The above expression will convert the values of variable a and value of variable b into an int data type. By doing this, the above expression can be evaluated successfully. Like in the implicit type casting, it is important to explain that the values of both the variables a and b are temporarily converted while the actual data type remains float according to the above-mentioned example.

Find More:

35 thoughts on “What is Type casting? Implicit type casting and Explicit type casting:”

  1. Do you mind if I quote a couple of your posts as long as I provide credit and sources back to
    your blog? My blog is in the exact same area of interest as yours and
    my users would definitely benefit from some of the information you provide here.
    Please let me know if this okay with you. Thank you!

  2. Thanks for every other informative blog. The place else may just I am getting that type of
    information written in such an ideal method?

    I’ve a project that I am just now operating on, and I have been at the look out for such
    info.

  3. Hello! This is my first visit to your blog! We are a
    collection of volunteers and starting a new initiative in a community in the same niche.
    Your blog provided us useful information to work on. You
    have done a outstanding job!

  4. Hello my family member! I want to say that this article is awesome, nice written and
    include almost all important infos. I’d like to peer more posts like this .

  5. You’ve made some good points there. I looked on the
    web for additional information about the issue and found most individuals will go
    along with your views on this website.

  6. Today, I went to the beachfront with my children. I found a sea shell and
    gave it to my 4 year old daughter and said “You can hear the ocean if you put this to your ear.” She placed the shell to her ear and screamed.
    There was a hermit crab inside and it pinched her ear. She never wants to go back!

    LoL I know this is completely off topic but I
    had to tell someone!

  7. I do not even know how I ended up here, but I thought
    this post was great. I don’t know who you are but definitely you are going
    to a famous blogger if you aren’t already 😉
    Cheers!

  8. An impressive share! I have just forwarded this onto a friend who had been doing a little homework on this.

    And he actually bought me dinner due to the fact that I stumbled upon it for him…
    lol. So allow me to reword this…. Thank YOU for the meal!!
    But yeah, thanx for spending some time to talk about this topic
    here on your web site.

  9. I have been exploring for a little for any high-quality articles or blog posts
    on this sort of space . Exploring in Yahoo I eventually stumbled upon this website.
    Reading this info So i’m glad to convey that I have an incredibly
    excellent uncanny feeling I came upon just what I needed.
    I such a lot unquestionably will make sure to don?t fail to remember this
    site and provides it a glance on a relentless basis.

  10. certainly like your web site however you have to test the spelling on quite a few of your
    posts. Several of them are rife with spelling problems
    and I in finding it very troublesome to tell the truth then again I’ll certainly
    come again again.

  11. Having read this I thought it was really enlightening. I appreciate you spending some time and effort to put this short article together.
    I once again find myself personally spending way too much time
    both reading and leaving comments. But so what, it was still worth it!

  12. whoah this weblog is fantastic i really like studying your articles.
    Keep up the good work! You realize, many persons are hunting around for
    this information, you can aid them greatly.

  13. You are so interesting! I don’t believe I’ve read through a single
    thing like this before. So good to discover another person with a
    few genuine thoughts on this subject. Seriously.. thank you for starting this up.
    This site is something that is needed on the internet, someone with a bit
    of originality!

  14. My partner and I stumbled over here coming from a different
    page and thought I might as well check things out.
    I like what I see so now i am following you. Look forward to checking out your web page yet again.

  15. It’s actually a cool and helpful piece of info.

    I am happy that you shared this useful information with
    us. Please keep us informed like this. Thanks for sharing.

  16. Wonderful beat ! I would like to apprentice while you amend your web site, how
    can i subscribe for a blog web site? The account helped me a acceptable deal.
    I had been a little bit acquainted of this your broadcast provided bright
    clear concept

  17. Hello, I think your blog might be having browser compatibility issues.
    When I look at your blog in Safari, it llooks fine but when opening in Internet Explorer,
    it has some overlapping. I just wanted to give yoou a quick heads up!
    Other then that, amazing blog!

  18. Howdy! This blog post couldn’t be written any better!
    Reading through this post reminds me of my previous roommate!
    He continually kept talking about this. I will send this information to him.
    Fairly certain he will have a good read. Thank you
    for sharing!

Leave a Reply

Your email address will not be published.

en English
X