kyle k
kyle k

Reputation: 5522

C ascii character list

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;
}

Upvotes: 2

Views: 1313

Answers (8)

William Martens
William Martens

Reputation: 913

Final solution


I tried to make it so compact (although not obfuscated) as possible.


#include <stdio.h>
int main() {
  // for loop                                      //  outputs                |data type|
  // for(int i='a';i<='z';putchar(i),i++);          // a,b,c...x,y,z          |  char   |
  // for(int i='a';i<='z';printf("%c\n",i),i++);    // a,b,c...x,y,z          |  char   |
  // for(int i='A';i<='Z';putchar(i),i++);          // A,B,C...X,Y,Z          |  char   |
  for(int i='a';i<='z';printf("%d\n",i),i++);     // 97,98,99..120,121,122    |  ascii  |
  //for(int i='a';i<='z';printf("%x\n",i),i++);       // 61,62,63 ,78,79,7a   |  hex    |

 //// for(int i='a';i<'z';printf("%d\n",i),F(i < 5),i++);
// for(int i='a';i<='z'; printf("%c\n",i),i++);
  return 0;
}

Upvotes: 0

user1814023
user1814023

Reputation:

You should be using %c as format specifier instead of %d.

#include <stdio.h>
#include <conio.h>
#define N 127
int main(void) 
{   
    int n;  
    for (n=32; n<=N; n++)
        printf("%c", n);
    getch();
    return 0;
}

Upvotes: 1

Lake
Lake

Reputation: 4092

Replace

printf("%d", c) 

with

printf("%c", c)

Also, you don't need atoi.

Just the following is enough:

int main(void) 
{   
    int n;  

    for (n=32; n<=N; n++) {
        printf("%c", n);
    }
    return 0;
}

Upvotes: 1

deadeert
deadeert

Reputation: 41

Atoi convert ascii entry in int representation. The program is :

#include <stdio.h>
#define N 127

int main()
{
  int n;

  for (n=32; n<=N; n++) 
    printf("%c",n) ;

  return 0;
}

Upvotes: 1

Yu Hao
Yu Hao

Reputation: 122463

for (n=32; n<=N; n++) {
    printf("%c", n);
}

You can print n to characters directly using %c

Note that you defined two variable c, the inner one (char c)will shadow the outer one(int c), that's valid C, but usually bad practice.

Upvotes: 1

d6bels
d6bels

Reputation: 1482

Have a look at printf formats.

Indeed, %d is used to print signed decimal integers. You want to print the corresponding character, so the format you are looking for is %c.

So it gives :

printf("%d", c);

Upvotes: 1

Eric Postpischil
Eric Postpischil

Reputation: 223663

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.

Upvotes: 4

Prof. Falken
Prof. Falken

Reputation: 24917

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

Upvotes: 1

Related Questions