fgetc().....Don't Quite Get This:

This is a discussion on fgetc().....Don't Quite Get This: within the C Programming forums, part of the General Programming Boards category; fgetc() returns an int, right? Then why is it I can assign c, a character, to the result of fgetc(fp)? ...

  1. #1
    People Love Me
    Join Date
    Jan 2003
    Posts
    412

    fgetc().....Don't Quite Get This:

    fgetc() returns an int, right? Then why is it I can assign c, a character, to the result of fgetc(fp)? ...It doesn't make any sense to me.

    Code:
    #include <stdio.h>
    
    int main(void){
        FILE *fp = fopen("myFile.txt","r");
        if(!fp){
                printf("Error opening file.");
        }
        char c;
        int n=0;
        while(c!=EOF){
                     c = fgetc(fp);
                      printf("%c",c);
                      if(c == '$'){
                           n++;
                      }
        }
        printf("Your file has %d$s.\n",n);
        getchar();
        return 0;
    }

  2. #2
    C/C++Newbie Antigloss's Avatar
    Join Date
    May 2005
    Posts
    212
    Well... int is converted to char implicitly. But char is not guaranteed to be capable of holding EOF. So using char to hold the return value of fgetc is wrong!

  3. #3
    cwr
    cwr is offline
    Registered Luser cwr's Avatar
    Join Date
    Jul 2005
    Location
    Sydney, Australia
    Posts
    868
    Use an int to get the return value of fgetc, check if it is EOF. Once you've established that it's not EOF, you can safely put it in a char.

  4. #4
    Registered User
    Join Date
    Apr 2005
    Posts
    67
    Char values can be converted to ascii values if you like. The compiler does the job for you. But when you call fgetc() the compiler expects char or an int representation of that char. For example:

    int c;
    c=fgetc(); -->and you press letter 'A' on your keyboar , the value of c becomes 65.See the ascii table.

    or if you cast any char to int you get it's number in the ascii table.
    Code:
      char c='A';
       printf("%d",(int)c);
    In the above example I wouldn't have to cast char to int sice we already have formated output using %d...

  5. #5
    cwr
    cwr is offline
    Registered Luser cwr's Avatar
    Join Date
    Jul 2005
    Location
    Sydney, Australia
    Posts
    868
    You don't need to cast to int:
    Code:
    char c='A';
    printf("%d", c);
    The above is fine, since c will be promoted to int when passed to printf.

  6. #6
    Registered User
    Join Date
    Mar 2005
    Posts
    135
    Quote Originally Posted by cwr
    You don't need to cast to int:
    Code:
    char c='A';
    printf("%d", c);
    The above is fine, since c will be promoted to int when passed to printf.
    Interpreted...

  7. #7
    cwr
    cwr is offline
    Registered Luser cwr's Avatar
    Join Date
    Jul 2005
    Location
    Sydney, Australia
    Posts
    868
    Quote Originally Posted by xeddiex
    Interpreted...
    What exactly do you mean?

  8. #8
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,046
    I think that means "point taken and understood."
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. manipulating fgetc while reading a file
    By agentsmith in forum C Programming
    Replies: 1
    Last Post: 04-10-2008, 01:52 PM
  2. character input using fgetc() code???
    By lesrhac03 in forum C Programming
    Replies: 3
    Last Post: 03-27-2008, 10:55 PM
  3. fgets not working after fgetc
    By 1978Corvette in forum C Programming
    Replies: 3
    Last Post: 01-22-2006, 05:33 PM
  4. About getc and fgetc. Please help.
    By Antigloss in forum C Programming
    Replies: 4
    Last Post: 09-28-2005, 04:00 AM
  5. fgetc
    By linuxdude in forum C Programming
    Replies: 3
    Last Post: 03-03-2004, 08:18 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21