Printing asciee characters on the screen

I want to ask why the loop is printing more then the iteration of for loop. When I run this code the output shows more characters above 255 ?
[grey]
void main()
{
clrscr();

char asci;
for(asci = 32; asci<=255; asci++)
{
cout << asci << "
";
}
getch();
[/grey]

Talha Nasir

Comments

  • : I want to ask why the loop is printing more then the iteration of for loop. When I run this code the output shows more characters above 255 ?
    : [grey]
    : void main()
    : {
    : clrscr();
    :
    : char asci;
    : for(asci = 32; asci<=255; asci++)
    : {
    : cout << asci << "
    ";
    : }
    : getch();
    : [/grey]
    :
    : Talha Nasir
    :
    :


    You are using char, so 255 + 1 = 0, it will overflow.

    0 <= 255 gives TRUE.

    Change the type to int.
  • : : I want to ask why the loop is printing more then the iteration of for loop. When I run this code the output shows more characters above 255 ?
    : : [grey]
    : : void main()
    : : {
    : : clrscr();
    : :
    : : char asci;
    : : for(asci = 32; asci<=255; asci++)
    : : {
    : : cout << asci << "
    ";
    : : }
    : : getch();
    : : [/grey]
    : :
    : : Talha Nasir
    : :
    : :
    :
    :
    : You are using char, so 255 + 1 = 0, it will overflow.
    :
    : 0 <= 255 gives TRUE.
    :
    : Change the type to int.

    Better said, it will wrap back to zero. You have an endless loop.


    Greets,
    Eric Goldstein
    http://www.gvh-maatwerk.nl


  • : : : I want to ask why the loop is printing more then the iteration of for loop. When I run this code the output shows more characters above 255 ?
    : : : [grey]
    : : : void main()
    : : : {
    : : : clrscr();
    : : :
    : : : char asci;
    : : : for(asci = 32; asci<=255; asci++)
    : : : {
    : : : cout << asci << "
    ";
    : : : }
    : : : getch();
    : : : [/grey]
    : : :
    : : : Talha Nasir
    : : :
    : : :
    : :
    : :
    : : You are using char, so 255 + 1 = 0, it will overflow.
    : :
    : : 0 <= 255 gives TRUE.
    : :
    : : Change the type to int.
    :
    : Better said, it will wrap back to zero. You have an endless loop.
    :

    Actually, char is signed by default. So what actually happens is
    that the loop runs up to 127 then the variable overflows and gets set
    to -128.

    And overflow [italic]is[/italic] the correct term - the overflow flag in the cpu core will be set.
  • : : : : I want to ask why the loop is printing more then the iteration of for loop. When I run this code the output shows more characters above 255 ?
    : : : : [grey]
    : : : : void main()
    : : : : {
    : : : : clrscr();
    : : : :
    : : : : char asci;
    : : : : for(asci = 32; asci<=255; asci++)
    : : : : {
    : : : : cout << asci << "
    ";
    : : : : }
    : : : : getch();
    : : : : [/grey]
    : : : :
    : : : : Talha Nasir
    : : : :
    : : : :
    : : :
    : : :
    : : : You are using char, so 255 + 1 = 0, it will overflow.
    : : :
    : : : 0 <= 255 gives TRUE.
    : : :
    : : : Change the type to int.
    : :
    : : Better said, it will wrap back to zero. You have an endless loop.
    : :
    :
    : Actually, char is signed by default. So what actually happens is
    : that the loop runs up to 127 then the variable overflows and gets set
    : to -128.
    :
    : And overflow [italic]is[/italic] the correct term - the overflow flag in the cpu core will be set.

    Didn't think about the unsigned thing. You're right.



    Greets,
    Eric Goldstein
    http://www.gvh-maatwerk.nl


Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

In this Discussion