473,386 Members | 1,775 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

type casting

Hello, All!

Given the sample piece of code I have:

#include <stdio.h>
#include <string.h>

int main(void)
{
short int i, j;
int k;
i = j = 10;
k = i + j;

return 0;
}

So, here I expect that 'i' and 'j' will be type-casted to 'int' type, while
in debugger (exploring memory dump) I see these variables still occupy 2
bytes (i.e. 'short' type).

Is it normal behavior? I suppose, I just don't catch some point.

With best regards, Roman Mashak. E-mail: mr*@tusur.ru
Nov 15 '05 #1
9 3967
"Roman Mashak" <mr*@tusur.ru> wrote:
#include <stdio.h>
#include <string.h>
Two superfluous headers; you use nothing from either.
int main(void)
{
short int i, j;
int k;
i = j = 10;
k = i + j;

return 0;
}

So, here I expect that 'i' and 'j' will be type-casted to 'int' type,
Why on earth would you expect that? There is not a single cast in your
program.
Even supposing you mean converted rather than cast, yes, their values
will, but only temporarily during the assignments. This has no influence
on their actual sizes as objects.
while in debugger (exploring memory dump) I see these variables still
occupy 2 bytes (i.e. 'short' type).


Of course. You have explicitly asked for them to do so.

That _the values of_ these shorts will then be converted to int before
being added to one another and/or assigned to other objects (in the
first case, being converted back to short before the assignment),
doesn't change the size of the objects themselves.

Richard
Nov 15 '05 #2
Roman Mashak <mr*@tusur.ru> wrote:
Hello, All!

Given the sample piece of code I have:

#include <stdio.h>
#include <string.h>

int main(void)
{
short int i, j;
int k;
i = j = 10;
k = i + j;

return 0;
}

So, here I expect that 'i' and 'j' will be type-casted to 'int' type, while
in debugger (exploring memory dump) I see these variables still occupy 2
bytes (i.e. 'short' type).


Note that the term "type-cast" is used for explicit conversion of types
by usage of the so called cast operators. What you'ld expect was
probably a conversion.

It's not the variables themselve that would get converted, but the
operands of the operation. Here even the operands do not have to be
converted because i and j are of the same integer type and the
+ operator doesn't require any special conversion.

So if you do something along

i = 18888;
j = 22222;
k = i+j;

You may get undefined behavior (supposing that SHRT_MAX is 32767).
Even though k is capable of holding the result, the operation will not
be done in the type of k, but with the type of i and j.

Also note that explicit type casts do not change the size of a variable.
A type cast is an operator jsut like + or *, and has a result. The type
of the result is that specified by the cast operator.

--
Z (zo**********@web.de)
"LISP is worth learning for the profound enlightenment experience
you will have when you finally get it; that experience will make you
a better programmer for the rest of your days." -- Eric S. Raymond
Nov 15 '05 #3
Hello, Richard!
You wrote on Tue, 23 Aug 2005 06:47:33 GMT:

RB> Why on earth would you expect that? There is not a single cast in your
RB> program.
RB> Even supposing you mean converted rather than cast, yes, their values
RB> will, but only temporarily during the assignments. This has no
RB> influence on their actual sizes as objects.
Oops, I missed the terms. Converting != casting.
??>> while in debugger (exploring memory dump) I see these variables still
??>> occupy 2 bytes (i.e. 'short' type).

RB> Of course. You have explicitly asked for them to do so.

RB> That _the values of_ these shorts will then be converted to int before
RB> being added to one another and/or assigned to other objects (in the
RB> first case, being converted back to short before the assignment),
RB> doesn't change the size of the objects themselves.
Ok, I understood. In case of implicit type-casting - will it keep the object
size in memory ?

With best regards, Roman Mashak. E-mail: mr*@tusur.ru
Nov 15 '05 #4
Roman Mashak wrote:
Hello, Richard!
You wrote on Tue, 23 Aug 2005 06:47:33 GMT:

RB> Why on earth would you expect that? There is not a single cast in your
RB> program.
RB> Even supposing you mean converted rather than cast, yes, their values
RB> will, but only temporarily during the assignments. This has no
RB> influence on their actual sizes as objects.
Oops, I missed the terms. Converting != casting.
??>> while in debugger (exploring memory dump) I see these variables still
??>> occupy 2 bytes (i.e. 'short' type).

RB> Of course. You have explicitly asked for them to do so.

RB> That _the values of_ these shorts will then be converted to int before
RB> being added to one another and/or assigned to other objects (in the
RB> first case, being converted back to short before the assignment),
RB> doesn't change the size of the objects themselves.
Ok, I understood. In case of implicit type-casting - will it keep the object
size in memory ?
There is no implicit typecasting. You either cast or you don't.
Conversions can happen implicitly (without you doing anything about it)
or explicitly (with you casting).
Reread Richard's answer.

-Michael
With best regards, Roman Mashak. E-mail: mr*@tusur.ru

--
E-Mail: Mine is an /at/ gmx /dot/ de address.
Nov 15 '05 #5
Zoran Cutura wrote:
It's not the variables themselve that would get converted, but the
operands of the operation. Here even the operands do not have to be
converted because i and j are of the same integer type and the
+ operator doesn't require any special conversion.


It does require the Usual Arithmetic Conversions, which include integer
promotion. So i and j _do_ get promoted to int before the addition takes
place.
Christian
Nov 15 '05 #6
Christian Kandeler <ch****************@hob.de_invalid> wrote:
Zoran Cutura wrote:
It's not the variables themselve that would get converted, but the
operands of the operation. Here even the operands do not have to be
converted because i and j are of the same integer type and the
+ operator doesn't require any special conversion.


It does require the Usual Arithmetic Conversions, which include integer
promotion. So i and j _do_ get promoted to int before the addition takes
place.

uuhuu ... I stand corrected. My brain seems to shrink an leave holes all
over.

Sorry for the incorrect explanation.

--
Z (zo**********@web.de)
"LISP is worth learning for the profound enlightenment experience
you will have when you finally get it; that experience will make you
a better programmer for the rest of your days." -- Eric S. Raymond
Nov 15 '05 #7
Michael Mair wrote:

Roman Mashak wrote:

Ok, I understood. In case of implicit type-casting
- will it keep the object size in memory ?


There is no implicit typecasting. You either cast or you don't.


Typecasting is something that happens to thespians.

http://www.m-w.com/cgi-bin/dictionary?va=typecasting

In C, a cast, is just called a "cast"

--
pete
Nov 15 '05 #8
Zoran Cutura wrote:
Note that the term "type-cast"
is used for explicit conversion of types
No, it isn't.
A type cast is an operator jsut like + or *,
and has a result.


It's just a "cast", not a "type cast".

There is no other kind of cast in C, such that the word "cast"
needs an adjective in front of it to distinguish it.

--
pete
Nov 15 '05 #9
pete wrote:
Michael Mair wrote:
Roman Mashak wrote:


Ok, I understood. In case of implicit type-casting
- will it keep the object size in memory ?


There is no implicit typecasting. You either cast or you don't.

Typecasting is something that happens to thespians.

http://www.m-w.com/cgi-bin/dictionary?va=typecasting

In C, a cast, is just called a "cast"


Argh. I was not aware of this -- seemingly there are enough like
me out there:
http://en.wikipedia.org/wiki/Typecast
I don't know where I picked it up but I guess it was around here;
so I use "to cast" for the action and "typecasting" as noun.
However, thank you :-)

Cheers
Michael
--
E-Mail: Mine is an /at/ gmx /dot/ de address.
Nov 15 '05 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Suzanne Vogel | last post by:
** Isn't the 'static_cast' operator the same as traditional type casting? ie, Aren't the following ways of getting b1, b2 the same? // 'Derived' is derived from 'Base' Derived* d = new...
4
by: Jacob Jensen | last post by:
This question has probably been asked a million time, but here it comes again. I want to learn the difference between the three type cast operators: static_cast, reinterpret_cast, dynamic_cast. A...
3
by: Steve Teeples | last post by:
Can someone explain how to cast an object to a specific type during runtime? // This line of code tells me the objects type. System.Type type = System.Type.GetType("string of the type"); //...
10
by: Bob | last post by:
This has been bugging me for a while now. GetType isn't availble for variables decalred as interface types, I have to DirectCast(somevariable, Object). In example: Sub SomeSub(ByVal...
23
by: René Nordby | last post by:
Hi there, Is there anyone that knows how to do the following? I have a class A and a class B, that 100% inherits from class A (this means that I don't have other code in class B, than...
10
by: lovecreatesbeauty | last post by:
Why (type*)pointer isn't equal to *(type**)pointer, In the code snippet, it shows that: (int *) == (int **) , (int *) != (*(int **)) . Does type-casting change the address? or...
6
by: crook | last post by:
I have code below and it works properly but when I'm compiling it with "--pedantic" flag, GCC(3.4.2) shows such warning: "ISO C forbids casting nonscalar to the same type". How can I change this...
15
by: shuisheng | last post by:
Dear All, Assume I have a class named Obj. class Obj { }; And a class named Shape which is derived from Obj. class Shape: public Obj
11
by: Frederic Rentsch | last post by:
Hi all, If I derive a class from another one because I need a few extra features, is there a way to promote the base class to the derived one without having to make copies of all attributes? ...
8
by: Smithers | last post by:
Are there any important differences between the following two ways to convert to a type?... where 'important differences' means something more profound than a simple syntax preference of the...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.