Search code examples
cfseek

How does the computer count?


#include<stdio.h>

int main()
{
    FILE* f;
    f=fopen("book.txt","w");
    char* sentence="0123456789";
    fprintf(f,"%s\n",sentence);
    fseek(f,0,SEEK_END);
    int a=ftell(f);
    printf("%d\n",a);
    fclose(f);
    return 0;
}

I have the code above which prints out 12 when I run it. why is it not 11 (0,1,2,3,4,5,6,7,8,9,\0) instead of 12?

EDITED: (0,1,2,3,4,5,6,7,8,9,\r\n)


Solution

  • On Windows systems, the newline is actually two characters: Carriage-return and the newline characters ("\r\n").

    So you have your ten characters from the string you write out, plus the two for the newline.