LinuxQuestions.org
Visit Jeremy's Blog.
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 12-12-2018, 05:31 AM   #46
jsbjsb001
Senior Member
 
Registered: Mar 2009
Location: Earth? I would say I hope so but I'm not so sure about that... I could just be a figment of your imagination too.
Distribution: Currently OpenMandriva. Previously openSUSE, PCLinuxOS, CentOS, among others over the years.
Posts: 2,890

Original Poster
Rep: Reputation: 1481Reputation: 1481Reputation: 1481Reputation: 1481Reputation: 1481Reputation: 1481Reputation: 1481Reputation: 1481Reputation: 1481Reputation: 1481

Quote:
Originally Posted by hazel View Post
You do know, don't you, that coding is seriously addictive?
As I'm beginning to find out, but while I think I know where I've gone wrong (with it not going to the correct exit status) - I still can't figure out which operator and/or condition I should be specifying.

I'm beginning to think learning a foreign spoken language would be easier than trying to learn C...
 
Old 12-12-2018, 08:58 AM   #47
hydrurga
LQ Guru
 
Registered: Nov 2008
Location: Pictland
Distribution: Linux Mint 19.1 MATE
Posts: 8,016
Blog Entries: 5

Rep: Reputation: 2859Reputation: 2859Reputation: 2859Reputation: 2859Reputation: 2859Reputation: 2859Reputation: 2859Reputation: 2859Reputation: 2859Reputation: 2859Reputation: 2859
Quote:
Originally Posted by jsbjsb001 View Post
As I'm beginning to find out, but while I think I know where I've gone wrong (with it not going to the correct exit status) - I still can't figure out which operator and/or condition I should be specifying.

I'm beginning to think learning a foreign spoken language would be easier than trying to learn C...
Currently teaching myself German, and having learned other spoken languages, I can guarantee that this isn't the case. C rules are logic-based and discrete. For the grammar of spoken languages, I find that "irregular" is a very common word, and many rules have 1001 nuanced exceptions.

You won't learn C overnight, but as long as you take the time to learn and understand the base grammar (create your own document, for example, and write down the synthesised grammar rules there as you learn them), you'll be fine.
 
2 members found this post helpful.
Old 12-12-2018, 09:26 AM   #48
Beryllos
Member
 
Registered: Apr 2013
Location: Massachusetts
Distribution: Debian
Posts: 522

Rep: Reputation: 313Reputation: 313Reputation: 313Reputation: 313
@jsbjsb001, let's run a simple program to see what the "=" operator does.

Code:
// program to see what the "=" operator does
#include <stdio.h>
int main ()
{
    int a=2;
    int b=3;
    printf("Prior to test, a and b are %i and %i.\n", a, b);
    if ( a = b )
    {
        printf("The expression a = b is True.\n");
    }
    else
    {
        printf("The expression a = b is False.\n");
    }
    printf("After the test, a and b are %i and %i.\n", a, b);
    return 0;
}
equal_operator.c.txt

Last edited by Beryllos; 12-12-2018 at 10:06 AM. Reason: added attached file, then (oops!) corrected and re-uploaded attachment
 
2 members found this post helpful.
Old 12-12-2018, 09:33 AM   #49
hazel
Senior Member
 
Registered: Mar 2016
Location: Harrow, UK
Distribution: LFS, AntiX, Slackware
Posts: 3,166
Blog Entries: 9

Rep: Reputation: 1681Reputation: 1681Reputation: 1681Reputation: 1681Reputation: 1681Reputation: 1681Reputation: 1681Reputation: 1681Reputation: 1681Reputation: 1681Reputation: 1681
Well, that's not going to work. Or is it intended not to? a=b is always true unless b is zero.
 
Old 12-12-2018, 09:47 AM   #50
Beryllos
Member
 
Registered: Apr 2013
Location: Massachusetts
Distribution: Debian
Posts: 522

Rep: Reputation: 313Reputation: 313Reputation: 313Reputation: 313
Yes. @jsbjsb001's latest attachment is full of "=" (assignment) operators where the "==" (equality relational) operator is required.

And in his latest post he wrote "I still can't figure out which operator and/or condition I should be specifying."

I thought this would help.

Last edited by Beryllos; 12-12-2018 at 09:51 AM.
 
Old 12-12-2018, 09:55 AM   #51
hazel
Senior Member
 
Registered: Mar 2016
Location: Harrow, UK
Distribution: LFS, AntiX, Slackware
Posts: 3,166
Blog Entries: 9

Rep: Reputation: 1681Reputation: 1681Reputation: 1681Reputation: 1681Reputation: 1681Reputation: 1681Reputation: 1681Reputation: 1681Reputation: 1681Reputation: 1681Reputation: 1681
Quote:
Originally Posted by Beryllos View Post
Yes. @jsbjsb001's latest attachment is full of "=" (assignment) operators where the "==" (equality relational) operator is required.
Oops! Should have read the attachment, shouldn't I. But istr that's one mistake gcc always warns you about. So if he follows mina86's advice and sets -werror for his compilation, it should trip him up before he gets a chance to actually run the program.

Last edited by hazel; 12-12-2018 at 09:58 AM.
 
1 members found this post helpful.
Old 12-12-2018, 09:56 AM   #52
jsbjsb001
Senior Member
 
Registered: Mar 2009
Location: Earth? I would say I hope so but I'm not so sure about that... I could just be a figment of your imagination too.
Distribution: Currently OpenMandriva. Previously openSUSE, PCLinuxOS, CentOS, among others over the years.
Posts: 2,890

Original Poster
Rep: Reputation: 1481Reputation: 1481Reputation: 1481Reputation: 1481Reputation: 1481Reputation: 1481Reputation: 1481Reputation: 1481Reputation: 1481Reputation: 1481
Quote:
Originally Posted by Beryllos View Post
Yes. @jsbjsb001's latest attachment is full of "=" (assignment) operators where the "==" (equality relational) operator is required.
...
I thought this would help.
I did actually try the "==" in the if statements, but it didn't work. But maybe I did something else wrong ?

I cannot see anything wrong with the if statements themselves, but maybe I missed something. I'll have another read of the tutorial, as well as the links posted above. But not tonight, need to give my brain a rest for now.

Thanks for your help tho.
 
Old 12-12-2018, 10:36 AM   #53
Beryllos
Member
 
Registered: Apr 2013
Location: Massachusetts
Distribution: Debian
Posts: 522

Rep: Reputation: 313Reputation: 313Reputation: 313Reputation: 313
Ah, thanks for clarifying. There is another error in your if statements:
Code:
    if ( c = d )
In the code fragment above, the expression in parentheses performs an assignment and returns the assigned value. If the assigned value is equal to zero, the boolean value (for the if statement) is false. Otherwise it is true.


Code:
    if ( c == d )
In the code fragment above, the expression in parentheses tests whether the value of c is equal to the value of d.


Code:
    if ( c == 'd' )
In the code fragment above, the expression in parentheses tests whether the value of c is equal to the character constant 'd'. Loosely speaking, d is the variable d, and 'd' is the letter d.

Edit... One more thing. Where you have this,
Code:
char c,d,e,f,g,h,i;
you will only need to declare c. Character constants like 'd', 'e', and so on, are not declared.

P.S. Let me know (because I can't tell) if I am giving away too much and you would rather find the information yourself. All this information can be found online, but it's hard when you don't know the right keywords to search.

Last edited by Beryllos; 12-12-2018 at 10:50 AM.
 
Old 12-12-2018, 10:45 AM   #54
hazel
Senior Member
 
Registered: Mar 2016
Location: Harrow, UK
Distribution: LFS, AntiX, Slackware
Posts: 3,166
Blog Entries: 9

Rep: Reputation: 1681Reputation: 1681Reputation: 1681Reputation: 1681Reputation: 1681Reputation: 1681Reputation: 1681Reputation: 1681Reputation: 1681Reputation: 1681Reputation: 1681
Actually it is bad practice to use names like d for variables. In five minutes, you will have forgotten what "d" is. Variables should always have informative names; it makes your code so much easier to read and debug.

The example I used to quote when I was a librarian at BRE was "If you want a variable to represent a library accession number, call it AccessionNumber, not x.
 
3 members found this post helpful.
Old 12-12-2018, 10:49 AM   #55
jpollard
Senior Member
 
Registered: Dec 2012
Location: Washington DC area
Distribution: Fedora, CentOS, Slackware
Posts: 4,837

Rep: Reputation: 1472Reputation: 1472Reputation: 1472Reputation: 1472Reputation: 1472Reputation: 1472Reputation: 1472Reputation: 1472Reputation: 1472Reputation: 1472
Quote:
Originally Posted by hazel View Post
Those are warnings, not errors. I'm not sure what's causing them but they shouldn't stop the compilation from completing. Are there messages that actually contain the word "Error"? Those are the ones you need to look at.
The include files are accidentally commented out - there is a missing */ on line 5.

The clue is the "implicit definition" which defaults to "int printf()", which conflicts with the standard.

This may also affect the #define TRUE that follows as aI believe the bool.h include defines these.

Quote:
Now another piece of advice. The printf function is very useful for printing out mixtures of text boilerplate and variables (for example on line 46) but it's overkill for plain strings. Try replacing it with puts(). This is the function you usually use for that kind of thing. And you don't need separate statements for all those newlines. Just put them in at the end:
Code:
puts("ramble, ramble, ramble\n\n\n");
The simpler your code, the less likely it is to annoy the compiler.
Even easier is putting contigeous constant string outputs in a single message constant -

Last edited by jpollard; 12-12-2018 at 10:53 AM.
 
1 members found this post helpful.
Old 12-12-2018, 11:02 AM   #56
Andy Alt
Member
 
Registered: Jun 2004
Location: Minnesota, USA
Distribution: Slackware64-stable, Debian64 stable, LFS 7.1
Posts: 490

Rep: Reputation: 161Reputation: 161
Quote:
Originally Posted by jsbjsb001 View Post
And while I think I'm starting to get a better handle on things, I'm not really sure what it's on about below;

Code:
james@jamespc devel]$ gcc -Wall -Werror test3.c -o test3
test3.c: In function ‘main’:
test3.c:14:1: error: implicit declaration of function ‘myfunction’ [-Werror=implicit-function-declaration]
 ret = myfunction();
 ^
cc1: all warnings being treated as errors
Your function is defined after main(). In a manner of speaking, the compiler doesn't know about the function at the time you're trying to use it. Think of it like... the compiler is reading the file from top-to-bottom.

You can put the function definition above main(), or just leave it where it's at and put its function declaration (or prototype) above main().
 
1 members found this post helpful.
Old 12-12-2018, 12:05 PM   #57
Beryllos
Member
 
Registered: Apr 2013
Location: Massachusetts
Distribution: Debian
Posts: 522

Rep: Reputation: 313Reputation: 313Reputation: 313Reputation: 313
@jsbjsb001, Wait a second... There's another error.

(I'm afraid I'm like one of those old compilers that only reports the first error it finds. So you correct it and compile again, and then it reports the next error that it finds. Twenty compiles, twenty errors, and twenty revisions later, you might have a working program, if you're lucky! )

In the latest posted version of test3.c.txt (Post #44), you have nested all the if statements. That will not perform one test after another as you are expecting. Rather, the first test that comes up false will bypass all the remaining tests.

If you completely un-nest the if statements, you'll be another step closer to success.

You will also need to put an exit in the first if statement, like you did all the subsequent ones.

Don't be surprised if someone tells you to use case instead of if, but I think it's fine when you are learning C to write a bunch of separate if statements.
 
1 members found this post helpful.
Old 12-12-2018, 05:09 PM   #58
Andy Alt
Member
 
Registered: Jun 2004
Location: Minnesota, USA
Distribution: Slackware64-stable, Debian64 stable, LFS 7.1
Posts: 490

Rep: Reputation: 161Reputation: 161
Quote:
Originally Posted by jsbjsb001 View Post
I figured you were trying to give me enough hints so I could figure it out, so thank you for that. I think once I get some more experience it will be much easier for me to understand with just some hints given. It kinda does depend on exactly what it is as to whether I'll be able to understand just from hints being dropped. I'm sorry if I come off as a little rude - I didn't mean to, but just thought it was worth telling ya's how I learn.
Nah, you weren't rude, James.

Quote:
I was never any good at school, because it's like, they just dump a load theory on to you, then expect you to be able to read something from cover to cover, and then have a complete understanding of it. And it's like, it's just not going to happen with me - I can't help that - I wish I could, but just can't. So it's no-ones fault at all - it's just me - I'm a practical person not an academic. But, if I can actually see how it works, then I can understand it a LOT better.

Nearly everything (or close to it) I know about Linux wasn't learn't by reading about it - it was learnt because I could sit there and actually use it and play around with it. Something the public education system where I live simply does not understand - and it's complete joke, but that's another story and off-topic too - so I'll stop digressing there.
Same here. The last grade I graduated was 7th.
 
1 members found this post helpful.
Old 12-12-2018, 06:19 PM   #59
scasey
Senior Member
 
Registered: Feb 2013
Location: Tucson, AZ, USA
Distribution: CentOS 7.6
Posts: 3,458

Rep: Reputation: 1160Reputation: 1160Reputation: 1160Reputation: 1160Reputation: 1160Reputation: 1160Reputation: 1160Reputation: 1160Reputation: 1160
Quote:
Originally Posted by jsbjsb001 View Post
I'm beginning to think learning a foreign spoken language would be easier than trying to learn C...
Amen to that...a colleague and I once took a night course in C at the local university. We were programming in a 4GL at the time, on mainframes, and wanted to expand our knowledge of other languages.
He characterized C as a "2 and 1/2 GL"...

I never really got the hang of C -- it was just too much work to master, given that I had no real, practical application to apply it to. Following this thread with interest.

Last edited by scasey; 12-13-2018 at 10:59 AM.
 
Old 12-12-2018, 11:08 PM   #60
jsbjsb001
Senior Member
 
Registered: Mar 2009
Location: Earth? I would say I hope so but I'm not so sure about that... I could just be a figment of your imagination too.
Distribution: Currently OpenMandriva. Previously openSUSE, PCLinuxOS, CentOS, among others over the years.
Posts: 2,890

Original Poster
Rep: Reputation: 1481Reputation: 1481Reputation: 1481Reputation: 1481Reputation: 1481Reputation: 1481Reputation: 1481Reputation: 1481Reputation: 1481Reputation: 1481
Quote:
Originally Posted by Andy Alt View Post
Nah, you weren't rude, James.
Thanks for that, it just seemed to me I may have come off that way.

Quote:
Same here. The last grade I graduated was 7th.
At least I'm not the only one then - I ended up dropping out after 1 term of year 10. Because it's like, I'm learning nothing here - so it's just a waste of everyone's time staying here.

Quote:
Originally Posted by scasey View Post
...
I never really got the hang of C -- it was just too much work to master, given that I had no real, practical application to apply it to.
I do have some ideas on some stuff I want to write - I just don't have the skill yet to do it, that's all...

Anyhow, I've managed to clear all warnings and errors (I had to go back and re-read this), but my program is still not returning the correct exit status - but everything else seems to work. I really have no idea why my exit status's are not working. It's going to the wrong one, instead of the one for the letter I'm inputting.

I have and will even re-read the latest replies to this thread - I'm not ignoring anyone. It's just my post is going to be too long if I quote everyone - so I am reading what you guys are saying.

It just going to be a sllllow march, that's all.

Could someone sorta point me in the right direction? Thanks.

My latest code is attached.
Attached Files
File Type: txt test3.c.txt (1.1 KB, 10 views)
 
  


Reply

Tags
c programming, learning c


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
Finally decided to try to get libinput to work Timothy Miller Linux - Hardware 3 01-04-2018 08:04 PM
Decided to try Lubuntu 14.04 on my netbook... pcninja Ubuntu 4 04-20-2014 08:18 PM
Finally decided to get serious & learn a302svt LinuxQuestions.org Member Intro 1 07-19-2007 12:27 PM
Decided to try Debian some guidance required ninadb Debian 2 08-20-2004 11:40 AM

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

All times are GMT -5. The time now is 11:28 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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration