Can I pass parameters to main() in C - c

Im not sure about how it works but is it possible to pass parameters to a main() function like any other function in C? If so, please explain how it works?

You can't "pass parameters" to it because you can't call it per-se. It's the entry point of your application. It gets parameters from the parent process via the operating system.
int main(int argc, char** argv) is how you receive the arguments. How you "send" them is done by an exec call of some variety, like execv().
This is how your shell sends in arguments, so if you run:
./myprogram arg1 arg2
Then you have those arguments available. Note that argv[0] is the name of the program or the "zeroth" argument, which in this example is the "./myprogram" part.

Related

Using execve (linux)

Im confused of the use of the systemcall execve.
The second parameter should be a pointer to the arguments but it doesnt work, it does however execute correctly when I send the whole command(/bin/bash) + arg as a second parameter.
./test /bin/bash "echo 'this is a test' | wc -c"
#include <unistd.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <ctype.h>
int main(int argc, char *argv[]) {
execve(argv[1], &argv[1], NULL); ////works, but I dont understand how since the 2nd param is wrong.
return 0;
}
int main(int argc, char *argv[]) {
execve(argv[1], &argv[2], NULL); ////doenst work, it should since Im sending the correct aguments.
printf("hi");
return 0;
}
argv[0] contains the name of the file being executed. Therefore, execve also needs the file being executed as first element (index 0).
From the execve(2) man page:
argv is an array of pointers to strings passed to the new program
as its command-line arguments. By convention, the first of these
strings (i.e., argv[0]) should contain the filename associated
with the file being executed. The argv array must be terminated
by a NULL pointer. (Thus, in the new program, argv[argc] will be
NULL.)
In other words, the arguments list that you pass in the second parameter includes the name of the executable itself (even though that is already specified in the first parameter). This is how executables are able to detect how they're being invoked (e.g. through a symlink) and show the correct name in their "help" output.
In execve man we can see that its prototype is:
int execve(const char *pathname, char *const argv[], char *const envp[]);
According to execve man (https://man7.org/linux/man-pages/man2/execve.2.html)
argv is an array of pointers to strings passed to the new program
as its command-line arguments. By convention, the first of these
strings (i.e., argv[0]) should contain the filename associated
with the file being executed. The argv array must be terminated
by a NULL pointer. (Thus, in the new program, argv[argc] will be
NULL.)
In respect to these rules you're code should be:
#include <stdio.h>
#include <unistd.h>
int main ()
{
char *filepath = "/bin/echo";
char *argv[] = { filepath, "Hello World", NULL };
execve (filepath, argv, NULL);
return 0;
}
execve call executes a program from the caller. Just as any program in C, it requires argv[0] to be set to the name of the executable being executed. This is generally done automatically when executing from command line, but since you're using execve you have to do it yourself.
If argv_1[1] = <executable path for program_2>, when you call program_2 from program_1 with execve(argv_1[1], &argv_1[1], NULL), program_2 recieves an argv array such that argv_2[0] = argv_1[1], argv_2[1] = argv_1[2], and so on.
Notice how your second main does not follow this rule.

Accessing argc in main function of C program

I am currently trying to check the number of arguments supplied in the command line before my program executes. However, when I write argc within the main function, argc is underlined in red. When I hover over argc, I see a message saying, "Symbol argc could not be resolved." What could be happening?
int main(int argc, char **argv)
{
printf("%d", argc);
}
I can access argv without any issues. I am using the GCC C Compiler and working in Eclipse in a Unix environment.
EDIT: Renaming argc to something else, restarting Eclipse, and then changing that name back to argc resolved my issue. Thank you to everyone who helped me.

How to execute a code whose main function looks like this?

The code is like (real noob question) :
int main(int argc, char **argv){
//some code
}
I know, it means I have to give some arguments while executing in the terminal, but the code does not require any arguments or information from the user. I don't know what to give as the argument?
For example:
#include <stdio.h>
int main(int argc, char **argv)
{
printf("Hello World\n");
}
Compile with GCC,
$gcc prog.c -o prog
$./prog
Hello World
So, If you do not use agave in your code, then, there is no need to provide an argument.
I have to give some arguments while executing in the terminal,
No, you don't have to. You may give some arguments. There are conventions regarding program arguments (but these are just conventions, not requirements).
It is perfectly possible to write some C code with a main without argument, or with ignored arguments. Then you'll compile your program into some executable myprog and you just type ./myprog (or even just myprog if your PATH variable mentions at the right place the directory containing your myprog) in your terminal.
The C11 standard n1570 specifies in ยง5.1.2.2.1 [Program startup] that
The function called at program startup is named main. The implementation declares no
prototype for this function. It shall be defined with a return type of int and with no
parameters:
int main(void) { /* ... */ }
or with two parameters (referred to here as argc and argv, though any names may be
used, as they are local to the function in which they are declared):
int main(int argc, char *argv[]) { /* ... */ }
or equivalent) or in some other implementation-defined manner.
The POSIX standard specifies further the relation between the command line, the execve function, and the main of your program. See also this.
In practice I strongly recommend, in any serious program running on a POSIX system, to give two argc & argv arguments to main and to parse them following established conventions (In particular, I hate serious programs not understanding --help and --version).
You can always pass some number of arguments or pass nothing unless you are checking for the number of arguments and arguments passed (or forcing compiler to do so). Your command interpreter has no idea what your program is going to do with the passed argument or whether the program need any argument. It's your program which takes care of all these things.
For example,
int main(void){
return 0;
}
you can pass any number of arguments to the above program
$ gcc hello.c -o hello
$ ./hello blah blah blah
In case of
int main(int argc, char **argv){
return 0;
}
you can pass no arguments.
$ gcc hello.c -o hello
$ ./hello
For
int main(int argc, char **argv){
if(argc < 3){
printf("You need to pass two arguments to print those on the terminal\n");
exit(0);
}
else{
printf("%s %s\n", argv[1], arv[2]);
}
return 0;
}
You have to pass two arguments because the program checking the number of arguments passed and using them
$ gcc hello.c -o hello
$ ./hello Hello world

run linux command using execlp with more than one argument as string in c

I am trying to run ls using system calls in C with more than one argument, for example -l -a. The arguments and their number is changing depending on the user input. The input is concatenated "-l" + "-a" == "-l -a". The code I'm using is:
execlp("ls","ls",arguments,NULL) //arguments = "-l -a"
The user input is from Terminal:
-l
-a
if you want to executes more than one argument , then you should use execvp() instead of execlp.
#include<stdio.h>
#include <unistd.h>
int main(int argc,char *argv[])
{
execvp(argv[1],argv+1);// argv+1 means whatever arguments after argv[1] it will take & executes it
return 0;
}
for e.g your input like that
xyz#xyz-PC:~$ ./a.out ps -el
I hope it helps.

C Program Standard Input File Name Access

I was wondering if there is a way to access the name of the standard input file within a C program. I've looked around, and haven't found anything. For example
./a.out < file.txt
Is there a way within the C program to print out the name of the file (file.txt)?
If you are using Linux, you could accomplish what you want by reading /proc/[pid]/fd/0 or parsing the out of lsof -p [pid], where [pid] is the process ID of your process.
See proce(5) and lsof(8).
printf("%s", argv[1]);
Make sure you have your main set up to accept command line arguments
int main(int argc, char** argv)

Resources