Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7064157
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T04:47:59+00:00 2026-05-28T04:47:59+00:00

This is the program to convert a number into different representations like octal,decimal,hexadecimal etc.

  • 0

This is the program to convert a number into different representations like octal,decimal,hexadecimal etc.

    #include<stdio.h>

    char *convert(unsigned int num, int base)
    {
    static char buff[33];
    char *ptr;
    ptr=&buff[sizeof(buff)-1];
    *ptr='\0';
    do
    {
    *--ptr="0123456789abcdef"[num%base];
    num/=base;
    }while(num!=0);
    return(ptr);
    }

    int main(){

    puts(convert(65,8));
    puts(convert(65,10));
    puts(convert(65,16));
    return 0;
    }

Output gives 101 ,65 and 41 ( i.e the number ’65’ represented in octal,decimal and hexadecimal notations respectively)

I pretty much understand whats going on but i have never come across anything like

*--ptr="0123456789abcdef"[num%base]

I understand its working but i dont understand how it is a valid notation. Someone please explain the 0123456789abcdef(literal character array) part here.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T04:47:59+00:00Added an answer on May 28, 2026 at 4:47 am

    It is a ghastly piece of work; whoever wrote it should be taken out round the back of the shed and beaten up until they promise never to write such code again outside of an entry for the IOCCC (International Obfuscated C Code Contest).

    The right-hand side of the expression

    *--ptr = "0123456789abcdef"[num%base];
    

    is exploiting the fact that a string literal is converted to a pointer. If you saw:

    const char digit[] = "0123456789ABCDEF";
    *--ptr = digit[num%base];
    

    you’d not be worried. The expression with the string literal is equivalent to that. (At least they had the grace not to write:

    *--ptr = (num % base)["0123456789ABCDEF"];
    

    That’s also equivalent by virtue of the relation:

    a[i] <==> i[a] <==> *(a + i) <==> *(i + a)
    

    where the double-headed arrows indicate equivalence.

    The use of *--ptr is extracting the digits backwards, from least to most significant. It works because the pointer is initialized to the end of the static buffer (and it is crucial that it is static since the return value of the function is a pointer to that buffer).

    However, the code is not very useful because you can’t save the value from a call and print it later if there’s been another call. You could not write:

    printf("%s = %s = %sn", convert(65,8), convert(65,10), convert(65,16));
    

    Or, more accurately, you can, but you will see the same value three times one of “101”, “164” or “140” in the first position, and either “01” or “64” or “40” for the other two positions, and it is not defined by the C standard which values you will see. The static buffer also prevents the code from being thread-safe.

    All in all, it is weird show-off code for teasing beginners with. It works, but that’s about all that can be said about it. It doesn’t check its base for validity, either, so convert(65, 18) likely leads to undefined behaviour. (Base 17 would, at worst, reference a '\0' that was not intended, leading to confusion.)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to to convert from a decimal number into a binary number. Have
There's this program, pdftotext, that can convert a pdf file to a text file.
I wrote this code I have these errors Cannot implicitly convert type x.Program.TreeNode' to
This program stores pairs in a map, counting the number of times a word
This program reads emails (really just a .txt file structured like an email) and
For this program #include <iostream> using std::cout; struct C { C() { cout <<
I'm writing a Bison/Flex program to convert LaTeX into MathML. At the moment, dealing
I have a sentence say This is my new program I want to convert
I plan to make a program like this: loop read first character read second
I'm working on a program that converts between number bases. For example Octal is

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.