1

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;
} 
Martin G
  • 17,357
  • 9
  • 82
  • 98
Rohit Saluja
  • 1,517
  • 2
  • 17
  • 25

9 Answers9

18

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.

Adam D. Ruppe
  • 25,382
  • 4
  • 41
  • 60
  • 1
    Apologies for the Spelling mistake. But how come the loop is infinite...? as unsigned char has the range from 0 to 255 wouldn't the loop will terminate once it reaches 255...? – Rohit Saluja Nov 21 '14 at 05:11
  • 3
    An unsigned char is only one byte long, so it has a maximum value of 255. When you add one to it, the one gets carried over and the first byte resets to zero. Imagine for example if you only had a two digit number. You can easily show 10-99. But if you did 99+1, you now get 100. What the computer does here though is just keep the last two digits - 00 - so it rolls back to where it started. Bytes work similarly, except the "digits" range from 0-255 and roll back over at 255+1. – Adam D. Ruppe Nov 21 '14 at 05:13
  • its an unsigned char, cant go higher than 255 – James H Nov 21 '14 at 05:13
  • @RohitSaluja 255 < 256 – phuclv Nov 21 '14 at 07:39
  • "unsigned char cannot possibly go higher than 255." - that usually is true. – chux - Reinstate Monica Mar 27 '15 at 22:14
6

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;
}
Ryan
  • 14,392
  • 8
  • 62
  • 102
  • I love how C's `stdio` is so much easier to use (generally) than C++'s `iostream`, easier to internationalize too. The C++ folks really dropped the ball there! – Adam D. Ruppe Nov 21 '14 at 05:08
  • 2
    I absolutely agree. I prefer `stdio` for things like this. – Ryan Nov 21 '14 at 05:09
  • @AdamD.Ruppe you can still use [std::printf](http://en.cppreference.com/w/cpp/io/c/fprintf) in C++ – phuclv Nov 21 '14 at 07:39
4

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
}
Galik
  • 47,303
  • 4
  • 80
  • 117
3

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;
}
Himanshu
  • 4,327
  • 16
  • 31
  • 39
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.

phuclv
  • 37,963
  • 15
  • 156
  • 475
0

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;
}
Mehdi Hosseinzadeh
  • 1,160
  • 11
  • 25
Margaret
  • 1
  • 2
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 (int 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.

MRL
  • 389
  • 5
  • 22
-1
#include <stdio.h>
int main ()
{
    unsigned char a;
    for(a=0;a<=255;a++)
    {
        printf(" %c  %d  \n ",a,a);
    }
    getch();
    return 0;
}
David
  • 10,458
  • 1
  • 28
  • 40
Vikas Reddy
  • 45
  • 1
  • 2
  • 8
-1

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

Rain
  • 231
  • 3
  • 12