473,606 Members | 2,885 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Are C pitfalls the mistake of standard or that of implementations ?

Hi,
We see that C is a bit liberal towards non-standard syntax,
for e.g. when i tried
<printf("string 1", "string2 %d", v1);> in Dev-C++, it printed string1
and no error was raised, is this the mistake of standard or the
implementation (Dev-C++).
I was under the impression that the implementations could have been
better by implementing proper rules and adhering to the standard (ANSI
C) at the same time, for e.g., in the above case i was assuming that
the standard would have specified something like "an implementation
MUST provide printf("string where % is special char..", var1,..);". Now
a good implementor could have implemented this along with an error chek
to see the user code strictly adheres to that format and raise an error
if not (my e.g., above).
But, many implementations , even popular ones don't seem to do this.
What do u opine? People who have read the actual Standard can throw
some light on the specification in the standard.

Greenhorn.

Nov 14 '05 #1
41 1927

Greenhorn wrote:
Hi,
We see that C is a bit liberal towards non-standard syntax,
for e.g. when i tried
<printf("string 1", "string2 %d", v1);> in Dev-C++, it printed string1
and no error was raised, is this the mistake of standard or the
implementation (Dev-C++).
I was under the impression that the implementations could have been
better by implementing proper rules and adhering to the standard (ANSI C) at the same time, for e.g., in the above case i was assuming that
the standard would have specified something like "an implementation
MUST provide printf("string where % is special char..", var1,..);". Now a good implementor could have implemented this along with an error chek to see the user code strictly adheres to that format and raise an error if not (my e.g., above).
But, many implementations , even popular ones don't seem to do this.
What do u opine? People who have read the actual Standard can throw
some light on the specification in the standard.

Greenhorn.

If you invoke undefined behaviour, any implementation can do anything
with your program. In others words "if you don't follow the rules,
anything can happen, and this 'anything' can be different for any
implementation"

Nov 14 '05 #2
In article <11************ **********@f14g 2000cwb.googleg roups.com>,
Greenhorn <te************ @yahoo.com> wrote:
: We see that C is a bit liberal towards non-standard syntax,
:for e.g. when i tried
:<printf("strin g1", "string2 %d", v1);> in Dev-C++, it printed string1
:and no error was raised, is this the mistake of standard or the
:implementation (Dev-C++).

Neither. There is no requirement in the definition of printf()
that all of the value arguments need to be "consumed" by the printf().

:I was under the impression that the implementations could have been
:better by implementing proper rules and adhering to the standard (ANSI
:C) at the same time, for e.g., in the above case i was assuming that
:the standard would have specified something like "an implementation
:MUST provide printf("string where % is special char..", var1,..);".

I don't understand that last bit. There is nothing in the standard
that requires that the first argument to printf() have a % format
specifier.

:Now
:a good implementor could have implemented this along with an error chek
:to see the user code strictly adheres to that format and raise an error
:if not (my e.g., above).

It isn't an error. Some compilers will warn about it, as will 'lint'.
--
If a troll and a half can hook a reader and a half in a posting and a half,
how many readers can six trolls hook in six postings?
Nov 14 '05 #3
"Greenhorn" <te************ @yahoo.com> writes:
We see that C is a bit liberal towards non-standard syntax,
for e.g. when i tried
<printf("string 1", "string2 %d", v1);> in Dev-C++, it printed string1
and no error was raised, is this the mistake of standard or the
implementation (Dev-C++).


This is the correct behavior. It is usually a mistake, so better
C implementations will warn about it, if you turn on that
feature. GCC is an example of an implementation that will do so.
--
"When I have to rely on inadequacy, I prefer it to be my own."
--Richard Heathfield
Nov 14 '05 #4
Walter Roberson wrote:

If a troll and a half can hook a reader and a half in a posting and a half,
how many readers can six trolls hook in six postings?


I make it 24. I stand ready to be corrected.
Nov 14 '05 #5
Greenhorn wrote:
Hi,
We see that C is a bit liberal towards non-standard syntax,
for e.g. when i tried
<printf("string 1", "string2 %d", v1);> in Dev-C++, it printed string1
and no error was raised, is this the mistake of standard or the
implementation (Dev-C++).
I was under the impression that the implementations could have been
better by implementing proper rules and adhering to the standard (ANSI
C) at the same time, for e.g., in the above case i was assuming that
the standard would have specified something like "an implementation
MUST provide printf("string where % is special char..", var1,..);". Now
a good implementor could have implemented this along with an error chek
to see the user code strictly adheres to that format and raise an error
if not (my e.g., above).
But, many implementations , even popular ones don't seem to do this.
What do u opine? People who have read the actual Standard can throw
some light on the specification in the standard.


The issue is that the compiler can't necessarily see what you are
passing to printf, and it doesn't really care, either, as printf is a
library function and not an operator. Think of the following things:

1) the format string given to printf could also be a generated value, in
which case the compiler would not even be able to see what it is
2) printf is a library function, so someone could switch it out with
something that has different functionality, but the compiler wouldn't be
aware, and would generate errors even when there were none.
3) The call signature of printf is printf(char *format, ...). This
means that by its call signature, ANY number of parameters (except zero)
are LEGAL, even if the results aren't defined or useful. This is a
limitation of the C language, as this is a limitation of the way C
function prototypes work.

Jon
----
Learn to program using Linux assembly language
http://www.cafeshops.com/bartlettpublish.8640017
Nov 14 '05 #6
Jonathan Bartlett wrote:

3) The call signature of printf is printf(char *format, ...).


Minor nit: const char *format
Nov 14 '05 #7
I agree that any implementation can do anything, y not leverage on that
and try to raise warnings and errors when that is not expected in that
form. for e.g., i am guessing that ANSI standard doesn't say something
like "no errors are to be raised when printf("blah bhal.. is used".
which means an implementation can raise error (atleast a warning).
Ofcourse, some implementations use warnings as a proper tool.

Greenhorn

Kenneth Bull wrote:
Greenhorn wrote:
Hi,
We see that C is a bit liberal towards non-standard syntax,
for e.g. when i tried
<printf("string 1", "string2 %d", v1);> in Dev-C++, it printed string1 and no error was raised, is this the mistake of standard or the
implementation (Dev-C++).
I was under the impression that the implementations could have been
better by implementing proper rules and adhering to the standard

(ANSI
C) at the same time, for e.g., in the above case i was assuming that the standard would have specified something like "an implementation
MUST provide printf("string where % is special char..", var1,..);".

Now
a good implementor could have implemented this along with an error

chek
to see the user code strictly adheres to that format and raise an

error
if not (my e.g., above).
But, many implementations , even popular ones don't seem to do this.
What do u opine? People who have read the actual Standard can throw
some light on the specification in the standard.

Greenhorn.

If you invoke undefined behaviour, any implementation can do anything
with your program. In others words "if you don't follow the rules,
anything can happen, and this 'anything' can be different for any
implementation"


Nov 14 '05 #8
I agree that any implementation can do anything, y not leverage on that
and try to raise warnings and errors when that is not expected, say in
an expression. For e.g., i am guessing that ANSI standard doesn't say
something like "no errors are to be raised when printf("blah bhal.. is
used" which means an implementation can raise error (atleast a
warning). Ofcourse, some implementations use warnings as a proper tool.

Greenhorn

Kenneth Bull wrote:
Greenhorn wrote:
Hi,
We see that C is a bit liberal towards non-standard syntax,
for e.g. when i tried
<printf("string 1", "string2 %d", v1);> in Dev-C++, it printed string1 and no error was raised, is this the mistake of standard or the
implementation (Dev-C++).
I was under the impression that the implementations could have been
better by implementing proper rules and adhering to the standard

(ANSI
C) at the same time, for e.g., in the above case i was assuming that the standard would have specified something like "an implementation
MUST provide printf("string where % is special char..", var1,..);".

Now
a good implementor could have implemented this along with an error

chek
to see the user code strictly adheres to that format and raise an

error
if not (my e.g., above).
But, many implementations , even popular ones don't seem to do this.
What do u opine? People who have read the actual Standard can throw
some light on the specification in the standard.

Greenhorn.

If you invoke undefined behaviour, any implementation can do anything
with your program. In others words "if you don't follow the rules,
anything can happen, and this 'anything' can be different for any
implementation"


Nov 14 '05 #9
is it 6.

Nov 14 '05 #10

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

Similar topics

4
1685
by: Mantorok Redgormor | last post by:
Should I just avoid them? I have heard many bad things about them 1) if you set a signal handler, you can't really ignore the signal and continue with normal program execution, because after that undefined behavior is invoked. 2) if you have a signal handler set for say SIG_INT and you continue to reset the handler after you received the signal, to continue normal program execution, and the signal is received twice or something you end...
11
2016
by: bjrnove | last post by:
Hi. Me and a friend have a discussion about main. In my opinion the legal ways of writing main is: int main(void); int main(int,char**); My friend claims that you also have a standard version with enviroment variables like this:
85
4782
by: fermineutron | last post by:
Some compilers support __asm{ } statement which allows integration of C and raw assembly code. A while back I asked a question about such syntax and was told that __asm is not a part of a C standard. My question now is: Is there a chance that such statement will become a part of C standard in the future? In some cases using asm language is the best way to acomplish some small task, hence integration of C and asm would greatly enhence C,...
27
2808
by: Simon Biber | last post by:
The following Example 3 is given in the 1999 C standard for the function fscanf: I have tested several implementations and none of them get the last case right. In no case does fscanf return 0 indicating failure to match "100ergs of energy" with "%f". The actual behaviour varies. Some will match '100', leaving the 'e' unread:
4
1657
by: dustin | last post by:
I've been hacking away on this PEP for a while, and there has been some related discussion on python-dev that went into the PEP: http://mail.python.org/pipermail/python-dev/2007-February/070921.html http://mail.python.org/pipermail/python-dev/2007-February/071155.html http://mail.python.org/pipermail/python-dev/2007-February/071181.html I'd love to have feedback on this PEP: - from a user's perspective (would you want to write...
0
8031
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8456
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...
0
8443
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8107
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,...
1
5971
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
3989
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2452
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1565
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
1309
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.