how to generate expected value in C

Viewed 107

I am new to the C language and I'm trying to write a simple self banking console program. I want to make a function to generate an account number so I wrote the code below:

int first = 7856123490;
int accountNumber;

int generateAccNo ()
{
    printf("%d", first);
    first = first + 1;
    accountNumber = first;
    return (accountNumber);
}

The whole idea here is that when I call this function, it will get the value in variable called first and add 1 to it and replace the value of first and return that value as accountNumber.

As I expect the first time this function runs it should return 7856123491 this value and the second time it should return 7856123492. So how can I do that?

3 Answers

A more portable solution: use fixed-length type in the <inttypes.h>.

#include <inttypes.h>
#include <stdio.h>

int64_t first = 7856123490;
int64_t accountNumber;

int64_t generateAccNo() {
  first = first + 1;
  accountNumber = first;
  return (accountNumber);
}

#define TEST_CNT 10
int main(int argc, char **argv) {
  for (int64_t i = 0; i < TEST_CNT; ++i) {
    int64_t res = generateAccNo();
    printf("%" PRId64 " %016" PRIX64 "\n", res, (uint64_t) res);
  }

  return 0;
}

Simple approach to work with big number, it will work even if you have N digits bank account

void big_number_inc(char * const big_number, const int size) {
    int i = size - 1;

    big_number[i]++;
    while ((i >= 0) && (big_number[i] > ASCII_9)) {
        big_number[i--] = ASCII_0;
        if (i >= 0) big_number[i]++;
    }
}

Full ref. code:

#include <stdio.h>

#define ARRAY_SIZE(x)       (sizeof(x)/sizeof(x[0]))
#define ASCII_0             48      // '0' assci value
#define ASCII_9             57      // '9' assci value

// internal use (debug only)
static void inline big_number_print(const char *const big_number, int size) {
    for (int i = 0; i < size; i++)
        printf("%c ", big_number[i]);
    printf("\n");
}

void big_number_inc(char * const big_number, const int size) {
    int i = size - 1;

    big_number[i]++;
    while ((i >= 0) && (big_number[i] > ASCII_9)) {
        big_number[i--] = ASCII_0;
        if (i >= 0) big_number[i]++;
    }
}

int main(int argc, char *argv[]) {
    // note: if max length of number doesn't match initial string length - 1, 
    // fill remaining with '0' or revert and start working from offset 0
    char number[] = { '0', '0', '7', '8', '5', '6', '1', '2', '3', '4', '9', '0' };

    for (int i = 0; i < 20; i++) {
        big_number_inc(number, ARRAY_SIZE(number));

        // wanna see the result
        big_number_print(number, ARRAY_SIZE(number));
    }

    return 0;
}
long int first = 7856123490;
long int accountNumber;

int generateAccNo ()
{
    printf("%ld \n", first);
    first = first + 1;
    accountNumber = first;
    return (accountNumber);
}
  • int hase a very small size to take such a big number so we will add keyword long when declaring the int
  • To suppress the warning while using long int with %d, we'll use %ld for long int
Related