Thread: getc and int data type

  1. #1
    Registered User
    Join Date
    Aug 2008

    getc and int data type

    I've trying to test things out to know how they work. We were asked to do an html tag counter program that well, lists the tags used and how many times they appear. So I was trying out fopen to open the file and print the contents first to see if they do work. I still haven't gone to the html-counter part, though. The program below is just to test and make fopen work.

    #include <stdio.h>
    #include <stdlib.h>
    int main(int argc, char **argv)
            int c_html;
            FILE *html_file;
            html_file  = fopen(argv[1], "r" );
                    printf("Please put an input file.\n"
                            "Use the syntax: ./a <filename>\n"
                            "The < and > are not include in the syntax.\n");
                            c_html = getc(html_file);
                            if(c_html == EOF)
                            printf("&#37;c", c_html);
                            /*printf("%d", argc);*/
    This works and gives the output I want, however, my question is that how come that if I change the data type of the variable c_html to char, I get the warning "comparison is always false due to limited range of data type", while it works with the int data type.

    The warning is for the line "if(c_html == EOF)".

    Isn't getc function supposed to give out char? I also used the "%c" when printing, which if I remember correctly, is used for printing character values?

    Sorry, if this question is n00bish, but it's been two years since I got a programming subject (and I don't have my resource book right now). Most of my memories of it are washed away.

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Farncombe, Surrey, England
    getc returns an integer. This is just because EOF is not a character - it is an integer value to distinguish it from the character values that are use up the entire range of char values.

    Note also that printf() arguments are automatically converted to int (or double if it's a floating point value), so %c will consume an integer sized object on the printf argument list, not a character size object.

    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #3
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    You might want to read the FAQ on Definition of EOF and how to use it effectively.

    Isn't getc function supposed to give out char?
    getc() returns an int, with the reason given in the FAQ I linked to.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    The edge of the known universe
    > html_file = fopen(argv[1], "r" );
    Check argc before you do this.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. undefined reference to `recursiveFunction'
    By chocolatecake in forum C Programming
    Replies: 2
    Last Post: 03-31-2009, 11:45 AM
  2. Strange core dump, help please
    By Smattacus in forum C Programming
    Replies: 6
    Last Post: 09-30-2008, 11:50 AM
  3. Line Counting
    By 00Sven in forum C Programming
    Replies: 26
    Last Post: 04-02-2006, 08:59 PM
  4. File I/O
    By bliss in forum C++ Programming
    Replies: 11
    Last Post: 06-30-2005, 03:38 AM
  5. getchar()
    By shiju in forum C Programming
    Replies: 17
    Last Post: 12-06-2003, 09:13 PM