This is a C program that i am trying to make print a list of ASCII characters. I could make the program print a range of numbers, bit i cannot get it to print the ASCII value of each number in the list.
#include <stdio.h>
#define N 127
int main(void)
{
int n;
int c;
for (n=32; n<=N; n++) {
char c = atoi( n);
printf("%d", c);
}
return 0;
}
atoi converts ASCII to int. You are passing it n. n is not ASCII; it is int. Therefore, atoi(n) does not work.
After deleting that, what you want to do is print the ASCII character that n represents. You do this with:
printf("%c", n);
You might want to label each character with its number, like this:
for (n=32; n<=N; n++) {
printf("%d: %c\n", n, n);
}
Incidentally. this requires that your C implementation use ASCII for its execution character set (and for its “C locale”). Many do. However, this program will not be portable to an implementation that uses a different character set.
Use %c for the ASCII value, like so: printf("%d, %c\n", n, n);
Then remove the atoi() line.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With