LinuxQuestions.org
Review your favorite Linux distribution.
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 12-13-2018, 02:09 AM   #61
Michael Uplawski
Senior Member
 
Registered: Dec 2015
Posts: 1,622
Blog Entries: 40

Rep: Reputation: Disabled

Branching in, here. I have not read the posts after my own last.

In your if-cascade, you compare variables, not 1 variable to 1 literal character:
Code:
if(c == d)
But at this time, the variable d does not contain a usable value. And it is not, what you intended to do, either. This said, and once you have corrected the conditions, you will need less variables, too.

Hazel's statement above, about naming variables, is very important and it saves you a lot of trouble. If you had called the variables, e.g. “in_char” in myfunction(), “comp_char” for the if-cascade, then tried a similar convention on the others, the error would have been avoided.

Last edited by Michael Uplawski; 12-18-2018 at 12:05 PM. Reason: read more posts, Kraut2English, Grammar... stuff
 
1 members found this post helpful.
Old 12-13-2018, 03:14 AM   #62
mina86
Member
 
Registered: Aug 2008
Distribution: Debian
Posts: 517

Rep: Reputation: 229Reputation: 229Reputation: 229
Also note that despite description, your program has no nested ifs in it. Once again, indent your code properly.

myfunction always returns zero. What’s the point of that? Better get rid of the global variable c and make myfunction return character that it read. There’s also no need to widen types from char to int.

With that, there’s no need for any of the global variables.

And as always, don’t use exit in main, prefer plain return from main.

Lastly, you don’t need unistd.h for anything.
 
1 members found this post helpful.
Old 12-13-2018, 05:09 AM   #63
jsbjsb001
Senior Member
 
Registered: Mar 2009
Location: Earth, unfortunately...
Distribution: Currently: OpenMandriva. Previously: openSUSE, PCLinuxOS, CentOS, among others over the years.
Posts: 3,881

Original Poster
Rep: Reputation: 2063Reputation: 2063Reputation: 2063Reputation: 2063Reputation: 2063Reputation: 2063Reputation: 2063Reputation: 2063Reputation: 2063Reputation: 2063Reputation: 2063
Thanks for the reply Michael, I'll do some reading up on what you said, and hopefully I can correct my code to reflect what you've said.

Quote:
Originally Posted by mina86 View Post
Also note that despite description, your program has no nested ifs in it.
You're right, I completely forgot to change that - changing that now. I think I get enough of an idea on how they work (nested if's) now anyways.

Quote:
Once again, indent your code properly.
I'll have a look at the link you posted about that - I was trying to understand the code, and trying to get it working as intended. But I see your point, so thanks for pointing that out.

Quote:
myfunction always returns zero. What’s the point of that? Better get rid of the global variable c and make myfunction return character that it read. There’s also no need to widen types from char to int.
This is where I'm getting stuck. I'll do some more reading up about it.

Quote:
With that, there’s no need for any of the global variables.
Thanks for the tip.

Quote:
And as always, don’t use exit in main, prefer plain return from main.
return is the next thing I will be practising. But I need to get a handle on exit so I can better understand what you're saying, as well as the differences between return and exit. I know this was discussed before, but I've gotta actually do it to be able understand so it sinks in. I take your point and I'm not ignoring what you have said.

Quote:
Lastly, you don’t need unistd.h for anything.
Yeah, I figured I didn't need some of those headers, but I was more concerned about trying to get the code working right to help me better understand it.

Last edited by jsbjsb001; 12-13-2018 at 05:12 AM. Reason: forgot "that"
 
Old 12-13-2018, 05:43 AM   #64
hazel
LQ Guru
 
Registered: Mar 2016
Location: Harrow, UK
Distribution: LFS, AntiX, Slackware
Posts: 7,650
Blog Entries: 19

Rep: Reputation: 4480Reputation: 4480Reputation: 4480Reputation: 4480Reputation: 4480Reputation: 4480Reputation: 4480Reputation: 4480Reputation: 4480Reputation: 4480Reputation: 4480
Remember, the man page for a function will tell you what header it needs. istr unistd.h is for system calls like fork() and malloc().

However extra headers don't actually do any harm. They don't even make your code bigger, because they are only used to provide information to gcc.

Last edited by hazel; 12-13-2018 at 05:44 AM.
 
1 members found this post helpful.
Old 12-13-2018, 06:22 AM   #65
jsbjsb001
Senior Member
 
Registered: Mar 2009
Location: Earth, unfortunately...
Distribution: Currently: OpenMandriva. Previously: openSUSE, PCLinuxOS, CentOS, among others over the years.
Posts: 3,881

Original Poster
Rep: Reputation: 2063Reputation: 2063Reputation: 2063Reputation: 2063Reputation: 2063Reputation: 2063Reputation: 2063Reputation: 2063Reputation: 2063Reputation: 2063Reputation: 2063
I've tried changing a few things and reading a few things about the errors below, but I'm still getting no-where fast;

Code:
[james@jamespc devel]$ gcc -Wall -Werror test3.c -o test3
test3.c: In function ‘main’:
test3.c:13:3: error: too few arguments to function ‘myfunction’
   myfunction();
   ^
test3.c:6:5: note: declared here
 int myfunction(char);
     ^
test3.c:15:7: error: ‘char_in’ undeclared (first use in this function)
  if ( char_in = d ) {
       ^
test3.c:15:7: note: each undeclared identifier is reported only once for each function it appears in
test3.c:15:17: error: ‘d’ undeclared (first use in this function)
  if ( char_in = d ) {
                 ^
test3.c:19:19: error: ‘e’ undeclared (first use in this function)
    if ( char_in = e ) { 
                   ^
test3.c:25:17: error: ‘f’ undeclared (first use in this function)
  if ( char_in = f ) {
                 ^
test3.c:31:19: error: ‘g’ undeclared (first use in this function)
    if ( char_in = g ) {
                   ^
test3.c:37:21: error: ‘h’ undeclared (first use in this function)
      if ( char_in = h ) {
                     ^
test3.c:43:23: error: ‘i’ undeclared (first use in this function)
        if ( char_in = i ) {
                       ^
test3.c: In function ‘myfunction’:
test3.c:56:1: error: parameter name omitted
 int myfunction(char)
 ^
I had all those variables there because of the "undeclared" errors above (it still didn't work tho). I tried looking at this, this and this, but the "parameter name" error is not anymore clear to me. I'm sorry, I don't understand what's wrong here, other than I've missed something(s).

I'm sorry, but that tutorial I've been following simply isn't going into enough details. I tried re-reading "data types" and what not in it, but it's just too vague.

I've attached my code. Sorry guys, it really is getting hopeless at the moment.
Attached Files
File Type: txt test3.c.txt (1.1 KB, 26 views)
 
Old 12-13-2018, 06:41 AM   #66
hazel
LQ Guru
 
Registered: Mar 2016
Location: Harrow, UK
Distribution: LFS, AntiX, Slackware
Posts: 7,650
Blog Entries: 19

Rep: Reputation: 4480Reputation: 4480Reputation: 4480Reputation: 4480Reputation: 4480Reputation: 4480Reputation: 4480Reputation: 4480Reputation: 4480Reputation: 4480Reputation: 4480
You declared myfunction to have one argument, a character. But you have called it without any argument at all! Hence the first message. The others are due to you not putting single quotes round your character constants. In the absence of these, gcc treats them as variable names and complains that you haven't declared them.

char_in is a genuine variable. But you didn't declare that either so gcc can't use it.

Please make sure everything is declared before you use it.

Last edited by hazel; 12-13-2018 at 06:43 AM.
 
1 members found this post helpful.
Old 12-13-2018, 07:27 AM   #67
jsbjsb001
Senior Member
 
Registered: Mar 2009
Location: Earth, unfortunately...
Distribution: Currently: OpenMandriva. Previously: openSUSE, PCLinuxOS, CentOS, among others over the years.
Posts: 3,881

Original Poster
Rep: Reputation: 2063Reputation: 2063Reputation: 2063Reputation: 2063Reputation: 2063Reputation: 2063Reputation: 2063Reputation: 2063Reputation: 2063Reputation: 2063Reputation: 2063
Thanks Hazel, I done what you said but I'm still getting problems, just a different one along with the same "error: parameter name omitted". I've tried at least few different things to fix it, but it just doesn't matter what I try and do - I just keep getting errors. I did understand your post above, and feel pretty stupid for calling myfunction without any argument, as soon as I changed that, it did make a difference (it stopped giving me the same errors - expect the last one), but I've clearly missing something else. I've tried doing a search and tried what it suggested. I also tried changing the "int" to "char", but it did not help.

Code:
[james@jamespc devel]$ gcc -Wall -Werror test3.c -o test3
test3.c: In function ‘main’:
test3.c:14:22: error: expected ‘)’ before ‘char’
   myfunction(char_in char);
                      ^
test3.c: In function ‘myfunction’:
test3.c:57:1: error: parameter name omitted
 int myfunction(char)
 ^
Sorry, I'm stuck again and still stuck on the "parameter name omitted" error. I attached my code once again. Sorry for the trouble - I'll get it one day (hopefully soon).
Attached Files
File Type: txt test3.c.txt (1.2 KB, 25 views)
 
Old 12-13-2018, 07:58 AM   #68
Beryllos
Member
 
Registered: Apr 2013
Location: Massachusetts
Distribution: Debian
Posts: 529

Rep: Reputation: 319Reputation: 319Reputation: 319Reputation: 319
Step back and think about exactly what you want my_function to do:
  1. It does not need any information from main.
  2. It receives a character from the user.
  3. It gives the character to main.
Now let's work out how to accomplish each part:
  1. myfunction will have no argument (also known as parameter).
  2. I think you have the user input covered all right.
  3. myfunction will return the character to main.
So here is the way I would do it:
Code:
// before main
// declare the function
char myfunction (); // char return type, no parameter
Code:
// in main
// declare the character variable char_in
    char char_in;

// use myfunction to get a character and assign it to char_in
    char_in = my_function();
Code:
// in the function definition (the function code)
// define the function's type and arguments (parameters) consistently with the prior declaration
char myfunction ()
{
    char char_in;

// (your code here)

// return the user-inputted character (the value of the local char_in)
    return char_in;
}

Last edited by Beryllos; 12-13-2018 at 07:59 AM.
 
1 members found this post helpful.
Old 12-13-2018, 08:55 AM   #69
Beryllos
Member
 
Registered: Apr 2013
Location: Massachusetts
Distribution: Debian
Posts: 529

Rep: Reputation: 319Reputation: 319Reputation: 319Reputation: 319
Bear in mind that the variable char_in in main is not the same as the variable char_in in myfunction. It's like two guys named Mike who live in separate houses. They just happen to have the same name.

In programming tutorials, I think they use the term scope. A variable which is declared in one function only works in that function. Its scope is limited to that function.

When your function returns char_in, it is actually not returning the variable char_in. It is returning the value contained in it. So if char_in contains the letter 'e', the function reads out the value 'e' and returns nothing but 'e' to main. And main doesn't know the name of the variable used in the function. It only gets 'e' and stores it in its own variable which happens to be called char_in.

In my own programming, I like to avoid confusion and make the scope totally obvious by using different variable names in functions, as in the following example:
Code:
// pass a character value from a function to main
#include <stdio.h>

// function declaration, also known as function prototype
char my_function();

int main ()
{
    // declare the local variable fedex
    char fedex;
    // call my_function and assign the return value to fedex
    fedex=my_function();
    // print the value of fedex
    printf("The character is %c.\n",fedex);
    // And now a well-deserved rest...
    return 0;
}

char my_function()
{
    // declare the local variable amazon
    char amazon;
    // assign a value to amazon
    amazon = 'e';
    // return the value of amazon
    return amazon;
}
In my_function, amazon takes an item 'e' and passes it to main.

In main, fedex receives the 'e' and delivers it.

Last edited by Beryllos; 12-13-2018 at 09:03 AM. Reason: corrected the comments
 
2 members found this post helpful.
Old 12-13-2018, 09:55 AM   #70
jsbjsb001
Senior Member
 
Registered: Mar 2009
Location: Earth, unfortunately...
Distribution: Currently: OpenMandriva. Previously: openSUSE, PCLinuxOS, CentOS, among others over the years.
Posts: 3,881

Original Poster
Rep: Reputation: 2063Reputation: 2063Reputation: 2063Reputation: 2063Reputation: 2063Reputation: 2063Reputation: 2063Reputation: 2063Reputation: 2063Reputation: 2063Reputation: 2063
Hey Beryllos, thanks for stopping by. I tried what you said and while I *think* I understand what you're saying, I'm still missing something. As I'm still getting the error below, I don't know why I'm getting it. I know I'm missing something, but it just seems every time I get close, gcc finds something else wrong.

Code:
[james@jamespc devel]$ gcc -Wall -Werror test3.c -o test3
test3.c: In function ‘main’:
test3.c:17:14: error: expected expression before ‘char’
   myfunction(char);
              ^
Am I missing something in the brackets there? Because I'm buggered if I know what. But thanks again, I need all the help I can get. Assuming I'm ever going to even understand enough to write anything useful.

I added some comments both in the attached code, as well as in the first c file (that's where I've added most of the comments, along with the comments Hazel was kind enough to add for me - I've attached that one as well so people can see the comments - I didn't change any of the code in my first c file posted tho) I posted, to try and help me when I get stuck.

I might call it a night for now tho, it's it's really starting to piss me off that I can't seem to get my latest effort working properly (and have no idea why it's not working). Thanks for your help, and I'll have another look at it all tomorrow.

Thanks again.
Attached Files
File Type: txt test3.c.txt (1.2 KB, 22 views)
File Type: txt test.c.txt (7.2 KB, 26 views)
 
Old 12-13-2018, 10:10 AM   #71
hydrurga
LQ Guru
 
Registered: Nov 2008
Location: Pictland
Distribution: Linux Mint 21 MATE
Posts: 8,048
Blog Entries: 5

Rep: Reputation: 2925Reputation: 2925Reputation: 2925Reputation: 2925Reputation: 2925Reputation: 2925Reputation: 2925Reputation: 2925Reputation: 2925Reputation: 2925Reputation: 2925
It's ages since I programmed in C but a quick look at test3.c shows me that you call myfunction() from main() twice, first with:

Code:
char_in = myfunction();
then with

Code:
myfunction(char);
Why's that?

As it is, the first one looks correct but the second one will fail as myfunction() itself is not declared as accepting parameters. Also, in the second call, you don't make use of the returned value and you shouldn't use "char" as a variable name as it is a reserved word.
 
2 members found this post helpful.
Old 12-13-2018, 10:33 AM   #72
jsbjsb001
Senior Member
 
Registered: Mar 2009
Location: Earth, unfortunately...
Distribution: Currently: OpenMandriva. Previously: openSUSE, PCLinuxOS, CentOS, among others over the years.
Posts: 3,881

Original Poster
Rep: Reputation: 2063Reputation: 2063Reputation: 2063Reputation: 2063Reputation: 2063Reputation: 2063Reputation: 2063Reputation: 2063Reputation: 2063Reputation: 2063Reputation: 2063
I know I said I'd call it a night (and after I finish this post I will), but I couldn't resist having another look at it when I saw your post...

Quote:
Originally Posted by hydrurga View Post
It's ages since I programmed in C but a quick look at test3.c shows me that you call myfunction() from main() twice, first with:

Code:
char_in = myfunction();
then with

Code:
myfunction(char);
Why's that?
Because I'm an idiot when it comes to C programming.

Quote:
As it is, the first one looks correct but the second one will fail as myfunction() itself is not declared as accepting parameters. Also, in the second call, you don't make use of the returned value and you shouldn't use "char" as a variable name as it is a reserved word.
And of course you're absolutely correct, as taking your advice, it worked straight away, and now it works! But I'm bloody hopeless with this tho...

I'm never going to get this :sigh:

Thank you for popping in hydrurga.
 
Old 12-13-2018, 10:53 AM   #73
hydrurga
LQ Guru
 
Registered: Nov 2008
Location: Pictland
Distribution: Linux Mint 21 MATE
Posts: 8,048
Blog Entries: 5

Rep: Reputation: 2925Reputation: 2925Reputation: 2925Reputation: 2925Reputation: 2925Reputation: 2925Reputation: 2925Reputation: 2925Reputation: 2925Reputation: 2925Reputation: 2925
Quote:
Originally Posted by jsbjsb001 View Post
I know I said I'd call it a night (and after I finish this post I will), but I couldn't resist having another look at it when I saw your post...

Because I'm an idiot when it comes to C programming.

And of course you're absolutely correct, as taking your advice, it worked straight away, and now it works! But I'm bloody hopeless with this tho...

I'm never going to get this :sigh:

Thank you for popping in hydrurga.
You've only just started, jsb. Cut yourself some slack. Just take it easy and be methodical and you'll get there...

And keep on posting in this thread - I imagine that it's also of use to other people learning C.
 
2 members found this post helpful.
Old 12-13-2018, 11:30 AM   #74
hazel
LQ Guru
 
Registered: Mar 2016
Location: Harrow, UK
Distribution: LFS, AntiX, Slackware
Posts: 7,650
Blog Entries: 19

Rep: Reputation: 4480Reputation: 4480Reputation: 4480Reputation: 4480Reputation: 4480Reputation: 4480Reputation: 4480Reputation: 4480Reputation: 4480Reputation: 4480Reputation: 4480
What Beryllos said about the scope of variables is very good and useful. I think part of your problem is that you originally used code that contained global variables. This was no doubt a decision by the teacher you are following to "make it easier" for you. But in the long run it has made things more difficult, as such shortcuts usually do, by confusing you about the way in which C uses variables.

Global variables are variables that are defined outside of any function, even main(). They are therefore accessible to all functions. The reason why you don't use them if you can help it is that any function can modify the value of a global variable and then another function can read the altered value when the logic of your program expected the original one. This introduces many bugs. That's why you were advised to use local variables defined within functions and meaningful only within those functions. But code written for global variables is likely to malfunction with local ones, especially if you don't quite know what you are doing.

Local variables only have meaning within the scope of the function they were defined in and this prevents functions from meddling with each others' variables. But their values can be passed between functions. A caller function like main() can pass variables as arguments to the functions it calls, and the current values of those variables will be passed over. And a called function can pass values back to its caller as a return value or a pointer argument.
 
3 members found this post helpful.
Old 12-13-2018, 12:41 PM   #75
Andy Alt
Member
 
Registered: Jun 2004
Location: Minnesota, USA
Distribution: Slackware64-stable, Manjaro, Debian64 stable
Posts: 528

Rep: Reputation: 167Reputation: 167
Quote:
Originally Posted by jsbjsb001 View Post
I'm never going to get this :sigh:
Sure you will. Just think about all you've learned in the last few days. You've been hitting it pretty hard; this may be one of those times where a break is needed. It can be very overwhelming at first, so much new information. After a while, you'll be able to absorb it more easily. Trust me, you'll be amazed at the difference.

There's a couple debugging techniques I haven't seen mentioned in this thread yet. With regard to not getting the exit code you wanted, you can insert printf() statements into your code to see the value of variables. For example (not necessarily for this program), you could have 3 different printf() statements for the same variable, all in different locations that would help you narrow down where the problem is. After you fix the problem you remove or comment out the printf() statements.

But before you get used to that method, you'll want to also be aware of gdb, a program which allows you to see variables while the program is running. Check out this video: https://www.youtube.com/watch?v=sCtY--xRUyI To be able to use gdb, you'll want to add "-g -O0" to your gcc flags. (That's -O0"capital o" and zero")

Last edited by Andy Alt; 12-13-2018 at 12:47 PM. Reason: mention gcc flags for debugging
 
1 members found this post helpful.
  


Reply

Tags
c programming, learning c



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 12:52 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