469,160 Members | 1,679 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,160 developers. It's quick & easy.

Test Your C Skills - Errors

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/Tes...illsErrata.htm
Please let me know if I've written anything wrong there, or anything
that should be added there.
-
Sharath

Apr 19 '07 #1
24 8617
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/Tes...illsErrata.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.

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at the above domain, - www.
Apr 19 '07 #2
Sharath <av*******@gmail.comwrites:
http://avsharath.googlepages.com/Tes...illsErrata.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.
--
int main(void){char p[]="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuv wxyz.\
\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[i]\
);}return 0;}
Apr 19 '07 #3
On Apr 20, 1:10 am, Sharath <avshar...@gmail.comwrote:
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/Tes...illsErrata.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
-
Sharath

Apr 19 '07 #4
On Apr 20, 1:17 am, Ben Pfaff <b...@cs.stanford.eduwrote:
Sharath <avshar...@gmail.comwrites:
http://avsharath.googlepages.com/Tes...illsErrata.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.)
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

Apr 19 '07 #5
Ben Pfaff wrote On 04/19/07 16:17,:
Sharath <av*******@gmail.comwrites:

>>http://avsharath.googlepages.com/Tes...illsErrata.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.

--
Er*********@sun.com
Apr 19 '07 #6
On Apr 20, 1:20 am, Richard Heathfield <r...@see.sig.invalidwrote:
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/Tes...illsErrata.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.
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.
Apr 19 '07 #7
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/Tes...illsErrata.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.

--
Er*********@sun.com
Apr 19 '07 #8
Sharath said:
On Apr 20, 1:20 am, Richard Heathfield <r...@see.sig.invalidwrote:
>>
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.
Well answered.

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at the above domain, - www.
Apr 19 '07 #9
In article <11**********************@y80g2000hsf.googlegroups .com>,
quarkLore <ag*************@gmail.comwrote:
>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.

--
"It is important to remember that when it comes to law, computers
never make copies, only human beings make copies. Computers are given
commands, not permission. Only people can be given permission."
-- Brad Templeton
Apr 19 '07 #10
On Apr 20, 1:46 am, Eric Sosman <Eric.Sos...@sun.comwrote:
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 Kanetkar.
http://avsharath.googlepages.com/Tes...illsErrata.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.
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/Tes...llsErrata2.htm
Please let me know if you find anything else wrong in the list.
-
Sharath

Apr 20 '07 #11
Walter Roberson wrote:
>
In article <11**********************@y80g2000hsf.googlegroups .com>,
quarkLore <ag*************@gmail.comwrote:
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.
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:Th*************@gmail.com>

Apr 20 '07 #12
On 19 Apr 2007 13:10:11 -0700, Sharath <av*******@gmail.comwrote:
>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/Tes...illsErrata.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
--
All opinions are mine and do not represent the views or
policies of my employer.
R Pradeep Chandran rpc AT pobox DOT com
Apr 20 '07 #13

"Ben Pfaff" <bl*@cs.stanford.eduha scritto nel messaggio
news:87************@blp.benpfaff.org...
--
int main(void){char
p[]="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuv wxyz.\
\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[i]\
);}return 0;}
The output should end with a comma, not a period.

--
int p(char*s,int n){putchar(n[s]);return n&&p(s,n-016?n*5%23
:0);}int main(void){return p("\nJsa ukenethr ,cto haCr",1);}
Apr 20 '07 #14
"Army1987" <pl********@for.itwrites:
"Ben Pfaff" <bl*@cs.stanford.eduha scritto nel messaggio
news:87************@blp.benpfaff.org...
>--
int main(void){char
p[]="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuv wxyz.\
\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[i]\
);}return 0;}

The output should end with a comma, not a period.
Why?
--
"A lesson for us all: Even in trivia there are traps."
--Eric Sosman
Apr 20 '07 #15
"Ben Pfaff" <bl*@cs.stanford.eduha scritto nel messaggio
news:87************@blp.benpfaff.org...
"Army1987" <pl********@for.itwrites:
>"Ben Pfaff" <bl*@cs.stanford.eduha scritto nel messaggio
news:87************@blp.benpfaff.org...
>>--
int main(void){char
p[]="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuv wxyz.\
\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[i]\
);}return 0;}

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

Why?
http://en.wikipedia.org/wiki/JAPH
Apr 20 '07 #16
Sharath wrote:
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/Tes...illsErrata.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.
Apr 21 '07 #17
Joe Estock wrote:
>
.... 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.

--
<http://www.cs.auckland.ac.nz/~pgut001/pubs/vista_cost.txt>
<http://www.securityfocus.com/columnists/423>
<http://www.aaxnet.com/editor/edit043.html>
cbfalconer at maineline.net

--
Posted via a free Usenet account from http://www.teranews.com

Apr 21 '07 #18
CBFalconer wrote:
Joe Estock wrote:
... 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.
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/Tes...illsErrata.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.
Apr 21 '07 #19
On Sat, 21 Apr 2007 08:34:12 -0500, Joe Estock
<je*****@NOSPAMnutextonline.comwrote:
>Sharath wrote:
>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/Tes...illsErrata.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"
should not to be #include <stdio.h>
for standard stdio functions printf and scanf?

main()
{
char ch;
int i;

scanf("%c",&i);
scanf("%d",&ch);
printf("%c%d",ch,i);
}
Apr 21 '07 #20
Joe Estock wrote:
>
char ch;
int i;

scanf("%c",&i);
scanf("%d",&ch);
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.
Not defined at all.

scanf("%d",&ch) is going to treat (&ch)
as though it were the address of an int type object.
That means that scanf may write to more than one byte
when there is only one byte of memory reserved there.

--
pete
Apr 21 '07 #21
On Apr 21, 6:34 pm, Joe Estock <jest...@NOSPAMnutextonline.comwrote:
Sharath wrote:
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/Tes...illsErrata.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);
}
<snip>
Erratum:
<snip>
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.
Yes, I was referring to scanf statement. Thanks for pointing it out.
But it is UB for scanf statement, as pete said, it may write more
than one byte to the char object.
I have now made corrections, you can find link to the modified page is
below:
http://avsharath.googlepages.com/Tes...llsErrata2.htm
-
Sharath.

Apr 22 '07 #22
On Apr 20, 8:10 am, Sharath <avshar...@gmail.comwrote:
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/Tes...illsErrata.htm
Please let me know if I've written anything wrong there, or anything
that should be added there.
Something nobody has mentioned yet: 'erratum' is singular. When
you are presenting multiple errors the word is 'errata'.

Apr 22 '07 #23
"Army1987" <pl********@for.itwrote:
"Ben Pfaff" <bl*@cs.stanford.eduha scritto nel messaggio
news:87************@blp.benpfaff.org...
"Army1987" <pl********@for.itwrites:
"Ben Pfaff" <bl*@cs.stanford.eduha scritto nel messaggio
news:87************@blp.benpfaff.org...
--
int main(void){char
p[]="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuv wxyz.\
\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[i]\
);}return 0;}

The output should end with a comma, not a period.
Why?
http://en.wikipedia.org/wiki/JAPH
I'm sorry, but _Perl_ hackers are off-topic here. Ben is a _C_ hacker,
and JACH don't require the comma. That's because C hackers, unlike Perl
hackers, aren't psychotic where punctuation is concerned.

Richard
Apr 23 '07 #24
CBFalconer <cb********@yahoo.comwrote:
A corrected source:
^almost!
#include"stdio.h"
#include <stdio.h>

--
C. Benson Manica | I *should* know what I'm talking about - if I
cbmanica(at)gmail.com | don't, I need to know. Flames welcome.
Apr 23 '07 #25

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

reply views Thread by Remy Blank | last post: by
162 posts views Thread by techievasant | last post: by
12 posts views Thread by ambreen_ssuet | last post: by
40 posts views Thread by Allan M. Bruce | last post: by
176 posts views Thread by nw | last post: by
35 posts views Thread by globalrev | last post: by
reply views Thread by mam | last post: by
1 post views Thread by CARIGAR | last post: by
1 post views Thread by Mortomer39 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.