0

Hi,


When writing a little code for showing ASCII characters in C++ and C, the only difference is when I do it in C++ I print until char ( or integer ) reaches value of 127, and in C I print until it reaches 255.

Why it's like that?


Thanks!

3
Contributors
6
Replies
7
Views
5 Years
Discussion Span
Last Post by MrEARTHSHAcKER
0

I also noticed, regardless of what's final value ( 127 or 255 ), console outputs numbers from 0 to 255.

C++:

#include <iostream>

using namespace std;

int main(void){
    char a=0;
    for(a;a<127;++a)
    cout<<(int)a<<"\t"<<(char)a<<endl;
      
    system("PAUSE");
    return 0;    
    }

C:

#include <stdio.h>
#include <stdlib.h>

int main(int argc, char *argv[])
{
  int a=0;
  
  for(a;a<256;++a)
  printf("%d \t %c \n",a,a);
  system("PAUSE");	
  return 0;
}

How does it actually work?

0

I'm not sure what you mean by 'How does it actually work?'.

Well, I don't understand, why does it print 255 characters if I say 127.

0

Your examples are mixing character variables with integer variables and looping over different ranges. I'm not sure why you expect common behavior if you do not provide common code...

0

Your examples are mixing character variables with integer variables and looping over different ranges. I'm not sure why you expect common behavior if you do not provide common code...

Yeah, I accidentally mixed variable types.

Sorry for confusing you people. I'll remove the post.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.