ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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
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.
Distribution: Currently: OpenMandriva. Previously: openSUSE, PCLinuxOS, CentOS, among others over the years.
Posts: 3,881
Original Poster
Rep:
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
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"
Distribution: Currently: OpenMandriva. Previously: openSUSE, PCLinuxOS, CentOS, among others over the years.
Posts: 3,881
Original Poster
Rep:
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.
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.
Distribution: Currently: OpenMandriva. Previously: openSUSE, PCLinuxOS, CentOS, among others over the years.
Posts: 3,881
Original Poster
Rep:
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).
Step back and think about exactly what you want my_function to do:
It does not need any information from main.
It receives a character from the user.
It gives the character to main.
Now let's work out how to accomplish each part:
myfunction will have no argument (also known as parameter).
I think you have the user input covered all right.
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;
}
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
Distribution: Currently: OpenMandriva. Previously: openSUSE, PCLinuxOS, CentOS, among others over the years.
Posts: 3,881
Original Poster
Rep:
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.
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.
Distribution: Currently: OpenMandriva. Previously: openSUSE, PCLinuxOS, CentOS, among others over the years.
Posts: 3,881
Original Poster
Rep:
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
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...
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.
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
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.