0

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?

2
Contributors
2
Replies
4
Views
6 Years
Discussion Span
Last Post by WaltP
0

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;

 }
This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.