So I have this code that is supposed to generate a random 4 digit code. It correctly prints out each generated number but when I try to print out the entire array, its empty. Or at least it doesnt print anything out...
#include <stdio.h>
#include <stdlib.h>
#define CODE_LENGTH 4
void generate_code(int code[])
{
int i;
for(i = 0; i < CODE_LENGTH; i++) {
int x = rand() % 10;
printf("\n%i", x);
code[i] = x;
}
printf("\nCode: %n", code);
}
int main(void)
{
int code[4];
generate_code(code);
return 0;
}
Output:
1
7
4
0
Code:
There are several problems with your code.
The first one is that even you defined the constant CODE_LENGTH
#define CODE_LENGTH 4
the array declared in main
uses the magic number 4
instead of the constant
int main(void)
{
int code[4];
//...
Moreover the array is not used in main. At least you should output the filled in the function generate_code
array in main. That is the function should do only one thing: to fill a passed to it array.
Also before using the function rand
you should call function srand
.
There is no such a conversion specifier that allows to output a whole array in one call of printf
except character arrays. To output an integer array you need to use a loop inside which each element of the array will be outputed in turn.
The conversion specifier n
is designed for entirely other task. As it is written in the C Standard
n The argument shall be a pointer to signed integer into which is written the number of characters written to the output stream so far by this call to fprintf. No argument is converted, but one is consumed. If the conversion specification includes any flags, a field width, or a precision, the behavior is undefined.
Your program can look the following way
#include <stdlib.h>
#include <stdio.h>
#include <time.h>
#define CODE_LENGTH 4
void generate_code( int code[], size_t n )
{
const int Base = 10;
srand( ( unsigned int )time( NULL ) );
for ( size_t i = 0; i < n; i++ )
{
code[i] = rand() % Base;
}
}
int main(void)
{
int code[CODE_LENGTH];
generate_code( code, CODE_LENGTH );
for ( size_t i = 0; i < CODE_LENGTH; i++ )
{
printf( "%d", code[i] );
}
putchar( '\n' );
}
Or you could use a character array to store the generated code. For example
#include <stdlib.h>
#include <stdio.h>
#include <time.h>
#define CODE_LENGTH 4
void generate_code( char code[], size_t n )
{
const int Base = 10;
srand( ( unsigned int )time( NULL ) );
for ( size_t i = 0; i < n; i++ )
{
code[i] = rand() % Base + '0';
}
}
int main(void)
{
char code[CODE_LENGTH];
generate_code( code, CODE_LENGTH );
printf( "%.*s\n", CODE_LENGTH, code );
}
Or you could declare the character array as an array that will contain a string. For example
#include <stdlib.h>
#include <stdio.h>
#include <time.h>
#define CODE_LENGTH 4
void generate_code( char code[], size_t n )
{
const int Base = 10;
srand( ( unsigned int )time( NULL ) );
for ( size_t i = 0; i < n; i++ )
{
code[i] = rand() % Base + '0';
}
}
int main(void)
{
char code[CODE_LENGTH + 1] = "";
generate_code( code, CODE_LENGTH );
printf( "%s\n", code );
}