473,667 Members | 2,583 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

integer promotions

Hi all,

Assume x is declared as char x, and a function f( ) with prototype void
f(char y).

Assume f( ) is invoked somewhere in the code and:

1. The f( )'s prototype is provided, then an one-byte copy of x is passed to
f( ) (unix/intel platform).
2. The prototype is not provided, then a four-byte int, which contains the
value of x, is passed to f( ) (unix/intel platform).
3. If f( ) was a variadic function instead, a four-byte int, which contains
the value of x, is passed to f( ) unix/intel platform).

Right?

TIA
Nov 15 '05 #1
14 1700
"jimjim" <ne*****@blueyo nder.co.uk> wrote:
Assume x is declared as char x, and a function f( ) with prototype void
f(char y).
Assume f( ) is invoked somewhere in the code and:
1. The f( )'s prototype is provided, then an one-byte copy of x is passed to
f( ) (unix/intel platform).
2. The prototype is not provided, then a four-byte int, which contains the
value of x, is passed to f( ) (unix/intel platform).
Yes, but only if you are assuming that a char variable is stored in a
single byte (define byte?) and the sizeof(int) is 4.
Most likely true in a "unix/intel platform", but not necessarily so.
( I am currently working on code running on a TI 2812 DSP. There is
no byte (octet) addressing, both char and int types are 16 bits )
3. If f( ) was a variadic function instead, a four-byte int, which contains
the value of x, is passed to f( ) unix/intel platform).


Not applicable to this f(). A variadic function must have at least
one "regular" parameter.

Roberto Waltman

[ Please reply to the group, ]
[ return address is invalid. ]
Nov 15 '05 #2
In article <6q************ *******@text.ne ws.blueyonder.c o.uk> "jimjim" <ne*****@blueyo nder.co.uk> writes:
....
3. If f( ) was a variadic function instead, a four-byte int, which contains
the value of x, is passed to f( ) unix/intel platform).


No, see my article in the other thread.
--
dik t. winter, cwi, kruislaan 413, 1098 sj amsterdam, nederland, +31205924131
home: bovenover 215, 1025 jn amsterdam, nederland; http://www.cwi.nl/~dik/
Nov 15 '05 #3
On Tue, 13 Sep 2005 16:49:06 GMT, "jimjim" <ne*****@blueyo nder.co.uk>
wrote in comp.lang.c:
Hi all,

Assume x is declared as char x, and a function f( ) with prototype void
f(char y).

Assume f( ) is invoked somewhere in the code and:

1. The f( )'s prototype is provided, then an one-byte copy of x is passed to
f( ) (unix/intel platform).
No, the value of 'x' is passed as a char, which is one byte by
definition in C (but may have more than 8-bits).
2. The prototype is not provided, then a four-byte int, which contains the
value of x, is passed to f( ) (unix/intel platform).
If you call a function without a prototype in scope, the number and
types of arguments that you call it with, after the default
promotions, must exactly match the number and types of arguments the
function is defined to accept. If this is not true, you produce
undefined behavior, so it doesn't matter here what happens because
it's game over, you lose.

So if you have a function that accepts a parameter of a float, plain,
signed, or unsigned char, or signed or unsigned short, you can't call
it without a prototype in scope. Well you can, but we don't talk
about it here, because neither we nor the C language know or care what
happens. It might wipe the UNIX OS off your box and install Windows
95, for all we care.
3. If f( ) was a variadic function instead, a four-byte int, which contains
the value of x, is passed to f( ) unix/intel platform).
If f() was a variadic function, there must be a prototype in scope
when you call it, or you're back to undefined behavior and we don't
care. If there is a variadic function, and the char is passed to one
of the arguments in the "..." portion, then the value of the char is
promoted to an int and the int is passed by value. That int is
sizeof(int) bytes, which is sometimes one byte but usually more.

But it is always sizeof(int) bytes, and that is true whatever the
platform is. If the platform is important to your question, you are
wrong to ask it here.
Right?


Well, no, not completely.

If you don't know what sizeof(int) is on your platform, compile and
run the following program:

#include <stdio.h>
int main(void)
{
printf("sizeof( int) is %d\n", (int)sizeof(int ));
return 0;
}

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.l earn.c-c++
http://www.contrib.andrew.cmu.edu/~a...FAQ-acllc.html
Nov 15 '05 #4
>> 1. The f( )'s prototype is provided, then an one-byte copy of x is passed
to
f( ) (unix/intel platform).
No, the value of 'x' is passed as a char, which is one byte by
definition in C (but may have more than 8-bits).

As x on a unix/intel platform is 8-bits *, I would say that x 's value is
essentially an 8-bit integer number, which depending on the interpretation
may be displayed as a character or an integer value. So, I would say that
this integer number is passed to the f( ) function.
(1) Is this right?
(2) You said that x is passed as a char. Can you exlain what does this
mean, please?

* Given that currently character sets may have different width, should I
assume that C standard's definition of a char as one byte (which in the
context of computer science by definition is 8-bits) is a histrorical left
over?
2. The prototype is not provided, then a four-byte int, which contains
the
value of x, is passed to f( ) (unix/intel platform).


If you call a function without a prototype in scope, the number and
types of arguments that you call it with, after the default
promotions, must exactly match the number and types of arguments the
function is defined to accept. If this is not true, you produce
undefined behavior, so it doesn't matter here what happens because
it's game over, you lose.

Ohh i see. So, because I havent provided a prototype in scope, char x is
promoted to an int. But because f( ) actually accepts a char, undefined
behavior is invoked. thx
So if you have a function that accepts a parameter of a float, plain,
signed, or unsigned char, or signed or unsigned short, you can't call
it without a prototype in scope.
I see. thx
3. If f( ) was a variadic function instead, a four-byte int, which
contains
the value of x, is passed to f( ) unix/intel platform).


If f() was a variadic function, there must be a prototype in scope
when you call it, or you're back to undefined behavior and we don't
care.

Is this because the compiler explicitly needs to know the types of arguments
before the "..."?
If there is a variadic function, and the char is passed to one
of the arguments in the "..." portion, then the value of the char is
promoted to an int and the int is passed by value. That int is
sizeof(int) bytes, which is sometimes one byte but usually more.

Ohh, thats why we use va_arg(ap, int) for an argument of type char.

Thx
Nov 15 '05 #5
"jimjim" <ne*****@blueyo nder.co.uk> wrote:
* Given that currently character sets may have different width, should I
assume that C standard's definition of a char as one byte (which in the
context of computer science by definition is 8-bits)
No. A char is of size one byte by definition, and I don't expect this
to be changed in any forthcoming C standard. Note, that the somewhat
common definition of a byte consisting of an octet of bits has no
bearing on the definition of byte in the C standard:

C99 3.6
byte
addressable unit of data storage large enough to hold any member of
the basic character set of the execution environment

C99 3.7.1
character
[...] bit representation that fits in a byte
5.2.4.2.1p1
The values given below shall be replaced by constant expressions
[...]. Their implementation-defined values shall be equal or greater
in magnitude (absolute value) to those shown, with the same sign.

- number of bits for smallest object that is not a bit-field (byte)
CHAR_BIT 8
[...]

In C a byte has to be /at least/ 8 bits wide, but that's about it.

I wouldn't be surprised, if this message had passed through several
systems with byte sizes > 8 bit until it reached its destination.
is a histrorical left over?


IIRC, among the first computers C was ever implemented on were
machines with a word size of 18 or 36 bits, so I'd say no. But I
leave it to the experts to give a definite answer.

Best regards
--
Irrwahn Grausewitz (ir*******@free net.de)
welcome to clc : http://www.ungerhu.com/jxh/clc.welcome.txt
clc faq-list : http://www.faqs.org/faqs/C-faq/faq/
clc frequent answers: http://benpfaff.org/writings/clc
Nov 15 '05 #6
jimjim wrote:
[...]

* Given that currently character sets may have different width, should I
assume that C standard's definition of a char as one byte (which in the
context of computer science by definition is 8-bits) is a histrorical left
over?

[...]
Few corrections (the ones I got clarified few days ago)

Byte is *not* 8 bits .. yes now most of the systems (all of those I know
of have 8 bits.. so it's become a defacto)

C's definition of byte need not be same as underlying systems definition
of byte.

Here are few guarantees of C:
1) char is 1 byte ( hence sizeof(char) is *always* 1)
2) for ex: signed char the range of -127(SCHAR_MIN) to (SCHAR_MAX) +127
(or a bigger range than this)
3) CHAR_BIT of \at least\ 8 bits
So on a system where system's byte size is greater or equals to 8 bits
it's straight fwd as all 3 conditions will be automatically satisfied
(CHAR_BIT, SCHAR_MIN, SCHAR_MAX etc.. will be different than what you
see if a 8-bit byte system)

On the systems where system's byte is lesser than 8 bits then C's byte
definition would represent more than one system-byte.
For ex. if the system-byte size is 7 bits then CHAR_BIT would be 14
(7*2) etc.

Nov 15 '05 #7
jimjim wrote:
[...]

* Given that currently character sets may have different width, should I
assume that C standard's definition of a char as one byte (which in the
context of computer science by definition is 8-bits) is a histrorical left
over?

[...]
Few corrections (the ones I got clarified few days ago)

Byte is *not* 8 bits .. yes now most of the systems (all of those I know
of have 8 bits.. so it's become a defacto)

C's definition of byte need not be same as underlying systems definition
of byte.

Here are few guarantees of C:
1) char is *always* 1 byte ( hence sizeof(char) is *always* 1)
2) for ex: signed char the range of -127(SCHAR_MIN) to (SCHAR_MAX) +127
(or a bigger range than this)
3) CHAR_BIT of \at least\ 8 bits
So on a system where system's byte size is greater or equals to 8 bits
it's straight fwd as all 3 conditions will be automatically satisfied
(CHAR_BIT, SCHAR_MIN, SCHAR_MAX etc.. will be different than what you
see if a 8-bit byte system)

On the systems where system's byte is lesser than 8 bits then C's byte
definition would represent more than one system-byte.
For ex. if the system-byte size is 7 bits then CHAR_BIT would be 14
(7*2) etc.
Nov 15 '05 #8
> Here are few guarantees of C:
1) char is *always* 1 byte ( hence sizeof(char) is *always* 1)


Hmm, suppose I would like to allocate 2 chars with malloc: malloc( 2 *
sizeof (char)).
If sizeof(char) is *always* 1, on a platform that a char occupies 2 bytes I
have allocated 2 * 1 = 2 bytes, rather than 2 * 2bytes = 4 bytes.

This is a side question regarding malloc( ), which recieves one argument of
type size_t.
sizeof ( ) returns size_t. What happens when I multiply a number with
size_t, as malloc( 2 * sizeof (char))? Is this the right way to invoke
malloc( )?

TIA
Nov 15 '05 #9
jimjim wrote:
Here are few guarantees of C:
1) char is *always* 1 byte ( hence sizeof(char) is *always* 1)

Hmm, suppose I would like to allocate 2 chars with malloc: malloc( 2 *
sizeof (char)).
If sizeof(char) is *always* 1, on a platform that a char occupies 2 bytes I


To reiterate:
Definition of byte may be different from C to it's underlying system.

char always occupy 1 C-byte, but internally it may be n-system bytes.

sizeof(char) in malloc is not required as it's guaranteed to be 1 by C.
(FAQ covers this)

So malloc(2) [or malloc(2* sizeof(char)), if you prefer] would always
allocate 2 C-bytes. Again... internally it may be 2n system-bytes, but
in C realm its immaterial.
have allocated 2 * 1 = 2 bytes, rather than 2 * 2bytes = 4 bytes. The bytes you are talking about C-byte.
This is a side question regarding malloc( ), which recieves one argument of
type size_t.
sizeof ( ) returns size_t. What happens when I multiply a number with
size_t, as malloc( 2 * sizeof (char))? Is this the right way to invoke
malloc( )?


Here's what C99 has to say about sizeof and size_t
6.5.3.4: sizeof operator
....
4. The value of the result is implementation-defined, and its type
(an unsigned integer type) is size_t, defined in <stddef.h> (and other
headers).

you can conclude about the type by the above info (and boundary
conditions ... if you were worried about them)
Nov 15 '05 #10

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

Similar topics

5
3001
by: Mantorok Redgormor | last post by:
Would the following determine if the bit pattern for floating point 0.0 and integer 0 are the same? #include <stdio.h> int main(void) <% if(0 == 0.0f) puts("Bit pattern is the same."); return 0;
4
10460
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 an unsigned integer, so I see a +1 if the bit is set. Microsoft VC++ thinks it's signed, so I see -1 if the bit is set. Ex. typedef enum {
4
1933
by: spibou | last post by:
On 6.3.1.1 of N1124 we read: If an int can represent all values of the original type, the value is converted to an int; otherwise, it is converted to an unsigned int. A few lines above that we read: The following may be used in an expres-
6
13816
by: sarathy | last post by:
Hi, What is integer promotion? How is it different from arithmetic conversion? Regards, Sarathy
232
13242
by: robert maas, see http://tinyurl.com/uh3t | last post by:
I'm working on examples of programming in several languages, all (except PHP) running under CGI so that I can show both the source files and the actually running of the examples online. The first set of examples, after decoding the HTML FORM contents, merely verifies the text within a field to make sure it is a valid representation of an integer, without any junk thrown in, i.e. it must satisfy the regular expression: ^ *?+ *$ If the...
9
2110
by: Fred | last post by:
I'm having terrible trouble trying to work out exactly which of the promotions one reads about in old books are still present in current C - and there seems to be a distinction between promotion for function arguments versus expressions? For example, consider the following code: short u; /* declare u as a short */ (void) (u=10); /* set u to 10 */
7
12266
by: Spoon | last post by:
Hello everyone, In my code, I use uint16_t (exactly 16-bit-wide unsigned integer type) and I need to print their value in base 10. http://www.opengroup.org/onlinepubs/009695399/basedefs/inttypes.h.html As far as I understand, the recommended method is: #include <inttypes.h>
2
1676
by: Sune | last post by:
Hi all, there are several situations where integer promotion is performed in C and I need to confirm my understanding of it. Let's contain the discussion to workstation/server CPUs of 32/64 bits, I'm afraid 8/16 bits CPUs may add details I'm not interested in. Hopefully someone here can help out: - As long as I use function prototypes there will be no argument integer promotions (unless they are variadic)
3
3229
by: =?ISO-8859-15?Q?Jean=2DFran=E7ois?= Lemaire | last post by:
Hello, I'm having a discussion with someone who sustains that function parameters, when they are smaller than an int (say short or char) are automatically promoted to int before being passed to the caller. Despite looking in the Standard (ISO/IEC 9899:TC2), &6.5.2.2 "Function calls", I'm still unsure, because this is rather technical reading. Can someone enlighten me?
0
8457
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8365
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8883
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8788
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8563
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
7390
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6203
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5675
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
2
2013
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.