Find the output of a C program [closed] - c

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

Related

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

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.

execvp wont print echo command [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 4 years ago.
Improve this question
I am trying to understand execvp command, when i type /bin/ls it print the right output out but not when i print echo something.
char* argumentCommand[11];
char args[10][256]; // inside of this are arguments
//args[0] = echo args[1] = "some text"
//point it to the argument so i can pass it as a vector
for(int i = 0; i<10;i++)
{
argumentCommand[i] = args[i];
}
//wont print anything when i type echo something or wont take the second parameter when i use /bin/ls -l
execvp(argumentCommand[0],argumentCommand);
Does anyone know where the mistake it is?
To wrap this up:
You need to terminate your argument vector argumentCommand with a NULL pointer, and you need to do it at the correct position!
So if you have three arguments:
char* argumentCommand[11];
char args[10][256]; // inside of this are arguments
strcpy(args[0], "ls");
strcpy(args[1], "-l");
strcpy(args[2], "/");
and you fill your argumentCommand-vector like you did:
for(int i = 0; i<10;i++)
{
argumentCommand[i] = args[i];
}
you still have to NULL-terminate it after the third argument:
argumentCommand[3] = NULL;
before executing:
execvp(argumentCommand[0],argumentCommand);
Then it should work as you expect.
Otherwise, the argumentVector is either not NULL-terminated at all, leading to an -EFAULT on execvp(), or it is incidentally NULL-terminated somewhere in memory, which results in the command receiving a number of pseudo-random arguments, leading to unexpected behaviour.
Replace this
execvp(argument[0],argumentCommand);
with
execvp(argumentCommand[0],argumentCommand + 1);
if argumentCommand holds correct executables with valid option & null terminated. For e.g
int main(void) {
char* argumentCommand[] = {"/bin/ls","ls","-l",NULL}; /* Or you can use as you are trying but make sure when you are doing argumentCommand[i] = args[i];, at the end argumentCommand should have valid input */
execvp(argumentCommand[0],argumentCommand + 1);
return 0;
}

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.

impacting Strings with pointers [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 5 years ago.
Improve this question
The code which does not work. In function main after initializing variables by the user, it goes to if part and after that crash happens. This program's goal is to impact the repetitive characters in the string. So I decided to have a pointer in a function f1 and the pointer should point to 0th char of the array kar[]
then compare the 0th char to 1st char but the program crashes exactly after the last scanf("%s",kar[1000]).
where is the problem?
#include <stdio.h>
char f1(char* p)
{
int i=0,c=1;
while(*(p+i))
{
if(*p==*(++p))
{
c+=1;
p++;
i++;
continue;
}
else
{
if(c>1)
{
printf("%c%d",*p,c);
}
else
{
printf("%c",*p);
}
}
i++;
}
}
int main()
{
int n,i,m;
char kar[1000];
scanf("%d",&n);
for(i=1;i<=(2*n);++i)
{
scanf("%d",&m);
scanf("%s",kar[1000]);
if(m==1)
{
f1(kar);
}
}
}
This
scanf("%s",kar[1000]);
should trigger a compiler warning, as you pass a char where a char* is expected. You pass the 1001st element of kar (which is out of kar's bounds, BTW). In C array indices start with 0 for the 1st element.
To scan into kar, just pass the address of kar's 1st element.
scanf("%s", &kar[0]);
As arrays get decayed to the address of their 1st element when being passed to a function the following statement is equivalent
scanf("%s", kar);
Please note that the above two statements very well allow the user to enter more characters then kar can hold and with this make scanf() overflow kar, that is write beyond it bounds, invoking undefined behaviour, which might crash your program or whatever ...
To avoid this tell scanf() the maximum to scan by doing:
scanf("%999s", kar);
The maximum for chars a strings can hold in C is always one less then defined as C strings need one more char to have their end marked by a '\0'terminator.

argc and argv[] in C language [closed]

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.

Resources