Go Back   TechArena Community > Software > Software Development
Become a Member!
Forgot your username/password?
Tags Active Topics RSS Search Mark Forums Read

Reply
 
Thread Tools Search this Thread
  #1  
Old 26-08-2009
Member
 
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?
Reply With Quote
  #2  
Old 26-08-2009
Member
 
Join Date: Jan 2008
Posts: 1,515
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.
Reply With Quote
  #3  
Old 26-08-2009
Member
 
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.
Reply With Quote
  #4  
Old 26-08-2009
Member
 
Join Date: Nov 2008
Posts: 1,221
Re: What is typecasting and how does it work in C programming

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

Quote:
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.

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

Quote:
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....
Reply With Quote
  #5  
Old 06-11-2009
Member
 
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.
Reply With Quote
  #6  
Old 06-11-2009
Member
 
Join Date: Feb 2008
Posts: 1,848
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.
Reply With Quote
Reply

  TechArena Community > Software > Software Development
Tags: , , ,



Thread Tools Search this Thread
Search this Thread:

Advanced Search


Similar Threads for: "What is typecasting and how does it work in C programming"
Thread Thread Starter Forum Replies Last Post
What does CVS mean in programming? Gajananvihari Software Development 5 12-03-2010 05:09 PM
What does DOM mean in programming? Gadin Software Development 5 11-03-2010 05:33 PM
Object Typecasting in C sharp Bottlenecked Software Development 5 09-02-2010 11:03 AM
Socket programming: Is any new Programming Language? Kushan Software Development 3 14-11-2009 11:13 AM
JetBrains Introduces a New Programming Paradigm With its Meta Programming System JoeFrat Software Development 3 13-12-2008 01:49 PM


All times are GMT +5.5. The time now is 09:01 AM.