Skip to content
Learn Netverks
0

Should I write long signed int or just long if I want a signed int of 32 bits?

asked 8 hours ago by @qa-rdxejco32pljjm69tj7j 0 rep · 145 views

c c23

So I just wondered if it would be better to be explicit with types e.g. using long signed int for a signed 32 bit int.
Or if it would be better to just not waste your time and just write long from a best practice perspective in C.

Can some compilers on some platforms misinterpret long as long float or long unsigned int?

Comments on this question (0)

Use comments to ask for clarification — answers go in the answer box below.

Log in to comment on this question.

7 answers

5

You may also consider using one of the fixed width integer types, to be explicit.

Reese Nguyen · 0 rep · 8 hours ago

5

If you want a 32-bit signed integer, you should write int32_t.

Taylor Singh · 0 rep · 8 hours ago

2

As you can see in the Arithmetic types reference:

  1. long by itself is always signed by default (same goes to long int).
    Explicitly adding signed is superfluous and IMHO will only confuse readers.
  2. long by itself always denotes an integral type.
    So no compiler will misinterpret it as a floating point type (unless you explicitly add float/double).

Blake Reed · 0 rep · 8 hours ago

2

long signed int for a signed 32 bit int

Why do you believe long signed int is going to be 32 bits?

Jamie Reed · 0 rep · 8 hours ago

2

I'm guessing they mean "to guarantee to get at least 32 bits", since a signed int can have as little as 16 bits.

One can use int_least32_t for that, or int32_t if one is ok assuming the environment has a 32-bit type.

Blake Morris · 0 rep · 8 hours ago

1

if I want a signed int of 32 bits? ... better to be explicit with types e.g. using long signed int for a signed 32 bit int.

Use int32_t from <stdint.h> if the object must be 32-bit, not wider, nor narrower.
Also use int32_t if it is part of an often used type.

This obliges a potential conversion from long, which may be wider than 32-bit and special macros when printing or scanning.


If this 32-bit integer is not heavily used in objects and may be allowed to be wider than 32-bit, long is OK. This simplifies scanning and printing.

Other candidates include int_least32_t and int_fast32_t, yet their preference is only in diminishing special cases.

Blake Singh · 0 rep · 8 hours ago

1

Reference is not the C spec. Note that signed int and int may differ with bit-fields.

Cameron Cole · 0 rep · 8 hours ago

Your answer