1

I am suppose to read the number of thread requested by the client. So if someone run my program like this:

./test 2 

I have to read the number of thread equal to 2. I try getchar() but it's not working. Any idea?

5
  • In this case the 2 is being passed as an argument to the program. Try searching for information about processing arguments in a C program. Commented Mar 8, 2013 at 22:18
  • 2
    I have an idea for you — read at least some book on C programming... Commented Mar 8, 2013 at 22:18
  • I tried argv[0], but it cannot read "2" Commented Mar 8, 2013 at 22:22
  • Check out stackoverflow.com/questions/3371886/command-line-arguments-in-c Commented Mar 8, 2013 at 22:25
  • argv[0] always contains the program name invoked by the command. Thus, the first real argument is stored in argv[1]. But you have to check if such an argument exist. To do so, just read argc, it tells you the number of elements pointed by argv. In your case, it should equal 2. Commented Mar 8, 2013 at 22:37

3 Answers 3

4

Here is a minimal example with complete, correct error checking and diagnostic messages. Note that setting errno to 0 is necessary for distinguishing range errors from valid strtoul() outputs, this is an annoying quirk of the function.

#include <stdlib.h> #include <stdio.h> #include <errno.h> int main(int argc, char *argv[]) { if (argc != 2) { fputs("usage: test NTHREAD\n", stderr); exit(1); } char *e; errno = 0; unsigned long nthread = strtoul(argv[1], &e, 0); if (!*argv[1] || *e) { fputs("error: invalid NTHREAD\n", stderr); exit(1); } if (nthread == (unsigned long) -1 && errno == ERANGE) { fputs("error: NTHREAD out of range\n", stderr); exit(1); } // Your code goes here } 
Sign up to request clarification or add additional context in comments.

7 Comments

Won't errno be zero to start with?
@paxdiablo: I put errno = 0 in order to allow the block of code to be copied and pasted into sections where the precondition errno == 0 is not guaranteed. It also means that people reading the code do not have to examine surrounding code to verify the precondition.
Fair point, that makes sense.
Perhaps I misunderstand, but I don't think this is valid C (although it might be OK for a C++ sample) because C requires all variable declarations to occur before any code (so the declarations of *e and nthread would cause errors)...
@neilr8133: You are referring to an obsolete version of the C standard. The C standard has allowed such declarations since 1999, and widespread compiler support for the feature is older than that.
|
3

This has nothing to do with threads per se.

The first argument to your program, if given, will be found in argv[1] but as a string, so you need to turn it into an integer with something like atoi or strtol.

Comments

2

int main(int argc, int **argv)

Using arguments of main, you should know the first argument argv[0] is the name of current executing file, and the following arguments are the parameters sent to your program.

In your case, you must read argv[1].

Always check argc to count the entered arguments.

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.