Code Effect

  • Thread starter Daniel Antonson
  • Start date
D

Daniel Antonson

What is the effect of the following code?

char Ch;

Ch = '7';

printf("%d\n", Ch);
 
D

Daniel Antonson

The choices available are:

a. It will cause an error

b. It will print out the computer's internal code for the character '7'

c. It will print out the character '7'

d. It will print out the character whose internal code is 7
 
M

Mac

The choices available are:

a. It will cause an error

b. It will print out the computer's internal code for the character '7'

c. It will print out the character '7'

d. It will print out the character whose internal code is 7

Which one did you choose?

--Mac
 
S

sandeep

Answer will be 'b'
It will print out the computer's internal code for the character '7'
 
W

Walter Roberson

:Answer will be 'b'

What answer will be 'b'? You didn't quote any establishing context.

:It will print out the computer's internal code for the character '7'

Obviously you didn't try it.

$ gcc -std=c89 -pedantic -o seven seven.c
seven.c:3: error: conflicting types for `Ch'
seven.c:1: error: previous declaration of `Ch'
seven.c:3: error: ISO C forbids data definition with no type or storage class
seven.c:5: error: parse error before string constant
seven.c:5: warning: conflicting types for built-in function `printf'
seven.c:5: error: ISO C forbids data definition with no type or storage class

$ gcc -std=c99 -pedantic -o seven seven.c
seven.c:3: warning: type defaults to `int' in declaration of `Ch'
seven.c:3: error: conflicting types for `Ch'
seven.c:1: error: previous declaration of `Ch'
seven.c:3: error: ISO C forbids data definition with no type or storage class
seven.c:5: error: parse error before string constant
seven.c:5: warning: type defaults to `int' in declaration of `printf'
seven.c:5: warning: conflicting types for built-in function `printf'
seven.c:5: error: ISO C forbids data definition with no type or storage class

$ : SGI IRIX below

$ cc -o seven -fullwarn seven.c
cc-1077 cc: ERROR File = seven.c, Line = 3
The indicated declaration has no storage class or type specifier.

Ch = '7';
^

cc-1143 cc: ERROR File = seven.c, Line = 3
Declaration is incompatible with "char Ch" (declared at line 1).

Ch = '7';
^

cc-1079 cc: ERROR File = seven.c, Line = 5
A type specifier is expected.

printf("%d\n", Ch);
^

cc-1079 cc: ERROR File = seven.c, Line = 5
A type specifier is expected.

printf("%d\n", Ch);
^

cc-1077 cc: WARNING File = seven.c, Line = 5
The indicated declaration has no storage class or type specifier.

printf("%d\n", Ch);
^

4 errors detected in the compilation of "seven.c".
 
M

Michel Rouzic

Daniel said:
What is the effect of the following code?

char Ch;

Ch = '7';

printf("%d\n", Ch);

idk, seems obvious to me that it prints the ascii value of '7'. is it a
question to tell newbies in C from others?
 
S

sandeep

Walter said:
:Answer will be 'b'

What answer will be 'b'? You didn't quote any establishing context.

:It will print out the computer's internal code for the character '7'

Obviously you didn't try it.
What made u think that i didn,t tried that
I tried and i got "55" as the internal value of '7'.
Which is also the ASCII value for "7".
Now i got it correct and you might be having some problem with your
compiler or you missed something.
anyways be careful in future.
 
L

Lawrence Kirby

Correct, assuming appropriate wrapper code to create a valid program.
As it will print the ASCII value of '7' which is 55

Often true but wrong in general because C does not require that the
character set used by an implementation is ASCII.

Lawrence
 
J

John Bode

Michel said:
idk, seems obvious to me that it prints the ascii value of '7'. is it a
question to tell newbies in C from others?

Smells like homework, actually.
 
E

Emmanuel Delahaye

D

Dik T. Winter

>
> idk, seems obvious to me that it prints the ascii value of '7'. is it a
> question to tell newbies in C from others?

Why do you think that it prints the ASCII value? C does not require
ASCII coding.
 
W

Walter Roberson

sandeep said:
Walter said:
What made u think that i didn,t tried that
I tried and i got "55" as the internal value of '7'.
Which is also the ASCII value for "7".
Now i got it correct and you might be having some problem with your
compiler or you missed something.

Any compiler that compiles the program *as given* is broken.

Perhaps you put some kind of wrapper around the code, but the
question was not "What would the code do if it were repaired",
the question was what the code would do -- and the answer to
that is that the code *given* is not a valid compilable C program.

If you were able to compile and execute the code *exactly as given*,
with full ANSI/ISO compliance mode turned on, then please let us
know what compiler you were using -- so that we will know to avoid it.


We were asked to interpret some code and say what it did, not to
indicate what *some other code* that sort of resembles the original
might do.

If you feel that for the sake of such questions that it is acceptable
to toss in header inclusions and function wrappers and such, then
you should be answering for all other possible manipulations of
the code -- such as inserting Ch = Ch / (Ch - '7'); in the middle.
If you are going to presume the presence of additional statements,
then why not presume the presence of additional code that will trigger
nasal demons or World War VI or a rise in the price of pork bellies?

In my opinion, when asked what code does, one should answer the
question about the code presented, not about what code might be there.
 
M

Mark Babli

"If you were able to compile and execute the code *exactly as given*,
with full ANSI/ISO compliance mode turned on, then please let us
know what compiler you were using -- so that we will know to avoid it.
.."
Answer:
DevC++ 4.9
Project ==> Options==> ANSI (Check) ==> OK

#include <stdio.h>


int main(int argc, char *argv[])
{
char Ch;
Ch = Ch / (Ch - '7');
printf("%d\n", Ch);

return 0;
}
and the result was:
0

Borland C++ Builder 6 rejected it (Big-time).
 
K

Keith Thompson

Mark Babli said:
"If you were able to compile and execute the code *exactly as given*,
with full ANSI/ISO compliance mode turned on, then please let us
know what compiler you were using -- so that we will know to avoid it.
."
Answer:
DevC++ 4.9
Project ==> Options==> ANSI (Check) ==> OK

#include <stdio.h>


int main(int argc, char *argv[])
{
char Ch;
Ch = Ch / (Ch - '7');
printf("%d\n", Ch);

return 0;
}
and the result was:
0

Borland C++ Builder 6 rejected it (Big-time).

That wasn't the point being made. The code *exactly as given* in the
original article did not include the "#include <stdio.h>", the return
statement, or the declaration of main(). It was just a declaration,
an assignment statement, and a call to printf(). It was an isolated
code fragment, not a legal program.

We could argue about whether that point was one worth making, but
let's not.
 
S

sandeep

Walter said:
sandeep said:
Walter said:
Any compiler that compiles the program *as given* is broken.

Perhaps you put some kind of wrapper around the code, but the
question was not "What would the code do if it were repaired",
the question was what the code would do -- and the answer to
that is that the code *given* is not a valid compilable C program.

If you were able to compile and execute the code *exactly as given*,
with full ANSI/ISO compliance mode turned on, then please let us
know what compiler you were using -- so that we will know to avoid it.
Obviously the given code would not compile i agree but what i done is:

#include<stdio.h>
void main()
{
char ch;
ch = '7';
printf("%d\n",ch);
return 0;
}
 
S

sandeep

Walter said:
sandeep said:
Walter said:
Any compiler that compiles the program *as given* is broken.

Perhaps you put some kind of wrapper around the code, but the
question was not "What would the code do if it were repaired",
the question was what the code would do -- and the answer to
that is that the code *given* is not a valid compilable C program.

If you were able to compile and execute the code *exactly as given*,
with full ANSI/ISO compliance mode turned on, then please let us
know what compiler you were using -- so that we will know to avoid it.
Obviously the given code would not compile on any comipler because of
missing header and all & we are assumed to add that(Mere common sense).
and the code which i compiled was:
#include<stdio.h>
main()
{
char ch;
ch = '7';
printf("%d\n",ch);
return 0;
}
Now is it okay for you.
Now compile it and it will print "55".
 

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

Forum statistics

Threads
474,166
Messages
2,570,907
Members
47,446
Latest member
Pycoder

Latest Threads

Top