EOF & precedence of !=


 
Thread Tools Search this Thread
Top Forums Programming EOF & precedence of !=
# 1  
Old 04-28-2004
Question EOF & precedence of !=

Gurus,

I am teaching myself C and have a question.

I wrote a small prog that reads characters as entered at the prompt and checks the value for EOF.

Unless I am 100% wrong, the value will be '1' until getchar() has anything to read in my stream.

/* PROG 1 */
#include <stdio.h>

main()
{
int c;

while (c = getchar() != EOF)
printf("%d\n", c);
}

I compile this code, I run it at the prompt and type a string "1234<enter>" and get this output
1
1
1
1
1

So far, so good, right? For grins, I changed the number of brackets in my while() clause like below:

/* PROG 2 */
...
while ((c = getchar()) != EOF)
...

I re-compile this code and run it: at the prompt, I type the same string "1234<enter>" and get this output
49
50
51
52
10

Now, I obtain the decimal values for the stream I entered (line feed included).

Can someone please explain what is going on?

Thanks in advance! Al.
# 2  
Old 04-28-2004
Based on the title of the thread, you seem to know what's happening. But to spell it out....

You are in effect doing....

c = (getchar() != EOF)

The expression (a != b) will evaluate to 1 if a is not equal to b. If they are equal it evaluates to 0.
# 3  
Old 04-28-2004
Quote:
Originally posted by Perderabo
Based on the title of the thread, you seem to know what's happening. But to spell it out....

You are in effect doing....

c = (getchar() != EOF)

The expression (a != b) will evaluate to 1 if a is not equal to b. If they are equal it evaluates to 0.
I think I realized what I am doing...

Without brackets, the `...!= EOF...` part of the expression is evaluated first. That's why it is equal to 1 and displays accordingly with printf("%d\n", c);

With the brackets, the `...getchar()...` part of the expression is evaluated first. Consequently, EOF is not equal to 1, getchar() does its job and scans the streams, the decimal values for the character in my stream display accordingly with printf.

Am I making sense?
# 4  
Old 04-28-2004
Not exactly. getchar() does its work in either case. First it gets that "49". Then we test the 49 to see if it is EOF. It's not. So the (49 != EOF) evaluates to 1. Now we store that 1 in the variable c.

When you put the parenthesis in, getchar() gets that 49. Then we store the 49 in "c". Now we test to see if c is EOF.
# 5  
Old 04-29-2004
Thanks for your reply.

For what it is worth, I want to say that I greatly appreciate your effort and patience in answering my (stupid/basic...) questions.

Thanks again!

Al.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

SFTP Shell Script Get & Delete && Upload & Delete

Hi All, Do you have any sample script, - auto get file from SFTP remote server and delete file in remove server after downloaded. - only download specify filename - auto upload file from local to SFTP remote server and delete local folder file after uploaded - only upload specify filename ... (3 Replies)
Discussion started by: weesiong
3 Replies

2. UNIX for Dummies Questions & Answers

What should be precedence of using awk, sed, head and tail in UNIX?

Hi All, I am new to unix. In this forum some days back, I have read something like below: 1) Do not use perl if awk can do your work. 2) Do not use awk if sed can do your work. . . . I do not re-collect the whole thing. I think it is good to know the precedence of using these... (2 Replies)
Discussion started by: Prathmesh
2 Replies

3. Shell Programming and Scripting

Precedence in operators issue

Hello, I am trying to write a small acript to change directory to $HOME depending on the user logged in. However when i provide this command say, ABC_USER=myself cd ~${ABC_USER} i am getting the following error, ksh: ~myself: not found I know i am doing something really silly but... (4 Replies)
Discussion started by: arvindspr06
4 Replies

4. Shell Programming and Scripting

PHP read large string & split in multidimensional arrays & assign fieldnames & write into MYSQL

Hi, I hope the title does not scare people to look into this thread but it describes roughly what I'm trying to do. I need a solution in PHP. I'm a programming beginner, so it might be that the approach to solve this, might be easier to solve with an other approach of someone else, so if you... (0 Replies)
Discussion started by: lowmaster
0 Replies

5. Shell Programming and Scripting

confused with << EOF EOF

Hi friends , I am confused with << EOF EOF Most of the cases I found sqlplus $db_conn_str << EOF some sql staments EOF another exapmle is #!/bin/sh echo -n 'what is the value? ' read value sed 's/XXX/'$value'/' <<EOF The value is XXX EOF (1 Reply)
Discussion started by: imipsita.rath
1 Replies

6. Programming

Makefile -> pc precedence over c

Hi All, I have created a common makefile that compiles both pc and c files. i have created the dependency between the files as .pc.o: ----------- .c.o: ----------- I will be deleting the .c files created from the .pc files, once the object file is created. ( better storage... (7 Replies)
Discussion started by: quintet
7 Replies

7. Programming

Eof

How can I write EOF into a file? I tryed to do as follows: size=sizeof(EOF); end_of_file=EOF; write(fdMutexRichieste, &end_of_file, size); But it does non work correctly, becouse in the next cicle (using lseek(..., SEEK_END) of my code it seems to ignore my EOF and use the LAST... (5 Replies)
Discussion started by: DNAx86
5 Replies

8. Shell Programming and Scripting

What is << EOF

I'm trying to connect to oracle with the following code in the script: checksqlerror{ if echo $1 exit fi } $SQLPLUS username/password@schemaname checksqlerror "failed to connect to oracle" If I'm passing wrong schema name,instead of executing checksqlerror it stops and expects user... (2 Replies)
Discussion started by: BhawanaAggarwal
2 Replies

9. Shell Programming and Scripting

setting precedence with getopts

Hi, I am re-writing a script I wrote which emulated the "rm" command, in my orginal script I had problems with precedence, I did find a way round it by creating a seperate case statements which checked the options and performed the actions accordingly, does anyone know if I can use getopts... (1 Reply)
Discussion started by: jack1981
1 Replies

10. Shell Programming and Scripting

precedence of stderr and stdout

#!/usr/bin/perl open(STDOUT, ">>$Textfile") open(STDERR, ">>$Textfile") print "program running\n"; $final = join("+", $initial,$final) #5 close (STDOUT); close (STDERR);Hi all, above is my perl code. Notice i have captured the stdout and stderr to the same textfile. my code is expected to... (1 Reply)
Discussion started by: new2ss
1 Replies
Login or Register to Ask a Question