Sponsored Content
Full Discussion: new to C
Top Forums Programming new to C Post 32792 by pat on Friday 6th of December 2002 02:26:06 PM
Old 12-06-2002
new to C

although i'm new to programming in C i'm not new to programming. Normally i program in Pl/1 or cobol on a mainframe.
I wanted to learn C so i bought myself the book the C programming language. I know it's a simple program but i want to learn and understand the language so that's why
i want to know why it isn't working as i expected.
one of the examples in the book is the one below
Code:
#include <stdio.h>
main()
{
    int c;
    c = getchar();
     while (c != EOF) {
          putchar(c);
          c =getchar();
     }
}

first when i type -1 (i thought is EOF) the program doesn't stop.(At least used printf to show what EOF has as a value).
Second i created the one below. I guessed after two loops the program stops but it doesn't could someone explain to me why ??? (i use gcc on linux 8.0 to compile )
Code:
#include <stdio.h>

main()
{
        int c, i ;
        c = 0;
        i = 0;
        while ( c != EOF) {
                c = getchar();
                putchar(c);
                i++;
                printf("i has value %d", i);
                if (i == 2){
                        c = EOF;
                        printf("\nend of file #%d#\n", c);
                }
        }
}

(I added code tags so we can see the indentation... Perderabo)

Last edited by Perderabo; 12-06-2002 at 06:07 PM..
 
GETS(3) 						     Linux Programmer's Manual							   GETS(3)

NAME
fgetc, fgets, getc, getchar, gets, ungetc - input of characters and strings SYNOPSIS
#include <stdio.h> int fgetc(FILE *stream); char *fgets(char *s, int size, FILE *stream); int getc(FILE *stream); int getchar(void); char *gets(char *s); int ungetc(int c, FILE *stream); DESCRIPTION
fgetc() reads the next character from stream and returns it as an unsigned char cast to an int, or EOF on end of file or error. getc() is equivalent to fgetc() except that it may be implemented as a macro which evaluates stream more than once. getchar() is equivalent to getc(stdin). gets() reads a line from stdin into the buffer pointed to by s until either a terminating newline or EOF, which it replaces with ''. No check for buffer overrun is performed (see BUGS below). fgets() reads in at most one less than size characters from stream and stores them into the buffer pointed to by s. Reading stops after an EOF or a newline. If a newline is read, it is stored into the buffer. A '' is stored after the last character in the buffer. ungetc() pushes c back to stream, cast to unsigned char, where it is available for subsequent read operations. Pushed - back characters will be returned in reverse order; only one pushback is guaranteed. Calls to the functions described here can be mixed with each other and with calls to other input functions from the stdio library for the same input stream. For non-locking counterparts, see unlocked_stdio(3). RETURN VALUE
fgetc(), getc() and getchar() return the character read as an unsigned char cast to an int or EOF on end of file or error. gets() and fgets() return s on success, and NULL on error or when end of file occurs while no characters have been read. ungetc() returns c on success, or EOF on error. CONFORMING TO
ANSI - C, POSIX.1 BUGS
Never use gets(). Because it is impossible to tell without knowing the data in advance how many characters gets() will read, and because gets() will continue to store characters past the end of the buffer, it is extremely dangerous to use. It has been used to break computer security. Use fgets() instead. It is not advisable to mix calls to input functions from the stdio library with low - level calls to read() for the file descriptor associ- ated with the input stream; the results will be undefined and very probably not what you want. SEE ALSO
read(2), write(2), ferror(3), fopen(3), fread(3), fseek(3), puts(3), scanf(3), unlocked_stdio(3) GNU
1993-04-04 GETS(3)
All times are GMT -4. The time now is 07:50 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy