lundi 15 février 2021

c random int types

i need to put some int into strings like here:

#include <stdio.h>
#include <stdlib.h>
int main(void){

time_t t;
srand((unsigned) time(&t));
int alea1= rand()%11;
int alea2= rand()%11;

int add=alea1+alea2;
char question[256]="add %d to %d", alea1, alea2;
printf(%s, question);
return 0;
}

I need to put this into a string because after that im using

if (cJSON_AddStringToObject(qr, "Question", question) == NULL){
  goto end;
}

but i get this:

  srand((unsigned) time(&t));
                   ^~~~
test.c:11:37: error: conflicting types for ‘alea1’
  char question[256]="add %d to %d", alea1, alea2;
                                     ^~~~~
test.c:7:6: note: previous definition of ‘alea1’ was here
  int alea1= rand()%11;
      ^~~~~
test.c:11:44: error: conflicting types for ‘alea2’
  char question[256]="add %d to %d", alea1, alea2;
                                            ^~~~~
test.c:8:6: note: previous definition of ‘alea2’ was here
  int alea2= rand()%11;
      ^~~~~
test.c:12:9: error: expected expression before ‘%’ token
  printf(%s, question);

I tried strcat but i need to convert int to char dunno how to do this. i tried var=fprint("str&var", ...); but it is wrong.

Thank you.




Aucun commentaire:

Enregistrer un commentaire