Path: ...!news.mixmin.net!eternal-september.org!feeder3.eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail From: Keith Thompson Newsgroups: comp.lang.c Subject: Re: "undefined behavior"? Date: Thu, 13 Jun 2024 14:47:44 -0700 Organization: None to speak of Lines: 32 Message-ID: <87plsk5xbz.fsf@nosuchdomain.example.com> References: <666a095a$0$952$882e4bbb@reader.netnews.com> <8t3k6j5ikf5mvimvksv2t91gbt11ljdfgb@4ax.com> <666a18de$0$958$882e4bbb@reader.netnews.com> <87y1796bfn.fsf@nosuchdomain.example.com> <666a2a30$0$952$882e4bbb@reader.netnews.com> <87tthx65qu.fsf@nosuchdomain.example.com> MIME-Version: 1.0 Content-Type: text/plain Injection-Date: Thu, 13 Jun 2024 23:47:45 +0200 (CEST) Injection-Info: dont-email.me; posting-host="32c0f168ad8ff54df06981ba253a92e6"; logging-data="2600419"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1/sVx0PdBlxl3Olw/PWx6Tn" User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/27.2 (gnu/linux) Cancel-Lock: sha1:hpAJdjBvaISi1yW4a6Gu8ulC0x4= sha1:BBguGmCgXSySnqCIMika/72EXiY= Bytes: 2593 Malcolm McLean writes: > On 13/06/2024 01:33, Keith Thompson wrote: >> printf is a variadic function, so the types of the arguments after >> the format string are not specified in its declaration. The printf >> function has to *assume* that arguments have the types specified >> by the format string. This: >> printf("%d\n", foo); >> (probably) has undefined behavior if foo is of type size_t. >> > And isn't that a nightmare? Not at all. Compilers commonly diagnose mismatches when the format string is a string literal, as it most commonly is. The format specifier for size_t is "%zu", since C99. >> There is no implicit conversion to the expected type. Note that >> the format string doesn't have to be a string literal, so it's >> not always even possible for the compiler to check the types. >> Variadic functions give you a lot of flexibility at the cost of >> making some type errors difficult to detect. >> (I wrote "probably" because size_t *might* be a typedef for unsigned >> int, and there are special rules about arguments of corresponding >> signed and unsigned types.) > > We just can't have size_t variables swilling around in prgrams for > these reasons. We can and do. -- Keith Thompson (The_Other_Keith) Keith.S.Thompson+u@gmail.com void Void(void) { Void(); } /* The recursive call of the void */