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

  • SEARCH
  • Home
  • 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 6644149
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T00:08:01+00:00 2026-05-26T00:08:01+00:00

I’m learning C and I have written a simple program (just a tanning). On

  • 0

I’m learning C and I have written a simple program (just a tanning). On input you pass two arguments (row and column) and output you get a Calc (or Excel) code for this cell.
For example:

Input: 3 1     Output: A3
Input: 1 27    Output: AA1

The code:

#include <stdio.h>

char kol[7] = "";
unsigned int passes=0, nr;

int powa(unsigned int lv)
{
  if(passes < nr)
  {
    if(kol[lv] == '\0')
    {
      kol[lv] = 'A';
      kol[lv+1] = '\0';
    } else
    {
      kol[lv]++;
      if(kol[lv] == 'Z'+1)
      {
        kol[lv] = 'A';
        powa(lv+1);
        return 0;
      }

    }
    passes++;
    if(lv != 0)
    {
      powa(lv-1);
    } else
    {
      powa(lv);
    }

  }
}

int main(void)
{
  unsigned int wier;
  int i, len=0;
  scanf("%u %u", &wier, &nr);
  powa(0);
  while(kol[len] != '\0')
  {
    len++;
  }

  for(i=len-1;i>=0;i--)
  {
    putchar(kol[i]);
  }
  printf("%u", wier);
  return 0; 
}

But if I pass in a larger value (such as 300000000) I get a segmentation fault error. Why?

  • 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-26T00:08:02+00:00Added an answer on May 26, 2026 at 12:08 am

    Are you experimenting with recursion? I don’t think I’d be using a recursive solution. You should probably not be using as many global variables as you are, either.

    Assuming recursion is crucial, then in outline, I think I’d expect to use a solution such as:

    char *powa(unsigned int code, char *buffer)
    {
        unsigned int div = code / 26;
        unsigned int rem = code % 26;
        if (div > 0)
            buffer = powa(div - 1, buffer);
        *buffer++ = rem + 'A';
        *buffer = '\0';
        return buffer;
    }
    
    int main(void)
    {
        char buffer[32];
        unsigned int col, row;
    
        printf("Enter column and row numbers: ");
        if (scanf("%u %u", &col, &row) == 2)
        {
            if (col == 0 || row == 0)
                fprintf(stderr, "Both row and column must be larger than zero"
                                " (row = %u, col = %u)\n", row, col);
            else
            {
                char *end = powa(col-1, buffer);
                snprintf(end, sizeof(buffer) - (end - buffer), "%u", row);
                printf("Col %u, Row %u, Cell %s\n", col, row, buffer);
            }
        }
        return 0;
    }
    

    Note that the revised powa() returns a pointer to the null at the end of the data it has formatted. Theoretically, I should check the return from snprintf() to ensure no buffer overflow. Since ...bogus... is not valid C, you can tell I’ve not compiled this, but I have now compiled this, and tested and corrected it (the correction being to replace the recursive call powa(div, buffer) with powa(div - 1, buffer), a change necessary because the calculation needs to deal with 0 versus 1 as the starting point for counting. The recursion scheme seems simpler to me (a single recursive call instead of three of them in your code).

    Enter column and row numbers: 13 27
    Col 13, Row 27, Cell M27
    
    Enter column and row numbers: 27 13
    Col 27, Row 13, Cell AA13
    
    Enter column and row numbers: 30000000 128
    Col 30000000, Row 128, Cell BMPVRD128
    
    Enter column and row numbers: 300000000 128
    Col 300000000, Row 128, Cell YFLRYN128
    

    Here is code to handle both scanning and formatting derived from the code above:

    /*
    ** Convert column and row number into Excel (Spreadsheet) alphanumeric reference
    ** 1,1     => A1
    ** 27,1    => AA1
    ** 37,21   => AK21
    ** 491,321 => RW321
    ** 3941,87 => EUO87
    ** From StackOverflow question 7651397 on 2011-10-04:
    ** http://stackoverflow.com/questions/7651397/calc-cell-convertor-in-c
    */
    
    #include <ctype.h>
    #include <stdio.h>
    #include <string.h>
    
    extern unsigned xl_row_decode(const char *code);
    extern char *xl_row_encode(unsigned row, char *buffer);
    
    static char *xl_encode(unsigned row, char *buffer)
    {
        unsigned div = row / 26;
        unsigned rem = row % 26;
        if (div > 0)
            buffer = xl_encode(div-1, buffer);
        *buffer++ = rem + 'A';
        *buffer = '\0';
        return buffer;
    }
    
    char *xl_row_encode(unsigned row, char *buffer)
    {
        return(xl_encode(row-1, buffer));
    }
    
    unsigned xl_row_decode(const char *code)
    {
        unsigned char c;
        unsigned r = 0;
        while ((c = *code++) != '\0')
        {
            if (!isalpha(c))
                break;
            c = toupper(c);
            r = r * 26 + c - 'A' + 1;
        }
        return r;
    }
    
    static const struct
    {
        unsigned col;
        unsigned row;
        char     cell[10];
    } tests[] =
    {
        {     1,     1, "A1"       },
        {    26,     2, "Z2"       },
        {    27,     3, "AA3"      },
        {    52,     4, "AZ4"      },
        {    53,     5, "BA5"      },
        {   676,     6, "YZ6"      },
        {   702,     7, "ZZ7"      },
        {   703,     8, "AAA8"     },
        {   728,     9, "AAZ9"     },
    };
    enum { NUM_TESTS = sizeof(tests) / sizeof(tests[0]) };
    
    int main(void)
    {
        char buffer[32];
        int pass = 0;
    
        for (int i = 0; i < NUM_TESTS; i++)
        {
            char *end = xl_row_encode(tests[i].col, buffer);
            snprintf(end, sizeof(buffer) - (end - buffer), "%u", tests[i].row);
            unsigned n = xl_row_decode(buffer);
            const char *pf = "FAIL";
    
            if (tests[i].col == n && strcmp(tests[i].cell, buffer) == 0)
            {
                pf = "PASS";
                pass++;
            }
            printf("%s: Col %3u, Row %3u, Cell (wanted: %-8s vs actual: %-8s) Col = %3u\n",
                   pf, tests[i].col, tests[i].row, tests[i].cell, buffer, n);
        }
        if (pass == NUM_TESTS)
            printf("== PASS == %d tests OK\n", pass);
        else
            printf("!! FAIL !! %d out of %d failed\n", (NUM_TESTS - pass), NUM_TESTS);
    
        return (pass == NUM_TESTS) ? 0 : 1;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have just tried to save a simple *.rtf file with some websites and
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have a jquery bug and I've been looking for hours now, I can't
this is what i have right now Drawing an RSS feed into the php,
Seemingly simple, but I cannot find anything relevant on the web. What is the
I have a French site that I want to parse, but am running into
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
I need to clean up various Word 'smart' characters in user input, including but

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.