What does '\n' have to do here for(i=0;i<'\n';i++) ?


I was going through some C code and this bit caught my eye and was wondering what is the '\n' bit about, as far as I know \n means new line, but i<'\n' ?

for(i=0;i<'\n';i++)

And just in case here is the code, it reads from a file and prints it:

case 1: {
FILE* fp;

fp = fopen("input.txt", "r");

for(i=0;i<'\n';i++) // This is the line
{
//read and save data from file
fscanf(fp,"%s%c%f%c%f%c%d%c%d%c%d%c%d%c\n",
my_tools[i].name,&ch,
&my_tools[i].ocost,&ch,
&my_tools[i].scost,&ch,
&my_tools[i].number,&ch,
&my_tools[i].month,&ch,
&my_tools[i].year,&ch,
&my_tools[i].numorder,&ch);

//print data
printf("%s %f %f %d %d %d %d\n",
my_tools[i].name,
my_tools[i].ocost,
my_tools[i].scost,
my_tools[i].number,
my_tools[i].month,
my_tools[i].year,
my_tools[i].numorder);
}

fflush(fp);
fclose(fp);};
break;
'\n' means Newline. It means while i does not equal a newline I guess. I don't think that is possible though? Because if you increment i it will never equal newline so the loop will never end. Also, I noticed you're using C instead of C++. It still is close enough for me to realize it's pointless to have that for loop. It would be more efficient to use a while loop.
Last edited on
for(i=0;i<'\n';i++) // This is the line

This loop will run for 10 times since value of \n is 0x0a

I don't know output as its based on file data..
I confirmed this.
for (X = 0; X < '\n'; X++)
This will run 10 times. How awkward.
'\n' has an ASCII value of 10; so it makes sense that it will run 10 times. I have no idea why that is being used in the for loop in the OP.

Weird.

Nick.
Topic archived. No new replies allowed.