Test Your C Skills - Errors

B

Ben Pfaff

Sharath said:
http://avsharath.googlepages.com/TestYourCSkillsErrata.htm
Please let me know if I've written anything wrong there, or anything
that should be added there.

I noticed the following right away. You claim:

There are only two types of scopes, file and block scope.
There is no function scope or prototype scope.

This is wrong. From C99, section 6.2.1p2:

For each different entity that an identifier designates, the
identifier is visible (i.e., can be used) only within a
region of program text called its scope. Different entities
designated by the same identifier either have different
scopes, or are in different name spaces. There are four
^^^^^^^^^^^^^^
kinds of scopes: function, file, block, and function
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
prototype. (A function prototype is a declaration of a
^^^^^^^^^^
function that declares the types of its parameters.)

Clearly, the book on which you are reporting has many errors, but
I'm not sure about your own qualifications, either. Do you have
a copy of the C standard?

I didn't read further in your webpage than this.
 
R

Richard Heathfield

Sharath said:
Below is a link to an errata list I made for the book "Test Your C
Skills" Written by Yashwant Kanetker.
http://avsharath.googlepages.com/TestYourCSkillsErrata.htm
Please let me know if I've written anything wrong there, or anything
that should be added there.

If you have quoted his Q1.0 correctly, why continue with the errata at
all? Clearly the guy doesn't know the language well enough to write an
authoritative book on it.
 
Q

quarkLore

Below is a link to an errata list I made for the book "Test Your C
Skills" Written by Yashwant Kanetker.http://avsharath.googlepages.com/TestYourCSkillsErrata.htm
Please let me know if I've written anything wrong there, or anything
that should be added there.
Nice attempt. Mention the edition you are referring (assuming there
might have been multiple ones :))
Regarding point #3 it is not valid in ANSI C also. Explain a little
more about undefined behavior. Your intended audience would be lay C
users corrupted after reading Yashwant Kanetkar. Undefined behavior
doesnt mean that it will give unpredictable behavior or your PC will
go berzerk but behavior will depend on the compiler and platform

Last question it is fine to describe non standard functions, no harm
in it
 
S

Sharath

I noticed the following right away. You claim:

There are only two types of scopes, file and block scope.
There is no function scope or prototype scope.

This is wrong. From C99, section 6.2.1p2:

For each different entity that an identifier designates, the
identifier is visible (i.e., can be used) only within a
region of program text called its scope. Different entities
designated by the same identifier either have different
scopes, or are in different name spaces. There are four
^^^^^^^^^^^^^^
kinds of scopes: function, file, block, and function
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
prototype. (A function prototype is a declaration of a
^^^^^^^^^^
function that declares the types of its parameters.)

Thanks for pointing that out. I will remove that from the list.
I had thought that function scope and block scope were the same.
Clearly, the book on which you are reporting has many errors, but
I'm not sure about your own qualifications, either. Do you have
a copy of the C standard?

I'm not a novice, and am not an expert either. I do have draft copies
of the C standard (including n1124).
-
Sharath
 
E

Eric Sosman

Ben Pfaff wrote On 04/19/07 16:17,:
http://avsharath.googlepages.com/TestYourCSkillsErrata.htm
Please let me know if I've written anything wrong there, or anything
that should be added there.


I noticed the following right away. You claim:

There are only two types of scopes, file and block scope.
There is no function scope or prototype scope.

This is wrong. From C99, section 6.2.1p2:

For each different entity that an identifier designates, the
identifier is visible (i.e., can be used) only within a
region of program text called its scope. Different entities
designated by the same identifier either have different
scopes, or are in different name spaces. There are four
^^^^^^^^^^^^^^
kinds of scopes: function, file, block, and function
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
prototype. (A function prototype is a declaration of a
^^^^^^^^^^
function that declares the types of its parameters.)
[...]

Sorta jumps right out, doesn't it? But since the
book's question asked about the "scope of a variable,"
there are only three relevant scopes: file, block,
and prototype. Variables never have function scope.
 
S

Sharath

Sharath said:


If you have quoted his Q1.0 correctly, why continue with the errata at
all? Clearly the guy doesn't know the language well enough to write an
authoritative book on it.

Yes, but there are whole lot of people who read this book, and believe
in whatever that is written there. I'm just trying to make them aware
of those errors through this list.
 
E

Eric Sosman

Sharath wrote On 04/19/07 16:10,:
Below is a link to an errata list I made for the book "Test Your C
Skills" Written by Yashwant Kanetker.
http://avsharath.googlepages.com/TestYourCSkillsErrata.htm
Please let me know if I've written anything wrong there, or anything
that should be added there.

Q8.8: Additionally, note that sizeof(NULL) might not be
the sizeof any pointer type at all: it could be the sizeof
any integer type.

Q12.11: In addition to the errors you mention, note that
the scanf() format specifiers do not match the other
arguments. (The author is somewhat optimistic in referring
to "the" error!) Also, your statement that "fflush() is
only meant for output streams" is not quite correct: it is
also well-defined for update streams if the most recent
operation on the stream was not input.
 
R

Richard Heathfield

Sharath said:
Yes, but there are whole lot of people who read this book, and believe
in whatever that is written there. I'm just trying to make them aware
of those errors through this list.

Well answered.
 
W

Walter Roberson

Undefined behavior
doesnt mean that it will give unpredictable behavior or your PC will
go berzerk but behavior will depend on the compiler and platform

I believe you are referring to unspecified behaviour, not
undefined behaviour. Undefined behaviour is a license for
anything to happen, including the possibility that the program
will appear to work perfectly the first 50 times, but will accidently
trigger an electric shock to your company president the first time
he or she runs the program.

This may sound rediculous, but it could be, for example, that
your president happens to be the only user with a particular
letter at a particular offset in their username, and that that
location is getting interpreted as an instruction, with
that particular character happening upon the only CPU
instruction that locks up the CPU, shorting out the databus
that then cascades to other hardware failures.
 
K

Kenneth Brody

Walter said:
I believe you are referring to unspecified behaviour, not
undefined behaviour. Undefined behaviour is a license for
anything to happen, including the possibility that the program
will appear to work perfectly the first 50 times, but will accidently
trigger an electric shock to your company president the first time
he or she runs the program.

This may sound rediculous, but it could be, for example, that
your president happens to be the only user with a particular
letter at a particular offset in their username, and that that
location is getting interpreted as an instruction, with
that particular character happening upon the only CPU
instruction that locks up the CPU, shorting out the databus
that then cascades to other hardware failures.

Sure, but what are the odds of that happeni;998&^%+_+075^%$
NO CARRIER

--
+-------------------------+--------------------+-----------------------+
| Kenneth J. Brody | www.hvcomputer.com | #include |
| kenbrody/at\spamcop.net | www.fptech.com | <std_disclaimer.h> |
+-------------------------+--------------------+-----------------------+
Don't e-mail me at: <mailto:[email protected]>
 
S

Sharath

Sharath wrote On 04/19/07 16:10,:


Q8.8: Additionally, note that sizeof(NULL) might not be
the sizeof any pointer type at all: it could be the sizeof
any integer type.

Q12.11: In addition to the errors you mention, note that
the scanf() format specifiers do not match the other
arguments. (The author is somewhat optimistic in referring
to "the" error!) Also, your statement that "fflush() is
only meant for output streams" is not quite correct: it is
also well-defined for update streams if the most recent
operation on the stream was not input.

Thank you Eric for pointing out the mistakes.
I have now made corrections to Q8.8, Q12.11 and Q1.28.
I have also added two more questions to the list, which
are Q11.11 and Q12.4. You can find the edited list at:
http://avsharath.googlepages.com/TestYourCSkillsErrata2.htm
Please let me know if you find anything else wrong in the list.
-
Sharath
 
R

R Pradeep Chandran

Below is a link to an errata list I made for the book "Test Your C
Skills" Written by Yashwant Kanetker.
http://avsharath.googlepages.com/TestYourCSkillsErrata.htm
Please let me know if I've written anything wrong there, or anything
that should be added there.

Thank You. I am glad that someone has the energy to do this.

I have a suggestion. Why don't you provide links to the relevant
sections of the C FAQ for each of the questions? The FAQ contains
detailed explanations for most of the topics.

Also, try to keep the URL constant.

Have a nice day,
Pradeep
 
A

Army1987

Ben Pfaff said:
--
int main(void){char
p[]="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz.\
\n",*q="kl BIcNBFr.NKEzjwCIxNJC";int i=sizeof p/2;char *strchr();int
putchar(\
);while(*q){i+=strchr(p,*q++)-p;if(i>=(int)sizeof p)i-=sizeof
p-1;putchar(p\
);}return 0;}


The output should end with a comma, not a period.
 
B

Ben Pfaff

Army1987 said:
Ben Pfaff said:
--
int main(void){char
p[]="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz.\
\n",*q="kl BIcNBFr.NKEzjwCIxNJC";int i=sizeof p/2;char *strchr();int
putchar(\
);while(*q){i+=strchr(p,*q++)-p;if(i>=(int)sizeof p)i-=sizeof
p-1;putchar(p\
);}return 0;}


The output should end with a comma, not a period.


Why?
 
J

Joe Estock

Sharath said:
Below is a link to an errata list I made for the book "Test Your C
Skills" Written by Yashwant Kanetker.
http://avsharath.googlepages.com/TestYourCSkillsErrata.htm
Please let me know if I've written anything wrong there, or anything
that should be added there.
-
Sharath

I noticed a couple of issues with Q12.11. I've pasted the original text
as-is from the site (with formatting changes and nothing more) for those
who may not have read it yet.

Q12.11

Point out the error in the following program.

#include"stdio.h"

main()
{
char ch;
int i;

scanf("%c",&i);
scanf("%d",&ch);
printf("%c%d",ch,i);
}

His answer:

You would not get a chance to supply a character for second scanf()
statement. Solution is to precede the second scanf() with the following
statement:

fflush(stdin);

This would flush out the enter hit for the previous scanf() to be
flushed out from the input stream, i.e. keyboard.

Erratum:

1. fflush is only meant for output streams. Using it for input stream
causes undefined behaviour.

2. In the printf statement, %c is used for printing integer i, and
%d for character, this causes undefined behaviour.

No this is not [entirely] true. Printing a character as an integral type
is perfectly valid and defined in the C standard, however it may not
yield the same results intended (e.g., an integral type instead of an
ascii character). After reviewing your above explanation along with the
code again I see that you are referring to the scanf statement instead
of the printf statement, however I would not say that the results are
UB. They would most certainly not be the intended results, however I
think that it would be perfectly defined.
 
C

CBFalconer

Joe said:
.... snip ...

I noticed a couple of issues with Q12.11. I've pasted the original
text as-is from the site (with formatting changes and nothing more)
for those who may not have read it yet.

Q12.11

Point out the error in the following program.

#include"stdio.h"

main() {
char ch;
int i;

scanf("%c",&i);
scanf("%d",&ch);
printf("%c%d",ch,i);
}

First problem, failure to specify the return type of main. 2nd,
failure to check the return value of scanf. 3rd, failure to return
a value, indicating success/failure. 4th, (fairly minor) failure
to specify the arguments for main as void. In addition the types
in the scanf and printf strings are inconsistent, and no final \n
is emitted. A corrected source:

#include"stdio.h"

int main(void) {
char ch;
int i;

if ((1 != scanf("%d", &i)) || (1 != scan(%c, &ch)))
fputs("no value received\n", stderror);
else printf("%c %d\n", ch, i);
return 0;
}

scanf will absorb blanks between the entries, but not other
characters. Note the use of the || operator, so that the second
scanf is not executed if the first fails.
 
J

Joe Estock

CBFalconer said:
Joe Estock wrote:
... snip ...

First problem, failure to specify the return type of main. 2nd,
failure to check the return value of scanf. 3rd, failure to return
a value, indicating success/failure. 4th, (fairly minor) failure
to specify the arguments for main as void. In addition the types
in the scanf and printf strings are inconsistent, and no final \n
is emitted. A corrected source:

#include"stdio.h"

int main(void) {
char ch;
int i;

if ((1 != scanf("%d", &i)) || (1 != scan(%c, &ch)))
fputs("no value received\n", stderror);
else printf("%c %d\n", ch, i);
return 0;
}

scanf will absorb blanks between the entries, but not other
characters. Note the use of the || operator, so that the second
scanf is not executed if the first fails.

I was addressing the text in Q12.11 of the file referenced by the OP,
not the code itself. The code is not from the OP, rather from a book the
OP was commenting on in the referenced URL in my original reply
[http://avsharath.googlepages.com/TestYourCSkillsErrata.htm for those
who have not yet read or do not have access to my original post]. I felt
it redundant to point out the obvious issues with the example code as
the very same issues have been addressed here time and time again and,
subsequently, are addressed in the FAQ.
 
¬

¬a\\/b

I noticed a couple of issues with Q12.11. I've pasted the original text
as-is from the site (with formatting changes and nothing more) for those
who may not have read it yet.

Q12.11

Point out the error in the following program.

#include"stdio.h"

should not to be #include <stdio.h>
for standard stdio functions printf and scanf?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top