print 19 on screen

A

asit

#include <stdio.h>

int main()
{
char p='19';
-----------------
return 0;
}


can anyone tell me what is the missing statement by writing which 19
will be printed on screen ???
 
R

Richard Tobin

#include <stdio.h>

int main()
{
char p='19';
-----------------
return 0;
}


can anyone tell me what is the missing statement by writing which 19
will be printed on screen ???

You could try

printf("19\n");

Perhaps you meant the preceding line to be

char *p="19";

in which case

printf("%s\n", p);

would work.

-- Richard
 
A

asit

#include <stdio.h>

int main()
{
char p='19';
-----------------
return 0;

}

can anyone tell me what is the missing statement by writing which 19
will be printed on screen ???


plz note that here p is a character. i want to access whole '19' from p
 
R

Richard Tobin

asit said:
plz note that here p is a character. i want to access whole '19' from p

But '19' is not a character. Character constants containing more than
one character do something implementation-defined. Most likely it's
just the same as '9', in which case you're not going to be able to get
'19' out of it.

-- Richard
 
M

Mark Bluemel

asit said:
//write a statement to print 19 on screen
return 0;
}
You snipped the vital part of my posting.

What do you expect "char p='19';" to achieve?
 
M

Mark Bluemel

Richard said:
But '19' is not a character. Character constants containing more than
one character do something implementation-defined. Most likely it's
just the same as '9', in which case you're not going to be able to get
'19' out of it.

Alternatively, if you meant the numeric value 19, you could simply write

#include <stdio.h>
int main()
{
char p=19;
printf("%d\n",(int)p);
return 0;
}
 
M

Mark Bluemel

asit said:
i want to access 19 from p in GCC compiler ???

What you have coded states that p is a char, not a pointer to char,
not a string of characters, but a single char data item.

You then specify an initial value for it which is not a single
character. This is not wise...
 
K

Kenny McCormack

What you have coded states that p is a char, not a pointer to char,
not a string of characters, but a single char data item.

You then specify an initial value for it which is not a single
character. This is not wise...

(Leaving aside all the usual CLC bullshit - i.e., forget about the missing
stdio.h, blah, blah, blah), I think what the OP is actually fishing for
is something like this:

main() {
char p = '\x19';
printf("Value: %x\n",p);
}

Note that, AFAIK, there is no notation for specifying character
constants in decimal. So, to do it without the x's is a little tricky.
 
A

asit

(Leaving aside all the usual CLC bullshit - i.e., forget about the missing
stdio.h, blah, blah, blah), I think what the OP is actually fishing for
is something like this:

main() {
char p = '\x19';
printf("Value: %x\n",p);
}

Note that, AFAIK, there is no notation for specifying character
constants in decimal. So, to do it without the x's is a little tricky.



THANK YOU.....finally i got the answer
 
K

Keith Thompson

asit said:
THANK YOU.....finally i got the answer

You got an answer, but not to the question you were asking.

I took your original question to mean: take the posted program, and
replace the comment with some statement *making no other changes* so
that the final program prints "19". KM's "solution" changes the
previous line from
char p='19';
to
char p=\x19';
If you can make any change you like to the entire program, the
solution is trivial. A more sensible change would have been to
change the declaration to
char *p = "19";

(If you're going to be participating here, you should know that Kenny
McCormack is a troll; you'll be better off if you ignore everything he
writes. Don't take my word for it; take a look at his posting
history. I expect he'll post some stupid personal insult in response
to this.)

Some of us have been hesitant to give you a solution because the
problem looks like homework; we generally try not to do people's
homework for them, because the whole point is to learn something.

But since a valid answer has already been posted, I'll bite. The
original code was:

#include <stdio.h>
int main()
{
char p='19';
//write a statement to print 19 on screen
return 0;
}

Here's my solution:

#include <stdio.h>
int main()
{
char p='19';
printf("19\n");
return 0;
}

I'm assuming that the "char p='19';" declaration was deliberately
misleading; the original problem statement didn't say that the output
had to be derived from the value of p.

And in fact there's no portable way to do so. A character constant
with more than one character between the single-quote characters has
an implementation-defined value. Unless you either read the
documentation for your compiler or write a program to display the
value, you have no way of knowing what value will actually be stored
in p.

It rarely makes any sense to use such character constants in the first
place.
 
K

Kenny McCormack

Keith Thompson said:
(If you're going to be participating here, you should know that Kenny
McCormack is a troll; you'll be better off if you ignore everything he
writes. Don't take my word for it; take a look at his posting
history. I expect he'll post some stupid personal insult in response
to this.)

Give it up, Keithy. We were all playing a round of "Read the OP's mind"
and the fact is, I won. Just admit it like a man.

You have to look at these kinds of posts as puzzles, and leave all the
usual CLS BS at the door.
 
A

Antoninus Twink

Give it up, Keithy. We were all playing a round of "Read the OP's mind"
and the fact is, I won. Just admit it like a man.

It's the sort of petulance you expect from CLC - he couldn't bring
himself to say, even through gritted teeth, that Kenny's practical
common sense had actually helped the OP, while the regulars' pedantic
literalism had failed to help the OP... And then this silly troll
name-calling (where troll = someone whose views differ from one's own)
replaces sensible discussion.

I think Godwin's Law applies in this group, with s/Nazi/troll/.
 
K

Kenny McCormack

It's the sort of petulance you expect from CLC - he couldn't bring
himself to say, even through gritted teeth, that Kenny's practical
common sense had actually helped the OP, while the regulars' pedantic
literalism had failed to help the OP...

Quite so. (channelling he who cannot be named)
And then this silly troll name-calling (where troll = someone whose
views differ from one's own) replaces sensible discussion.
Yes.

I think Godwin's Law applies in this group, with s/Nazi/troll/.

Excellent point. And in the classical sense of Godwin's law (the
non-pejorative sense - in which the probability approaches 1 over time).
 
M

Mabden

asit said:
#include <stdio.h>

int main()
{
char p='19';
-----------------
return 0;
}


can anyone tell me what is the missing statement by writing which 19
will be printed on screen ???

int main()
{
char p[3]="19";
printf ("%s\n", p);
return 0;
}
 
W

WANG Cong

On Sat, 15 Mar 2008 16:19:32 -0700,Mabden wrote:
asit said:
#include <stdio.h>

int main()
{
char p='19';
-----------------
return 0;
}


can anyone tell me what is the missing statement by writing which 19
will be printed on screen ???

int main()
{
char p[3]="19";
printf ("%s\n", p);
return 0;
}

puts("19");

Isn't this one simpler?
 
K

Keith Thompson

WANG Cong said:
asit said:
#include <stdio.h>

int main()
{
char p='19';
-----------------
return 0;
}


can anyone tell me what is the missing statement by writing which 19
will be printed on screen ???

int main()
{
char p[3]="19";
printf ("%s\n", p);
return 0;
}

puts("19");

Isn't this one simpler?

This and other solutions were discussed at some length when the
original question was posted 2 months ago. I'm not sure why Mabden
has decided to resurrect the old thread.
 

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,054
Latest member
TrimKetoBoost

Latest Threads

Top