473,473 Members | 1,808 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Able to write integer to char without cast.

Markus
6,050 Recognized Expert Expert
Is that normal behaviour? That is, x is of type char and when trying to then assign a number to x, no type errors are raised? However, if it was the other way around, char into int, it wouldn't compile.

Does this have to do with the memory sizes of types?

Excuse my ignorance, I'm a little old fool who's only ever really worked with dynamic 'languages'. :D
Jul 7 '09 #1
4 2600
JosAH
11,448 Recognized Expert MVP
@Markus
From the Standard:

When an integer is demoted to an unsigned integer with smaller
size, the result is the nonnegative remainder on division by the
number one greater than the largest unsigned number that can be
represented in the type with smaller size. When an integer is demoted
to a signed integer with smaller size, or an unsigned integer is
converted to its corresponding signed integer, if the value cannot be
represented the result is implementation-defined.
No warning is issued so you can do what you did in an implementation defined way.

kind regards,

Jos

ps. Old?
Jul 7 '09 #2
Markus
6,050 Recognized Expert Expert
@JosAH
Young wouldn't have worked...

But I am little.

Oh, and thanks.

Mark.
Jul 7 '09 #3
donbock
2,426 Recognized Expert Top Contributor
@Markus
I would have expected that to work just fine. What compiler message did you get?
Jul 8 '09 #4
weaknessforcats
9,208 Recognized Expert Moderator Expert
Markus is talking about automatic integer promotions:

If the value can be put in an int, then it is converted to an int
otherwise, it is converted to an unsigned int.
Now, if either operand is unsigned long, then the other is converted to unsigned long.
Now, if either operand is long, then the other is converted to long.
Now, if either operand is an unsigned int, then the other is converted to unsigned int.
Otherwise, both are ints.

Then the operator is performed and the type of the result is the type of the operands.


So, that char is converted to int. The calculation is done as an int and the result put in a char. It is your responsibility to insure the char can hold the result.

Likewise, there are separate automatic promotions for floating point.
Jul 11 '09 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

4
by: Ray | last post by:
When a single-bit bitfield that was formed from an enum is promoted/cast into an integer, does ANSI C say anything about whether that integer should be signed or unsigned? SGI IRIX cc thinks it is...
14
by: Markus Dehmann | last post by:
The following if condition if((stream = fmemopen((void*)str, strlen(str), "r")) == NULL){ // ... } gives me a warning: assignment makes pointer from integer without a cast But only when I...
4
by: Dawn Minnis | last post by:
Hi When I compile my files I get the following: driver.c: In function `main': driver.c:49: warning: assignment makes integer from pointer without a cast driver.c:50: warning: assignment...
16
by: jose_luis_fdez_diaz_news | last post by:
Hi, If I don't include <libgen.h> I get then warnig below in regcmp call: warning: improper pointer/integer combination: op "=" but if I include it the warning is not shown, but them program...
18
by: planetzoom | last post by:
Given the following code: #include <stdio.h> int main(void) { char array = "What is your favorite car?"; void *vp = &array; printf("%s\n", vp);
6
by: Kinbote | last post by:
Hi, I'm trying to make a function that opens a file, reads it in line by line, puts each line into an malloc'd array, and returns the array. I suspect I'm going about it in an atypical fashion, as...
1
by: woods1313drew | last post by:
The following code in c+ gives me the warning assignment makes integer from pointer without a cast. destination is set as char destination to limit the input string to 10 characters. name is an...
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,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.