简体   繁体   中英

Printing all the ASCII Values in C/C++

Hello Everyone its been Long that I am not in touch with the C/C++ Language and was just revising the concepts again and I came across this question which asked to write a program to display all the ASCII characters and I wrote the following good, but it is not giving expected result. Can anyone please tell what is the Problem with this code.

#include<iostrem.h>
int main()
{
    unsigned char a;
    for(a = 0; a < 256; ++a)
    {
        cout << a << " ";
    }
    return 0;
} 

a is always less than 256, since an unsigned char cannot possibly go higher than 255. You've written an infinite loop.

Your include also has a mispelling and extra .h and you didn't use the std namespace on cout .

edit: Finally, technically, ASCII only counts the first 128 characters, everything beyond that is the domain of various extended character sets.

If you can use <stdio.h> , then it is easier.

#include <stdio.h>

int main()
{
    for(int i = 0; i <= 255; i++) {
      fprintf(stdout, "[%d]: %c\n", i, i);
    }

  return 0;
}

The other answers have this well covered. I just thought I would throw in that it might be good to check if the characters are printable before printing them:

#include <cctype>
#include <iostream>

int main()
{
    for(int a = 0; a < 256; ++a) // use int (big enough for 256)
        if(std::isprint(a)) // check if printable
            std::cout << char(a) << " "; // print it as a char
}

Try this code:

=>C++

#include<iostream>
int main ()
{
    int a;
    for(a=0;a<256;++a)
    {
        cout<<(char)a<<" ";
    }
    return 0;
} 

=>C

#include<stdio.h>
int main ()
{
    int a;
    for(a=0;a<256;++a)
    {
        printf("%c " a);
    }
    return 0;
}

There are a lot of problems with your code.

First there is no iostrem.h . After correcting this to iostream.h g++ will give a fatal error: iostream.h: No such file or directory because the standard libraries must not be included with .h

Changing it to #include <iostream> results in the error: 'cout' was not declared in this scope . You need std::cout << a to make it compile successfully.

But even after resolving all the above problems, an important thing was output by gcc with -Wall -Wextra -pedantic option

warning: comparison is always true due to limited range of data type

That's because 256 is outside unsigned char 's typical range. It only works if char has more than 8 bits, which is not the case of your platform. You should always enable all compiler warnings. That'll help you identify a lot of problems without the need to ask here.

Nevertheless, don't use types less than int for temporary values unless very necessary because they all will be promoted to int in expressions anyway.

it works.

#include <iostream>
using namespace std;

int main() {
    char a;
    int i;

    for (int i=0; i<256; i++){
        a=i;
        cout << i << " " << a << " " <<endl;
    }
    return 0;
}

This is obviously a very old question, but if anyone wanted to actually print the ASCII code along with its corresponding numbers:

#include <iostream>
int main()
{  

char a; 

for (a=0; a<=127; a++)
{
    std::cout<<a<<" ";
    std::cout<<int(a)<<" "<<std::endl;
}

return 0;

}

NB/to the reader:

code 0-31: unprintable chars; used for formatting & control printers

code 32-127: printable chars; represents letters, numbers & punctuation.

#include <stdio.h>
int main ()
{
    unsigned char a;
    for(a=0;a<=255;a++)
    {
        printf(" %c  %d  \n ",a,a);
    }
    getch();
    return 0;
}

What result are you getting? I think you need to output

Cout<<(chat)a;

Otherwise it would only return the integer number that it assigned

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM