Question about C?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Pravin Shetty

    #1

    Question about C?

    Hi to all,

    Q)
    main()
    {
    int c;
    c=getchar();

    while(c!=EOF)
    {
    putchar(c);
    c=getchar();
    }
    }

    : - why instead of declaring c as a char is declaered as an int?
    why EOF is defined as an int in <stdio.h>
  • SnaiL

    #2
    Re: Question about C?

    Good day, dear colleague and merry xmas ;-)

    Please read manual --- http://bama.ua.edu/cgi-bin/man-cgi?getchar+3C
    Good luck!

    Comment

    • Mike Wahler

      #3
      Re: Question about C?


      "Pravin Shetty" <pra_m_shetty@r ediffmail.com> wrote in message
      news:c7ebc767.0 501032316.76648 4c6@posting.goo gle.com...[color=blue]
      > Hi to all,
      >
      > Q)
      > main()
      > {
      > int c;
      > c=getchar();
      >
      > while(c!=EOF)
      > {
      > putchar(c);
      > c=getchar();
      > }
      > }
      >
      > : - why instead of declaring c as a char is declaered as an int?[/color]

      Because 'getchar()' returns type 'int'.

      [color=blue]
      > why EOF is defined as an int in <stdio.h>[/color]

      So that it can be distinguished from any possible 'char' value.

      -Mike


      Comment

      • Ben Pfaff

        #4
        Re: Question about C?

        pra_m_shetty@re diffmail.com (Pravin Shetty) writes:
        [color=blue]
        > Hi to all,
        >
        > Q)
        > main()
        > {
        > int c;
        > c=getchar();
        >
        > while(c!=EOF)
        > {
        > putchar(c);
        > c=getchar();
        > }
        > }
        >
        > : - why instead of declaring c as a char is declaered as an int?[/color]

        It's in the FAQ.

        12.1: What's wrong with this code?

        char c;
        while((c = getchar()) != EOF) ...

        A: For one thing, the variable to hold getchar's return value must
        be an int. getchar() can return all possible character values,
        as well as EOF. By squeezing getchar's return value into a
        char, either a normal character might be misinterpreted as EOF,
        or the EOF might be altered (particularly if type char is
        unsigned) and so never seen.

        References: K&R1 Sec. 1.5 p. 14; K&R2 Sec. 1.5.1 p. 16; ISO
        Sec. 6.1.2.5, Sec. 7.9.1, Sec. 7.9.7.5; H&S Sec. 5.1.3 p. 116,
        Sec. 15.1, Sec. 15.6; CT&P Sec. 5.1 p. 70; PCS Sec. 11 p. 157.
        [color=blue]
        > why EOF is defined as an int in <stdio.h>[/color]

        I think it follows from the above.
        --
        "C has its problems, but a language designed from scratch would have some too,
        and we know C's problems."
        --Bjarne Stroustrup

        Comment

        • Keith Thompson

          #5
          Re: Question about C?

          pra_m_shetty@re diffmail.com (Pravin Shetty) writes:[color=blue]
          > Q)
          > main()
          > {
          > int c;
          > c=getchar();
          >
          > while(c!=EOF)
          > {
          > putchar(c);
          > c=getchar();
          > }
          > }
          >
          > : - why instead of declaring c as a char is declaered as an int?
          > why EOF is defined as an int in <stdio.h>[/color]

          The C FAQ is at <http://www.eskimo.com/~scs/C-faq/top.html>.
          See question 12.1.

          --
          Keith Thompson (The_Other_Keit h) kst-u@mib.org <http://www.ghoti.net/~kst>
          San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
          We must do something. This is something. Therefore, we must do this.

          Comment

          Working...