How to write a program to print the ASCII characters of the input given by user?
He needs to learn... I wouldn't give him that answer if I were you.
thyrgle
2010-08-12 01:41:14
@thyrgle - well, at least it is not a complete solution...
ysap
2010-08-12 01:42:50
@ysap: I still wouldn't give him that much of hint without seeing a good try.
thyrgle
2010-08-12 01:45:10
@thyrgle: We're only under the assumption it's homework at the moment, although it seems like a good assumption we cannot be sure.
John K
2010-08-12 01:51:20
A:
#include <stdio.h>
int main()
{
char c;
printf("Input: ");
scanf("%c", &c);
printf("\n%i\n", (int)c);
return 0;
}
Clark Gaebel
2010-08-12 01:41:27
Don't give him the full program! Can't you see this is his homework? >_<
thyrgle
2010-08-12 01:44:13
A downvote for answering the question? What's wrong with you people?
Clark Gaebel
2010-08-12 01:54:21
and if comments had tags I would have tagged that "smart ass remark"
Charlie Martin
2010-08-12 02:11:01
+2
A:
Try this:
#include <stdio.h>
int main(int argc, char *argv) {
return argc == 1
? main(28, "the input given by the user")
: putch(argv[0]) && --argc>1 && main(argc, ++argv);
}
Anybody who turns this in as homework should probably work on the line: "Would you like fries with that?"
Jerry Coffin
2010-08-12 02:18:03
Shouldn't `argv` be a `char**`? In that case, the call to `main()` should be `main(6, "the input given by the user")` and you'd have to loop through an array of null terminated strings...
Chinmay Kanchi
2010-08-12 02:24:17
When the system calls it, it passes the address of an array of pointers to char -- but for that case, we only pay attention to `argc`. The implementation is not allowed to supply a prototype for main, so when we invoke it, we can pass what we feel like. If you think that's a bad idea, well, take note of the final sentence (and whoever downvoted, next time I'll try to remember to include closed-captioning for the humor impaired).
Jerry Coffin
2010-08-12 03:00:42