0

Can somebody explain me how the following tochar function works? As an experiment I added the following to tochar:

cout<<'0' + value

When run, I got a result of:

51 50 49 52

My code is:

static int tochar(int value)
 {
    return '0' + value;//This is the part i don't understand
 }

int main()
{

    char c[20];

    int n = 4123;
    int count = 0; 
    int number = log10(n)+1;  //number of digits
    for (int i = number; i >= 1; i--)
    {
        c[i] = tochar(n % 10);
        n = n / 10;
        count++;

    }
    for (int i = 1; i <=count; i++)
        cout<<c[i];
    system("pause");
}
Michael Petch
  • 46,082
  • 8
  • 107
  • 198
andrew
  • 19
  • 1
  • And technically the function definition should be `static char tochar(int value)` instead. – owacoder Mar 30 '16 at 12:26
  • 2
    @Cheers and hth. - Alf How do you know the OP is not using `void main`? – NathanOliver Mar 30 '16 at 12:30
  • @NathanOliver: On the contrary, from his or her posting we know that the OP uses `void main`. It has never been valid C or C++, and AFAIK is presently only accepted by Visual C++ and compatible compilers. It would be really bad teaching to let it pass by in SO example code (except where it is the point of the code), so please do fix that wherever you see it. You might also admonish the OP about it. When you have the time. – Cheers and hth. - Alf Mar 30 '16 at 12:38
  • 2
    @Cheersandhth.-Alf I don't want to argue the point here but I did create a meta Q about it. If you would like to participate: http://meta.stackoverflow.com/questions/320037/should-we-change-void-main-to-int-main-in-the-ops-code – NathanOliver Mar 30 '16 at 13:16
  • I would have probably done a static_cast on the `int number = log10(n)+1; ` and written it as `int number = static_cast(log10(n)) + 1;` . This would have also dealt with a potential MSVC++ warning. – Michael Petch Mar 30 '16 at 21:26

2 Answers2

1

This function converts a single digit int to the character representing the digit. It assumes that 0-9 digits are consecutive in the char map, and works by computing the difference between the given argument and the '0' character.

Claudio
  • 10,614
  • 4
  • 31
  • 71
  • I understand that but i don't know how it does that. – andrew Mar 30 '16 at 12:27
  • Both C and C++ require that 0-9 digits are consecutive and in order, so this conversion always works. – Pete Becker Mar 30 '16 at 12:27
  • Yes, of course. I just tried to let him understand the (very basic) idea behind the code. – Claudio Mar 30 '16 at 12:27
  • @PeteBecker what do you mean C requires it? it's not just how ascii works? – MoonBun Mar 30 '16 at 12:29
  • 1
    @Vladp - it's require for **all** character encodings used by those languages, not just ASCII. – Pete Becker Mar 30 '16 at 12:30
  • @Vladp: For example, C++ supports EBCDIC because EBCDIC has the digits in order, and provides all the characters required for the subset of ASCII that is C++'s basic execution character set, even though EBCDIC doesn't have all the English letters in order (consecutive). – Cheers and hth. - Alf Mar 30 '16 at 12:34
  • @Cheersandhth.-Alf how does it support it? do you need special compile flag to work with EBCDIC ? or c++ doesnt know what format it is, and it's solely depends on the one that reads it ? – MoonBun Mar 30 '16 at 12:40
  • @Vladp: No, no special flag is required. Indeed Windows has some EBCDIC codepages. When I wrote "support" I meant that EBCDIC does not conflict with the requirements of C++, which are (1) a certain set of characters, which for graphical characters is most of the ASCII set except "$" and I think it was three more, and (2) that the digits are consecutive. – Cheers and hth. - Alf Mar 30 '16 at 12:44
0

You should change your function to

static char tochar(int value)
{
   return '0' + value;//This is the part i don't understand
}
LibertyPaul
  • 1,139
  • 8
  • 26
  • @NathanOliver: I upvoted it because it explains what the function returns, which in turns partially explains how it works. Re "you cannot have static free functions", that's incorrect. – Cheers and hth. - Alf Mar 30 '16 at 12:31
  • 1
    @Cheersandhth.-Alf Ah yes you can have non-member static functions. Still keeping the down vote though as it does not explain why the algorithm is doing and changing to return to `char` is not needed(although that is what it should be). – NathanOliver Mar 30 '16 at 12:40