i am having problem in type casting for lpc2129 please can any one give me an example of type casting which works,,,
(char)123
sir i know how to declare type casting; but alteast i want 1 example were it has been simulated correctly
Well, I'm much more interested in seeing any example where type casting have _not_ been simulated correctly. A failure to work correctly on the real hardware could indicate something seriously wrong with the compiler. A failure to work correctly when simulated could also indicate something seriously wrong with the simulator.
Typecasts should always work, as long as they are valid, i.e. not breaking any alignment rule or similar. The C language standard contains information about valid type casts and the situations where the results are undefined. The compiler can't catch and warn you about casts with undefined behaviour some of them can only be found out about during runtime.
If you have any program where a typecast doesn't work as expected, you have almost certainly missed one of the C standard paragraphs. Post a minimalistic example of your failed cast and people can probably spot why it fails.
main { unsigned long ii; func_foo(ii); } func_foo(unsigned long kk) { uunsigned int jj; jj =(unsigned int)kk; }
please correct me if i m wrong
Your code does not do anything so there is nothing to comment about.
If the function never makes use of jj, there is no need to perform any assign so no need to perform any type cast.
But a more important question - what is your reason for typecasting long to int? For some architectures they have the same numeric range. For some, long will be larger. So when code uses both int and long int, neither of the types should be used unless you as developer knows that both types are good enough for what they are used for.
If you do have code that needs to use specific data type sizes, and needs to convert between them, it's normally better to use int8_t, int16_t, int32_t and similar or maybe int_least8_t, ... or maybe int_fast8_t, ...
In the end, you do type casts for a reason. What is your reason? And what numeric range do you have on your data? And what did you expect should happen? And what did happen?
View all questions in Keil forum