Test Your C Skills - Errors

P

pete

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.
 
S

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);
}
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/TestYourCSkillsErrata2.htm
-
Sharath.
 
R

Richard Bos

Army1987 said:
Ben Pfaff said:
Army1987 said:
"Ben Pfaff" <[email protected]> ha scritto nel messaggio
--
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?

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
 

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
474,432
Messages
2,571,680
Members
48,796
Latest member
Greg L.

Latest Threads

Top