I'm new to C and I have a question I think it's not that complicated. I'm trying to write a code that creates strings like : File1,File2,File3, etc.
Here is my code:

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <string.h>

int main(void){

  char string1[80]="File";
  char string2[80]="";
  int number = 10;
  int i;

  for (i=0;i<number;i++){

  sprintf (string2,"%d",i);
  strcat( string1, string2);
  printf("Characters: %s \n" ,  string1);
  char string1[80]="File";
  char string2[80]="";
  }
  return 0;

 }

But what I'm getting is File0,File01,File012,...

Could you please tell me how to solve this?

I have changed it to the following code and now it works!

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <string.h>

int main(void){

  char string1[80]="File";
  char string2[80]="";
  int number = 10;
  int i;

  for (i=0;i<number;i++){

  char string1[80]="File";
  sprintf (string2,"%d",i);
  strcat( string1, string2);
  printf("Characters: %s \n" ,  string1);
  // for(i=0;i<80;i++) {
  string1[0]='\0';
  string2[0]='\0';
  //}
  }
  return 0;

 }
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.