LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 05-23-2019, 05:06 PM   #1
anon033
Member
 
Registered: Mar 2019
Posts: 188

Rep: Reputation: 13
K&R Exercise 1-6: What is this asking me to do?


I am working through K&R and have gotten to exercise 1-6 which asks you to

Code:
Verify that the expression getchar() != EOFis 0 or 1.
I am confused about what exactly this is asking. I may be over thinking this or just not understanding what they mean. Do they mean do this?

Code:
#include <stdio.h>

int main()
{
        int c;

        if ((c = getchar()) != EOF) {
                putchar(c);
                return 0;
        }

        else if ((c = getchar()) == EOF) {
                putchar(c);
                return 1;
        }
}
If someone could elaborate on what they are asking for that would be amazing! I would look it up, but I am afraid I'll find the answer which isn't what I want.

#Solution:

I got confused by directions, but luckily my code was correct. However, as a bonus if you look at this code it can be trimmed. I.E.

Code:
#include <stdio.h>

int main()
{
        int c;

        if ((c = getchar()) != EOF) {
                putchar(c);
                return 0;
        }
}

Last edited by anon033; 05-23-2019 at 05:47 PM. Reason: Giraffes
 
Old 05-23-2019, 05:29 PM   #2
Mechanikx
Member
 
Registered: Jul 2018
Distribution: Slackware
Posts: 351

Rep: Reputation: 258Reputation: 258Reputation: 258
They are asking you to test whether the expression "getchar() != EOF" evaluates to 1 or 0, or true or false. When you test an expression in C and it's true it evaluates to 1 and if not it evaluates to zero.

You can verify in different ways. In general:

If this condition is true then do this, else do this.

Last edited by Mechanikx; 05-23-2019 at 05:32 PM.
 
Old 05-23-2019, 05:34 PM   #3
anon033
Member
 
Registered: Mar 2019
Posts: 188

Original Poster
Rep: Reputation: 13
I apologize, this is just really confusing to me. So I went ahead and did the next one

Code:
#include <stdio.h>

int main()
{
        int c;

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

        if (c == EOF) {
                printf("%d\n", EOF);
        }
}
but this one is just confusing me. I believe what I did does as they asked, but are they looking for soemthing like this

Code:
#include <stdio.h>

int main()
{

        if (EOF == 0) {
                printf("EOF is true\n");
        }

        else if (EOF == 1) {
                printf("EOF is false\n");
        }
}
edit: This seems like it makes no sense when I read the code out loud haha. I just wanted to be sure my initial try was correct, the directions just got me confused.

Last edited by anon033; 05-23-2019 at 05:40 PM.
 
Old 05-23-2019, 05:42 PM   #4
Mechanikx
Member
 
Registered: Jul 2018
Distribution: Slackware
Posts: 351

Rep: Reputation: 258Reputation: 258Reputation: 258
No need to apologize. In C, EOF = -1. This is to set it apart from the other values getchar returns (characters). In ASCII 0-255. This is why you're storing the return value in "int c" and not "char c".

Since the expression they reference is "getchar() != EOF" I believe this is the expression whose value they want you to examine. Besides like I said EOF is -1 and it's constant.

Last edited by Mechanikx; 05-23-2019 at 05:56 PM. Reason: Terminology and grammar.
 
Old 05-23-2019, 05:46 PM   #5
anon033
Member
 
Registered: Mar 2019
Posts: 188

Original Poster
Rep: Reputation: 13
Talking

Quote:
Originally Posted by Mechanikx View Post
No need to apologize. In C, EOF = -1. This is to set it apart from the other values getchar returns (characters). In ASCII 0-255. This is why you're storing the return value in "int c" and not "char c".

Since the expression they reference is "getchar() != EOF" I believe this is expression whose return value they want you to examine. Besides like I said EOF is -1 and it's constant.
Aha! Thank you so much
 
Old 05-23-2019, 06:00 PM   #6
Mechanikx
Member
 
Registered: Jul 2018
Distribution: Slackware
Posts: 351

Rep: Reputation: 258Reputation: 258Reputation: 258
You're quite welcome.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] K&R Exercise 1-4: Help Checking My Math anon033 Programming 18 05-23-2019 05:10 PM
[SOLVED] K&R Exercise 1.10 - A Question About Backspaces killingthemonkey Programming 4 02-04-2017 08:25 PM
AOL UK && BT Voyager 100 && Slackware 10.2 && RP-PPPoE pitt0071 Linux - Networking 3 01-17-2006 06:10 AM
Ph&#7909;c h&#7891;i d&#7919; li&#7879;u b&#7883; m&#7845;t???, c&#7913; pollsite General 1 06-27-2005 12:39 PM
Gotta love those &#1649;&#1649;&#1649;&#1649;&#1649;&#1649;&#1649;&# iLLuSionZ Linux - General 5 11-18-2003 07:14 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 05:08 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration