tags:

views:

127

answers:

4

How to write a program to print the ASCII characters of the input given by user?

A: 

Homework?

Start with:

char a;
a = getchar();
printf("%c\n", a);
ysap
He needs to learn... I wouldn't give him that answer if I were you.
thyrgle
@thyrgle - well, at least it is not a complete solution...
ysap
@ysap: I still wouldn't give him that much of hint without seeing a good try.
thyrgle
@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
A: 
#include <stdio.h>

int main()
{
    char c;

    printf("Input: ");
    scanf("%c", &c);

    printf("\n%i\n", (int)c);

    return 0;
}
Clark Gaebel
Don't give him the full program! Can't you see this is his homework? >_<
thyrgle
Relax. This is readily available all over the Internet.
Dumb Guy
A downvote for answering the question? What's wrong with you people?
Clark Gaebel
@Clark Gaebel: What downvote?
thyrgle
A: 
od -c > characters.txt
Charlie Martin
Tagged as "c"..
Clark Gaebel
and if comments had tags I would have tagged that "smart ass remark"
Charlie Martin
+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
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
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