how to generate expected value in C
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?
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;
}