This question already has answers here:
How can I get argv[] as int?
(5 answers)
Closed 5 years ago.
I was asked to create a program that starts from the command line with an arg that defines how many threads that shall be created, and every thread that been created shall print out its number. I have just started with the threading so please bear with me!
I know learn how to create threads but only predefine nr in the program but how to take the arg from user input? i don't really know how to go about the problem.
The argument can be taken from the entry point of the C application. In the following example, I check if the argument count is 2. The first argument is the name of the program itself, the second being the count of threads you'd like to create.
int main(int argc, char *argv[]) {
if(argc != 2) {
return 0;
}
// Convert the string (char *) to an int with a base of 10
int threads = strtol(argv[1], NULL, 10);
for(int i = 0; i < threads; i++) {
// Create thread
}
return 0;
}
Related
This question already has answers here:
Why do I always get the same sequence of random numbers with rand()?
(12 answers)
Generating a random bit - lack of randomness in C rand()
(4 answers)
Closed 3 years ago.
I am creating a program that prints out 1000 random occurrences of the letters "H" and "T" and my code implements rand()%2 but as i can see from running this program (code below) that its not completely random (the letters are random but always the same with every execution). I want to establish a more effective way by implementing RANDMAX to make every case of the program running completely random, how would I be able to do this?
#include <stdio.h>
#include<stdlib.h>
int main()
{
int i=1,n;
char ch ;
for( i = 1; i <= 1000 ; i ++ )
{
n = rand()%2;
if(n==0)
ch = 'T';
else
ch = 'H';
putchar(ch);
}
return 0;
}
Just add
srand(time(NULL));
after
int main()
{
That will initialize the random generate with a new seed every time you run the program. In this way, you'll get a new random sequence every time.
This question already has answers here:
Arguments to main in C [duplicate]
(6 answers)
What are the arguments to main() for?
(5 answers)
Closed 3 years ago.
I have some parameters to the program work correctly. This arguments shoud be, MAX_NUM, x, y.
When catching the parameters of the char input list, I currently obtain MAX_NUM using the argument 1 instead of 0.
Ex:
int main (int argc, char *argv[]) {
int MAX_NUM = atoi(argv[0]);
int x = atoi(argv[1]);
int t = atoi(argv[2]);
printf("MAX_NUM %d\n", atoi(argv[0]));
....
Priting argv[1] i get the MAX_NUM correctly, when printing the first argument gets 0.
Why C init the char input list array on 1 instead of 0 or the program name?
In simple terms, the value of argv[0] is the name of the program to execute. C kinda reserves the argv[0] index for this purpose. To get into some nitty-gritty details of how C and your operating system start to work with each other - check out this post in Stack Overflow.
This question already has answers here:
Why are these constructs using pre and post-increment undefined behavior?
(14 answers)
Closed 3 years ago.
I wanna use int array in shared memory,after writing 1,2,3 into it,I expect read it like this:1,2,3.But I read this:3,2,1.I don't know why
write code:
int *gIn;
int main(){
int id;
id = shmget(0x666,1024,IPC_CREAT|0666);
gIn=(int *)shmat(id,NULL,0);
*gIn++=10;
*gIn++=20;
*gIn++=30;
sleep(10);
return 0;
}
read code:
int *gIn;
int main(){
int id;
id = shmget(0x666,1024,IPC_CREAT|0666);
gIn=(int *)shmat(id,NULL,0);
printf("%d|%d|%d\n",*gIn++,*gIn++,*gIn++);
return 0;
}
I expect the output of read process to be 10|20|30,but the actual output is 30|20|10.It's very strange.I don't know why
The problem is this line: printf("%d|%d|%d\n",*gIn++,*gIn++,*gIn++);. The order of evaluation for the parameters to printf is implementation defined. In your case it just happens to do this in an order you didn't expect.
Suggest you pull out the values separately before the printf in local variables (or an array) and then print the value.
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 8 years ago.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Improve this question
I'm learning C and in one of the examples we write a program like this:
#include <stdio.h>
int main(int argc, char *argv[])
{
// go through each string in argv
int i = 0;
while(i < argc)
{
printf("arg %d: %s\n", i, argv[i]);
i++;
}
// let's make our own array of stringd
char *states[] = {"California", "Oregon", "Washington", "Texas"};
int num_states = 4;
i = 0; // watch for this
while(i < num_states)
{
printf("state %d: %s\n", i, states[i]);
i++;
}
return 0;
}
and if I run it in terminal like this:
./ex11 test arguments
I get an output of:
arg 0: ./ex11
arg 1: test
arg 2: arguments
state 0: California
state 1: Oregon
state 2: Washington
state 3: Texas
However I don't understand why the "Test argument" part gets printed, i know it has something to do with argc and argv but I don't know how.
Can someone explain this to me (preferably in a simple manner)?
When a command, any command, is run by the shell (actually, any shell) on Unix, then the command line is converted into an array of strings:
cmd_argv[0] = "./ex11";
cmd_argv[1] = "test";
cmd_argv[2] = "arguments";
cmd_argv[3] = NULL;
cmd_argc = 3;
and then invokes:
execvp(cmd_argv[0], cmd_argv);
Note that all I/O redirections are removed, etc. Internally, the system ends up counting the number of non-null pointers at the start of the cmd_argv array and passes them as argv in the new program, and the count as argc. The new program is guaranteed that argc >= 0 and argv[argc] == NULL.
This list is almost the same form as the list of states; the primary difference is that cmd_argv[cmd_argc] is a null pointer, which is certainly not guaranteed with the states data.
That's what your first loop does, it iterates through the strings in argv (which contains the name of the program and the arguments you typed after it), and prints them out. Argc is the number of arguments you passed, or equivalently the length of argv.
argv is the command line arguments, including the executable's name (aguments' values). argc is the amount of elements in argv (argument count).
In your first loop, you are printing the contents of argv, therefore you get the arguments that were passed to your program.
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I have been trying solve this question but unable to understand.
If the following program (myprog) is run from the command line as:
myprog friday tuesday sunday
What would be the output?
#include<stdio.h>
int main(int argc, char *argv[]){
while(sizeof argv)
printf("%s",argv[--sizeof argv]);
return 0;
}
The output is-
sunday tuesday friday myprog
Please explain me the output.
Thanx :-)
I am guessing you really what this. It just prints the command line argument out backwards.
#include<stdio.h>
int main(int argc, char *argv[])
{
while (argc)
printf("%s ", argv[--argc]);
printf("\n");
return 0;
}
Disregarding the --sizeof issue, I'm assuming you want to know about the elements of argv.
It contains the arguments and the name of the program. Read any manual or document describing argv.
There are two issues in your code:
--sizeof argv is illegal. It would result in an error.
while(sizeof argv) will result in an infinite loop as the condition will always be true
So in short the code will not compile and will result in an error.
You probably want to understand command line argument processing in C. When you are given some list of program arguments, for example,
myprog friday tuesday sunday
The C language provides arguments to the main() function which provide the number of arguments (4 in this case), and an array of char* (pointers) to these arguments.
Note that sizeof argv is computed at compile time, and the value is the size of a pointer on your system (4 or 8).
First, we explain the arguments to the main function,
int main(
int argc, //an integer count of the number of arguments provided to the program
char* argv[] //an array of pointers to character arguments
)
Your main function is then defined to (apparently) print out the arguments starting at the rightmost argument and working back to the zero-th argument,
{
int argv_sizeof = argc; //you cannot use sizeof argv the way you specified
//argv_sizeof = 4 in your example, but argv[4] is not valid
//argv_sizeof has a value that is one past the rightmost element of argv[]
while( argv_sizeof ) //use argv_sizeof > 0 here; argv_sizeof is 4,3,2,1,0
//when argv_sizeof reaches 0, the while loop terminates
{
printf("%s",argv[--argv_sizeof]); //here you pre-decrement argv_sizeof (3,2,1,0)
//then use argv_sizeof to index into argv[]
//then you print the string at argv[3], argv[2], argv[1], argv[0]
}
//argv_sizeof = 0 here
return 0; //you return the value 0 from the main function
}
Error:
lvalue required as decrement operand
It does not compile.
sizeof is an operator just like + or % and it gives you the size of an object. So, you can't decrement it. Just as somehting like this wouldn't make any sense: --%
The gist of the question is:
What happens if the input is: myprog friday tuesday sunday
When the code does:
index = lastIndex
while(index) // note: while(0) == false
print(array[--index])
So, the output would be the reversal of the elements:
sunday tuesday friday