Why the first argument of argv is 1 not 0 [duplicate] - c

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.

Related

Array access args[0][1]-'0' [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 4 years ago.
Improve this question
I have searched for any reference to this for quite a bit now but I haven't had any success, so I thought I would ask here. Basically, I am trying to understand a C written program for creating a shell in linux and I am having problems with this piece of code
...
else if (args[0][0]-'!'==0){
int x = args[0][1]-'0';
int z = args[0][2]-'0';
...
}
The args is storing the command's entered by the user. For instance, later the address space of the child (parent process reads the commands, child executes them) is replaced using a call to execvp(args[0], args). The definition of args is as follows: char *args[MAX_LINE/2 +1];
What I have been having trouble understanding is the ways in which the array is accessed; specifically what is meant by these expressions in this context:
args[0][0]-'!'==0
args[0][1]-'0';
args[0][2]-'0';
Judging by the name of the variable, args stands for a list/array of arguments.
arg[0] is the first element of that array.
args[0][0] is the first character of the first element of that array.
The expression args[0][0]-'!'==0 checks whether that character is equal to '!'. That could have been written better as args[0][0] == '!'.
It's as if instead of using if ( i == 10 ), you decide to use if (i-10 == 0).
The next two lines
int x = args[0][1]-'0';
int z = args[0][2]-'0';
expect that the second and third characters of the first argument are digits and extract the decimal values they correspond to. If the first argument is "!26", then x will have the value 2 and z will have the value 6.
That logic depends on the guarantee that the encoding used for the characters '0' - '9' are required to be contiguous.
Probably args is a reference to
int main(int argc, char **argv);
Then args[0] is the name of the program and in the following args you would find the Arguments of the Programm, see e.g. Arguments to main in C
Thus args[0[0] is the first character of the name of the program.

Reduction in size of array when passed to a function [duplicate]

This question already has answers here:
Finding length of array inside a function [duplicate]
(7 answers)
Length of array in function argument
(9 answers)
Closed 4 years ago.
This code takes input from user and simply stores them in an array.The array is then passed to a function and then printed out.The problem is that when I check the size of array in main() and then in function() they are different.Please check the output image link I provided to clearly understand what I am saying. The size of int is 4 bytes. Dev-C++ is IDE used which uses TDM-GCC 4.9.2 64 bit compiler.Can someone clarify what is going on here?
#include<stdio.h>
int function(int formal[]);
main()
{
int input[10],loop,limit;
printf("Enter limit:");
scanf("%d",&limit);
for(loop=0;loop<limit;loop++)
{
scanf("%d",&input[loop]);
}
printf("Size of input array:%d\t",sizeof(input));
function(input);
}
int function(int formal[])
{
int loop;
printf("Size of parameter array:%d\n",sizeof(formal));
for(loop=0;loop<5;loop++)
{
printf("%d\n",formal[loop]);
}
}
output: [1]: https://i.stack.imgur.com/LqHiS.png

Pthreads_create function that create threads [duplicate]

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;
}

Find the output of a C program [closed]

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

Program doesn't show expected output [duplicate]

This question already has answers here:
Printing array elements
(6 answers)
Closed 9 years ago.
The expected output of the following C program is to print the elements in the array. But when actually run, it doesn't do so.
#include<stdio.h>
#define TOTAL_ELEMENTS (sizeof(array) / sizeof(array[0]))
int array[] = {23,34,12,17,204,99,16};
int main()
{
int d;
for(d=-1;d <= (TOTAL_ELEMENTS-2);d++)
printf("%d\n",array[d+1]);//printing the array
return 0;
}//looks simple but no result
What's going wrong? Why am I not getting any output?
In the comparison
d <= (TOTAL_ELEMENTS-2)
TOTAL_ELEMENTS has type size_t so d is converted to unsigned. For, say, sizeof(size_t)==4, this makes the test
0xffffffff < 5
which fails, causing the loop to exit.
If you really want to start your loop counter from -1
d <= (int)(TOTAL_ELEMENTS-2)
would work

Categories

Resources