Deutsch   English   Français   Italiano  
<87frrg9jud.fsf@nosuchdomain.example.com>

View for Bookmarking (what is this?)
Look up another Usenet article

Path: ...!weretis.net!feeder8.news.weretis.net!eternal-september.org!feeder3.eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: Keith Thompson <Keith.S.Thompson+u@gmail.com>
Newsgroups: comp.lang.c
Subject: Re: how cast works?
Date: Wed, 07 Aug 2024 13:08:42 -0700
Organization: None to speak of
Lines: 61
Message-ID: <87frrg9jud.fsf@nosuchdomain.example.com>
References: <v8vlo9$2oc1v$1@dont-email.me>
MIME-Version: 1.0
Content-Type: text/plain
Injection-Date: Wed, 07 Aug 2024 22:08:43 +0200 (CEST)
Injection-Info: dont-email.me; posting-host="fde1ffae9f6927ccc01afbae751eb89a";
	logging-data="3502686"; mail-complaints-to="abuse@eternal-september.org";	posting-account="U2FsdGVkX1/9+A6XTVOlAodxMtvKExb7"
User-Agent: Gnus/5.13 (Gnus v5.13)
Cancel-Lock: sha1:48pqBBkeU7OFaVqEGCxBM+Rc6/k=
	sha1:Q0m8L6aG/HVP2AM99+WWuDRECK0=
Bytes: 3351

Thiago Adams <thiago.adams@gmail.com> writes:
> How cast works?
> Does it changes the memory?
> For instance, from "unsigned int" to "signed char".
> Is it just like discarding bytes or something else?

It depends on the source and target types.

A cast is an explicit conversion.  It takes a value of some type and
yields a value of some (other) type -- or of the same type.  The
semantics of a conversion are not affected by whether it's done
explicitly or implicitly.

The rules depend on the source and target types, and are specified in
the standard, section 6.3 "Conversions".  You'll need to read the whole
thing.

> For instance, any 4 bytes type, cast to 2 bytes type is just the lower
> 2 bytes?
>
> [A][B][C][D]
> ->
> [A][B]

For some combinations of type, a conversion *might* be implemented that
way.  Converting a 4-byte float to a 2-byte short definitely does not.

Conversions between pointer types, and between a pointer and an integer
of the same size, are *typically* implemented by copying or
reinterpreting the bits, but that's not universal.  An implementation
could have different representations, even different sizes, for pointers
of different types.

> I also would like to understand better signed and unsigned.
> There is no such think as "signed" or "unsigned" register, right?
> How about floating point?

The C standard has no concept of registers (the "register" keyword
notwithstanding).  All conversions are defined in terms of values.
(Some CPUs have distinct floating-point registers, but that affect
the semantics of floating-point operations.)  An implementation will use
registers and operations on them to implement the semantics defined in
the standard (assuming the target CPU has registers).

> The motivation problem.
> I have a union, with unsigned int, unsigned char etc.(all types)
> I need to execute a cast in runtime (like script).
> The problem is that this causes an explosion of combinations that I am
> trying to avoid.

I don't think you're going to be able to avoid it.

Again, read section 6.3 "Conversions" of the standard.  It sounds like
you're going to have to explicitly cover all the cases in that section
(except for null pointer constants, which exist only in source code).

[...]

-- 
Keith Thompson (The_Other_Keith) Keith.S.Thompson+u@gmail.com
void Void(void) { Void(); } /* The recursive call of the void */