473,773 Members | 2,398 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Is this program legal C89?

Is this program legal C89?

/* no headers included */
int main(void)
{
if (sizeof (exit(0), 0),
((void (* )(int))&exit)(
(puts((const char *)"hello, world"), 0)
),
0) {
}
}

The the first operand of the first comma serves to implicitly
declare exit() as returning an int, but it is never called as
such. Does that wrong implicit declaration cause UB by itself,
even if the function is eventually called with the right type?

--
Army1987 (Replace "NOSPAM" with "email")
"Never attribute to malice that which can be adequately explained
by stupidity." -- R. J. Hanlon (?)

Jul 24 '07 #1
2 1681
Army1987 wrote On 07/24/07 07:01,:
Is this program legal C89?
Legal. Repellent, but legal.
/* no headers included */
int main(void)
{
if (sizeof (exit(0), 0),
((void (* )(int))&exit)(
(puts((const char *)"hello, world"), 0)
),
0) {
}
}

The the first operand of the first comma serves to implicitly
declare exit() as returning an int, but it is never called as
such. Does that wrong implicit declaration cause UB by itself,
even if the function is eventually called with the right type?
The type of the function pointer used at the call
site matches the type of the called function: it's legal.

--
Er*********@sun .com
Jul 24 '07 #2
Army1987 wrote:
Is this program legal C89?

/* no headers included */
int main(void)
{
if (sizeof (exit(0), 0),
((void (* )(int))&exit)(
(puts((const char *)"hello, world"), 0)
),
0) {
}
}

The the first operand of the first comma serves to implicitly
declare exit() as returning an int, but it is never called as
such. Does that wrong implicit declaration cause UB by itself,
even if the function is eventually called with the right type?
The only relevant paragraph in a C89 draft I'm looking at is
3.1.2.6 Compatible type and composite type
"All declarations that refer to the same object or function shall have
compatible type; otherwise the behavior is undefined."
Considering the implementation of the standard library contains a definition
(which is a declaration) of exit(), and your program's implicit declaration
is not compatible with it, I believe the behaviour is undefined, but
whether 3.1.2.6 is meant to apply to standard library functions is not
entirely clear. C99 clears it up with an explicit prohibition against
incorrect declarations of standard library functions:
7.1.3p2:
"No other identifiers are reserved. If the program declares or defines an
identifier in a context in which it is reserved (other than as allowed by
7.1.4), or defines a reserved identifier as a macro name, the behavior is
undefined."
so if you make the implicit declarations explicit, it's still definitely
invalid C99, but I have been told this text is not present in C89.
Jul 24 '07 #3

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

Similar topics

72
6989
by: Wim Vanhoof | last post by:
Dear all, I would like to announce that the department of computer science of the University of Namur, Belgium, is seeking a post-doctoral researcher for a one-year fellowship in the area of (logic-based) program development, analysis and transformation.
55
16713
by: Dev | last post by:
Hello Folks, I had faced this objective in one of my aptitude exams, that "What could be the smallest "C" program? And, as we know, smallest program means, it should execute single statement, I wrote ";" (Semicolon), so, program output will display blank screen. But Friends, I am not sure, Does anyone has perfect solution?
66
5414
by: genestarwing | last post by:
QUESTION: Write a program that opens and read a text file and records how many times each word occurs in the file. Use a binary search tree modified to store both a word and the number of times it occurs. After the program has read the file, it should offer a menu with three choices. the first is to list all the words along with the number of occurences. The second is to let you enter a word, with the program reporting how many times the...
9
6903
by: ehabaziz2001 | last post by:
I am facing that error message with no idea WHY the reason ? "Abnormal program termination" E:\programs\c_lang\iti01\tc201\ch06\ownarr01o01 Enter a number : 25 More numbers (y/n)? y Enter a number : 30 More numbers (y/n)? n
63
15273
by: biyubi | last post by:
Hi, a year ago I won the 2005 Best Game categoryof the International Obfuscated C Code Contestwith a chess program. http://www.ioccc.org/whowon2005.html http://www.mailcom.com/ioccc/toledo/hint.htmBut this post is because I have discovered (asurprise for me) that it is also the worldsmallest chess program written in C language.It has a size of 3004 bytes, or 2261 bytes simplydeleting all the spacing that makes the knightfigure, cutting...
3
3653
by: haelly | last post by:
Write a program that prompts the user to enter three different integer values.If the values are not different, the program prints a message"equal values" and terminates(hint: use the return statement).If either of the values is negative,the progra prints"Negative input" and terminates.Otherwie, it prints the values that the user enters. After that, the program compares the first integer and the second ineger and prints either "The first integer...
1
3772
by: haelly | last post by:
write a program that prompts the user to enter three different positive integer values.If the values are not different, the program prints a message"equal value" and terminates(hint:use the return statement).If eithere of the value is negative, the program prints "negative input" and terminates.Otherwise it prints the value the user entered. After that,the program compares he first integer and the second integer and prints either, "the first...
16
3413
by: Knute Johnson | last post by:
I'm trying to write a C wrapper to run a Java program. I need to distribute a CD with the Java runtime, my application and a C startup program. I've put the C wrapper program, my java app and the runtime directories in the same directory. It will run under MS XP or maybe Vista. I'm using MS Visual C++ Express 2005 for a compiler and I tried using the _execl and _spawnl functions. These all give me assertion errors and file not found...
46
2153
by: Kenny O'Clock | last post by:
This came up in a job interview, what is the output of the program below? I tried to compile and run it myself, but my compiler (lcc-win32) aborts with this errors.... Warning test2.c: 3 old-style function definition for 'main' Warning test2.c: 3 missing prototype for 'main' Warning test2.c: 3 ' Error test2.c: 3 compiler error in d:\lcc\mc71\types.c--assertion failure at line 868 Error c:\test\test2.c 3 Compiler error (trap)....
0
10264
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...
1
10039
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
8937
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
7463
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
6717
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();...
0
5355
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5484
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3610
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2852
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.