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!

Sounds like your using unsigned characters for one and signed characters for the other.

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?

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

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.

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...

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.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.