getc and int data type

This is a discussion on getc and int data type within the C Programming forums, part of the General Programming Boards category; I've trying to test things out to know how they work. We were asked to do an html tag counter ...

  1. #1
    Registered User
    Join Date
    Aug 2008
    Posts
    1

    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.

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    int main(int argc, char **argv)
    {
            int c_html;
            FILE *html_file;
    
            html_file  = fopen(argv[1], "r" );
    
            if(argc<2)
            {
                    printf("Please put an input file.\n"
                            "Use the syntax: ./a <filename>\n"
                            "The < and > are not include in the syntax.\n");
                    exit(1);
            }
            else
            {
                    while(1)
                    {
                            c_html = getc(html_file);
    
                            if(c_html == EOF)
                                    break;
    
                            printf("&#37;c", c_html);
                            /*printf("%d", argc);*/
                    }
            }
            fclose(html_file);
            return(0);
    }
    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
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    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.

    --
    Mats
    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
    Location
    Singapore
    Posts
    21,717
    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.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  4. #4
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,535
    > 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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

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, 08:13 PM

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