Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C ascii character list

Tags:

c

list

ascii

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;
}
like image 978
kyle k Avatar asked Dec 04 '25 03:12

kyle k


2 Answers

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.

like image 59
Eric Postpischil Avatar answered Dec 06 '25 17:12

Eric Postpischil


Use %c for the ASCII value, like so: printf("%d, %c\n", n, n); Then remove the atoi() line.

like image 44
Prof. Falken Avatar answered Dec 06 '25 17:12

Prof. Falken



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!