Path: csiph.com!eternal-september.org!feeder.eternal-september.org!nntp.eternal-september.org!.POSTED!not-for-mail From: wij Newsgroups: comp.lang.c Subject: Re: Safety of casting from 'long' to 'int' Date: Sat, 02 May 2026 12:32:11 +0800 Organization: A noiseless patient Spider Lines: 82 Message-ID: References: <10su8cn$am9i$1@dont-email.me> <10sv4v0$h9mn$1@dont-email.me> <84c1c180f4d5b96259a631bdb09b6054b4eb44d2.camel@gmail.com> <10svgfv$l2bu$1@dont-email.me> MIME-Version: 1.0 Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable Injection-Date: Sat, 02 May 2026 04:32:12 +0000 (UTC) Injection-Info: dont-email.me; logging-data="1963132"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX18GMIkf2Pyk3whEv+98Q3gQ"; posting-host="a88be652840902af0e716c90db02bbe8" User-Agent: Evolution 3.58.3 (3.58.3-1.fc43) Cancel-Lock: sha1:qFPZNrGkdmCpRvCdojUdsY6ohm0= In-Reply-To: <10svgfv$l2bu$1@dont-email.me> Xref: csiph.com comp.lang.c:398170 On Thu, 2026-04-30 at 14:04 +0200, David Brown wrote: > On 30/04/2026 13:35, wij wrote: > > On Thu, 2026-04-30 at 10:47 +0200, David Brown wrote: > > > On 30/04/2026 02:39, Kalevi Kolttonen wrote: > > > > Hello! > > > >=20 > > > > A simple question and yes, I know I could ask AI bots > > > > but the problem is that I cannot always trust their > > > > responses. > > > >=20 > > > > Is it always safe and not undefined behavior to do: > > > >=20 > > > > =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 int i; > > > > =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 long l; > > > > =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 i =3D (int)l; > > > >=20 > > > > as long as you have first veried that 'l' is within > > > > the range between INT_MIN and INT_MAX? Thanks. > > > >=20 > > > > br, > > > > KK > > >=20 > > > Others have given you the answer to your question.=C2=A0 Asking here = will get > > > you accurate answers (James, Keith and Tim are all experts at the > > > details of C), while AI may or may not be correct at this kind of thi= ng. > > > =C2=A0=C2=A0 There are a lot of people who misunderstand the subtleti= es of C, > > > especially when you are asking what the standards actually say rather > > > than just what is likely to work on a particular compiler.=C2=A0 When= people > > > write the kind of nonsense wij does, AI "learns" from them too, and c= an > > > regurgitate the same mistakes. > >=20 > > What mistake? Can compiler generate code other than "i=3D(int)l;" appea= rs to be? > >=20 > > I did not write C program for >30 years, something might be missed. So,= what > > would this expert say? > >=20 >=20 > Experts would /not/ say "C is portable assembly".=C2=A0 When asked a ques= tion=20 > about the guaranteed meaning of something in C (rather than the likely= =20 > behaviour of some particular compiler), they would /not/ say "don't=20 > over-interpret what the C standard says".=C2=A0 They would /not/ be askin= g=20 > some AI for answers here. The problem with 'legal terms' is it is abstract (I think the main audience of the standard is compiler implementer). Using it too much to explain C wo= uld=20 make the analysis like what 'TV expert' provides, lots of professional-look= ing terms but no real substance inside. E.g. int F() { int a,b; return a+b; // UB? } 1. F is standard conforming as long as F does not mean to portably return the value of the sum of a and b.=20 2. If the compiler translates the last line to "return a*b;", I am afraid s= uch translation is not rejected as non-standardare conforming, i.e. "a*b" is still likely standard conforming from the common rule I read. > You were correct that the behaviour of the conversion is defined - the= =20 > rest of your answer was directly counter-productive. In the early 2000, comp.lang.c++ is full of superstition (being abstract) a= s if c++ is an AI language. I have the feel that C is following c++'s (failed) i= deal. I still think 'portable assembly' is good (to understand C).