Results 1 to 6 of 6

Thread: What is typecasting and how does it work in C programming

  1. #1
    Join Date
    Jun 2009
    Posts
    53

    What is typecasting and how does it work in C programming

    I have some simple but important points to discuss about typecasting in C programming. This concept is new to me and so I am finding it difficult to understand this. First and foremost, what is typecasting and how does it work? When does a compiler uses typecasting? Can you create your own typecasting functions or variables? What all are the datatypes that can be typecasted?

  2. #2
    Join Date
    Jan 2008
    Posts
    1,521

    Re: What is typecasting and how does it work in C programming

    Typecasting involves converting an expression of a given type into another type. In C, you can perform typecasting by placing the datatype name in parentheses and placing this in front of the value. For example:

    int main()
    {
    int x;
    x = (int)8 / 6;
    return 0;
    }

    In the above example, the usual result of 8/6 should be 1.3333 but since we have typecasted it, the x value would be "1". The later part is simply dropped.

  3. #3
    Join Date
    Apr 2008
    Posts
    1,948

    Re: What is typecasting and how does it work in C programming

    Typecasting is very essential in programming to avoid any unnecessary errors or warnings. Sometimes C compilers need you to define as to what type of typecasting is needed whereas on the other it implicitly converts. The usual notification is "(<type>)<value>". Typecasting is like you want to make a variable of one type, such as an int, to act like another type, such as char, for single statement. Although typecast has so many benefits but still it is considered as a headache because it simply truncates your data at important steps.

  4. #4
    Join Date
    Nov 2008
    Posts
    1,221

    Re: What is typecasting and how does it work in C programming

    what is typecasting and how does it work?
    This question is already answered, so nothing to add to this.

    When does a compiler uses typecasting?
    Whenever the compiler finds the data types of left and right most expression is different, it can either use implicit conversion or uses the explicit typecasting. If it is unavailable then an error message is returned.

    Can you create your own typecasting functions or variables?
    Yes, as described above.

    What all are the datatypes that can be typecasted?
    The list is long such as short to int, unsigned to int, int to float, float to double, int to double, char to int and so on....

  5. #5
    Join Date
    Nov 2009
    Posts
    1

    Re: What is typecasting and how does it work in C programming

    I have a question here, regarding this typecasting.

    - will there be any potential issue with typecasting. Like will it introduce any alignment issue for a word processor (word alignment issue)
    - I saw some compiler used to show warnings if a integer variable is typecasted to an unsigned int or char. something like 'narrow down the datatype' issue.

  6. #6
    Join Date
    Feb 2008
    Posts
    1,852

    Re: What is typecasting and how does it work in C programming

    Typecasting is a way to make a variable of one type, such as an int, act like another type, such as a char, for one single operation. You still need to make sure that actions that should logically be indivisible actually are, that you avoid deadlocks, that your algorithm is sufficiently parallel for the performance you need, etc.

Similar Threads

  1. What does CVS mean in programming?
    By Gajananvihari in forum Software Development
    Replies: 5
    Last Post: 12-03-2010, 05:09 PM
  2. What does DOM mean in programming?
    By Gadin in forum Software Development
    Replies: 5
    Last Post: 11-03-2010, 05:33 PM
  3. Object Typecasting in C sharp
    By Bottlenecked in forum Software Development
    Replies: 5
    Last Post: 09-02-2010, 11:03 AM
  4. Socket programming: Is any new Programming Language?
    By Kushan in forum Software Development
    Replies: 3
    Last Post: 14-11-2009, 11:13 AM
  5. Replies: 3
    Last Post: 13-12-2008, 01:49 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Page generated in 1,714,050,348.00026 seconds with 16 queries