0

I am checking if user enters the correct number and type of cmd arguments when calling main.

I thought it would be a great idea to write a function, which prints out some text, so I can reuse it when checking for NULL pointers. I included <errno.h>.

void errcall() { perror("Error printed by perror()"); exit(EXIT_FAILURE); } 

Then I wrote a function to check if arguments are right and sufficient.

void err_cmd_handle(int argc_input) { if(argc_input==1 || argc_input>2) errcall(); } 

When I call this in main, giving int argc as an argument to err_cmd_handle(), then I get a success, even when I did not give any arguments besides starting the program. Why does the condition fail to check correctly?

int main(int argc,char* argv[]) { err_cmd_handle(argc); return 0; } 
14
  • 3
    Why do you think errno might have a useful value when you call errcall? Commented Nov 19, 2019 at 17:44
  • 4
    If for example a standard library function returns an error state (as documented) and it is documented that it sets errno, then it is valid to check errno. It is not a general purpose error handling support - it is for reporting errors from the standard library. Commented Nov 19, 2019 at 18:02
  • 1
    @BalázsBörcsök : Don't do that - have patience, answer the comments by improving the question - you are being over sensitive. Ultimately your question is not about errno, it is about why the if(argc_input==1 || argc_input>2) apparently fails. Your question cannot be answered without further information and investigation because it is implausible and is more likely an observation error. Commented Nov 19, 2019 at 18:06
  • 2
    No - after function calls that explicitly state they set it in their documentation - not all function calls. It is off-topic however - you did not ask about errno - just talked about it - it may have been better had you not done so and simplified the question to just exit-success, exit-error and asked separately about validation of command line arguments. Commented Nov 19, 2019 at 18:07
  • 1
    You did not "get a success", rather you got an error and printed "success". Simple as that. In future post all of: input, actual output, expected output, code (complete not fragments) and if there are build issues, the build log. Consider that a check list for a good question and you will get far fewer problems in future. Even rookie questions can get up-votes just for being well formed. Do persevere. Commented Nov 19, 2019 at 18:36

2 Answers 2

4

If you take your complete code with required headers:

#include <stdio.h> #include <errno.h> #include <stdlib.h> void errcall() { perror("Error printed by perror()"); exit(EXIT_FAILURE); } void err_cmd_handle(int argc_input) { if(argc_input==1 || argc_input>2)errcall(); } int main(int argc,char* argv[]) { err_cmd_handle(argc); return 0; } 

and then run it with no arguments, the output is:

Error printed by perror(): Success 

Clearly the validation has worked because errcall() has been called. The text "Success" is simply because the value of errno is zero - because nothing has set it.

Your original code before you changed the question had:

if(argc_input==1 || argc_input>2)errcall(); else exit(EXIT_SUCCESS); 

So when you stated:

then I get a success, even when I did not give any arguments besides starting the program

It was reasonable to assume that it was terminating via exit(EXIT_SUCCESS); - that is clearly not the case. It was also you I originally assumed that errno had nothing to do with the question because it seemed that errcall() cannot have been called if it returned EXIT_SUCCESS. Hopefully you see why your question caused so much confusion and comment?

Sign up to request clarification or add additional context in comments.

3 Comments

Note also that function calls can modify errno. Per 7.5 Errors &lt;errno.h>, p3: "The value of errno may be set to nonzero by a library function call whether or not there is an error, provided the use of errno is not documented in the description of the function in this International Standard." I'm also pretty certain I've seen standard functions that do document their use of errno munging its value when there has been no error, so the value of errno is only truly useful immediately after a function has failed.
@AndrewHenle I think your comment is misplaced. It has no relevance to this answer. Perhaps you were addressing comments posted to the question itself. Earlier in that conversation, I clearly stated that errno only indicates an error when the function that sets it returns the error state.
@BalázsBörcsök consider this, you have decided that argc_input==1 || argc_input>2 is an error condition, perrno() does not magically know to print an error, it simply renders specific known standard values of errno as text. You could set errno yourself, but it was really intended for system errors rather than application level errors, where you could output more helpful and relevant information such as a command line help/usage guide
1

Errno hasn't been set.

void errcall() { printf("Value of errno: %d\n", errno); printf("Error: %s\n",strerror(errno)); perror("Error printed by perror()"); exit(EXIT_FAILURE); } int main(int argc,char* argv[]) { if(argc!=2) { errno=EINVAL; errcall(); } return 0; } 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.