My code is as follows in c:
#include <stdio.h>
#include <stdlib.h>
int main(int argc,char *argv[])
{
int i;
for (i=0;i<argc;i++)
{
printf("Hello world! The arguments are %d, argc is %d and the string is %s\n",argc,i,argv);
}
return 0;
}
I'm unable to see arguments properly in the output. It sort of comes encrypted.
I went to projects-> set program arguments. It's not working though. Please help?
You are printing a pointer to pointer, as your compiler is telling you:
test.c:12:9: warning: format ‘%s’ expects argument of type ‘char *’, but argument 4 has type ‘char **’ [-Wformat=]
printf("Hello world! The arguments are %d, argc is %d and the string is %s\n",argc,i,argv);
^
argv is an array of pointers and you want to print the string pointed by each item of that array:
Corrected code:
#include <stdio.h>
#include <stdlib.h>
int main(int argc,char *argv[])
{
int i;
for (i=0;i<argc;i++)
{
printf("Hello world! The arguments are %d, argc is %d and the string is %s\n",argc,i,argv[i]);
}
return 0;
}
Related
I use termux with clang. I've tried compiling the following code in clang but it outputs an error, mentioned in the title.
Here is the code.
#include <ncurses.h>
#include <string>
using namespace std;
int main(int argc, char** argv) {
initscr();
start_color();
init_pair(1,argv[1],argv[2]);
attron(COLOR_PAIR(1));
for (int i = 3; i < argc; ++i) {
printw("%s",argv[i]);
attroff(COLOR_PAIR(1));
}
refresh();
}
The actual error message tells you what the problem is:
> clang -c foo.cc
foo.cc:8:1: error: no matching function for call to 'init_pair'
init_pair(1,argv[1],argv[2]);
^~~~~~~~~
/usr/include/curses.h:648:28: note: candidate function not viable: no known
conversion from 'char *' to 'short' for 2nd argument; dereference the
argument with *
extern NCURSES_EXPORT(int) init_pair (NCURSES_PAIRS_T,NCURSES_COLOR_T,NC...
^
1 error generated.
The init_pair function uses short-integer parameters (not a char*). You could make it compile by converting those char*'s to integer, e.g.,
> diff -u foo.cc.orig foo.cc
--- foo.cc.orig 2020-09-21 17:35:04.000000000 -0400
+++ foo.cc 2020-09-21 17:36:42.000000000 -0400
## -1,11 +1,12 ##
#include <ncurses.h>
+#include <stdlib.h>
#include <string>
using namespace std;
int main(int argc, char** argv) {
initscr();
start_color();
-init_pair(1,argv[1],argv[2]);
+init_pair(1,atoi(argv[1]),atoi(argv[2]));
attron(COLOR_PAIR(1));
for (int i = 3; i < argc; ++i) {
printw("%s",argv[i]);
(though that's just a quick fix).
Im trying to convert the command line argument(*argv[]) to an integer using the atoi function
int main(int argc, char *argv[]) {
This is my attempt
#include <iostream>
#include <sstream>
#include <string>
#include <cstdlib>
#include <conio.h>
using namespace std;
int main(int argc, char *argv[]) {
int x = 0;
for ( x=0; x < argc; x++ )
{
int x = atoi(argv[1]);
cout << x;
}
return 0;
}
However this returns 0 and im unsure why. Thankyou
It's hard to say having the arguments you pass to your program, but there are few problems here.
Your loop goes from 0 to argc, but your inside your loop you always use argv[1], if you didn't pass any arguments you're going out of bounds, because argv[0] is always the path to your executable.
atoi is a function from C, and when it fails to parse it's argument as an int, it returns 0, replace it with std::stoi, and you will get and execption if the conversion failed. You can catch this exception with try/catch, and then check the string that you tried to convert to int.
Well, this
#include <iostream>
#include <sstream>
#include <string>
#include <cstdlib>
#include <conio.h>
using namespace std;
int main(int argc, char* argv[]) {
int x = 0;
for (x = 0; x < argc; x++)
{
cout << argv[x];
}
return 0;
}
just prints the path to the .exe, the path is a string, it has no numbers. And as I understood from my "research" about command line arguments, you need to use your program through a command line, a terminal, to initialise the argv argument.
Link : https://www.tutorialspoint.com/cprogramming/c_command_line_arguments.htm
Also, as I understood at least, the argv[0] is always the path of the .exe
I hope I will be of some help, if I am mistaken at something, pls tell me where and I will correct my self by editing the answer
I am pretty new to C++ and making a program that takes a command line argument and counts how many arguments there are then prints the amount of characters and how many of them are alphabetical. I am getting this error code
Format '%c' expects argument of type 'int', but argument 2 has type 'char*'
(my code)
#include<cstdio>
#include<cctype>
#include<cstdlib>
#include<cstring>
int main(int argc, char *argv[])
{
/*
irrelevant
printf
statements
*/
for(pos=0; pos < maxCharLen; pos++){
int count;
char totalLen = strlen(argv[pos]);
char totalAlpha (isalpha(argv[pos][pos]);
printf("Argument %d is", count);
printf("%c and %c is it's length.\n", argv[pos], totalLen);
printf("%c are alphabetic characters\n", totalAlpha);
count ++
count++;
}
return 0;
}
argv is array of char* so argv[pos] returns char* so you should use %s. After all correct format is:
printf("%s and %c is it's length.\n", argv[pos], totalLen);
#include <stdio.h>
int main(char sendbuf[100])
{
printf (sendbuf);
return 0;
}
Somehow this very basic program crashes when I try to use it, it's meant to print up whatever is typed as a parameter. If I remove the line "printf (sendbuf);" the crash goes away.
The first argument to main is the number of parameters. The second argument is an array of strings. The first element (index 0) of the second argument is the name of your program:
#include <stdio.h>
int main(int c, char **argv)
{
printf ("%s\n", c > 1 ? argv[1] : "No Argument");
return 0;
}
Your first parameter must be an integer, not a char array. Here is the right program:
#include <stdio.h>
int main(int argc, char* argv[])
{
if (argc > 1) {
printf( argv[1] );
}
else {
printf( "No arguments provided" );
}
return 0;
}
argv[0] is your program name, so argv[1] is the first parameter provided on teh command line.
C supports two forms of main function:
int main() { /* ... */ }
and
int main(int argc, char* argv[]) { /* ... */ }
To take parameter from main, you need to change your code to:
#include <stdio.h>
int main(int argc, char* argv[])
{
if (argc > 1){
printf ("%s\n", argv[0]);
}
return 0;
}
Or use stream:
#include <iostream>
int main(int argc, char* argv[])
{
if (argc > 1){
std::cout << argv[0]) << std::endl;
}
return 0;
}
argv[0] is application name, input parameters starts from argv[1] if any.
An implementation must support the following two definitions of main:
int main() { }
int main(int argc, char* argv[]) { }
It is implementation-defined whether they support any other definitions. I don't know of any implementation that allows int main(char*) though (which is what yours is equivalent to).
This will print everything you type on the command line after the program name, even with spaces. It will not crash if you type nothing after the program name.
#include <stdio.h>
int main(int argc, char **argv)
{
for(int i=1; i<=argc; ++i) {
printf("%s\n", argv[i]);
}
}
This question already has answers here:
Closed 12 years ago.
Possible Duplicate:
Pass arguments into C program from command line.
mypro parameter
When run like above,how to get the parameter in mypro's main() :
#include <iostream>
int main()
{
char* str = "default_parameter";
if(parameter_exists())str = parameter;
...
}
How to implement the pseudo code above?
Just need to add (int argc, char *argv[]) to your main function. argc holds the number of arguments, and argv the arguments themselves.
int main(int argc, char *argv[])
{
std::string str = "default";
if (argc > 1) { str = argv[1]; }
}
Note that the command is also included as an argument (e.g. the executable). Therefore the first argument is actually argv[1].
When expecting command-line arguments, main() accepts two arguments: argc for the number of arguments and argv for the actual argument values. Note that argv[0] will always be the name of the program.
Consider a program invoked as follows: ./prog hello world:
argc = 3
argv[0] = ./prog
argv[1] = hello
argv[2] = world
Below is a small program that mirrors the pseudocode:
#include <iostream>
#include <string>
int main(int argc, char **argv) {
std::string arg = "default";
if (argc >= 2) {
default = argv[1]
}
return 0;
}
Your function should use the second of these two allowed C++ main function signatures:
int main(void)
int main(int argc, char *argv[])
In this, argc is the argument count and argv is an argument vector.
For more details, see this Wikipedia article.
Well you need a main function that accepts arguments, like so:
int main(int argc, char *argv[])
{
}
You would then check the number of arguments passed in using argc. Usually there is always one, the name of the executable but I think this is OS dependent and could thus vary.
You need to change the main to be:
int main(int argc, char** argv){
argc is the number of paramaters, argv is an array containing all of the paramaters (with index 0 being the name of the program). Your first parameter will be located at index 1 of argv.
#include <iostream>
using namespace std;
int main(int argc, char* argv[]) {
for(int i = 1; i < argc; i++)
cout << atoi(argv[i]) << endl;
return 0;
}
Here argc gives the count of the arguments passed
and argv[i] gives ith command line parameters.
where argv[0]='name of the program itself'
main has two parameters, int argc and char** argv which you can use to access to the command line parameters.
argc is the number of parameters including the name of the executable, argv points to the parameter list itself (so that argv[0] is "mypro" in your example).
Just declare main like
int main (int argc, char** argv){..}
You need to use this declaration of main:
int main(int argc, _TCHAR* argv[])
{
if (argc > 1)
{
str = argv[1];
}
}
argv[0] is the name of the executable, so the parameters start at argv[1]
On Windows, use the following signature instead to get Unicode arguments:
int wmain(int argc, wchar_t** argv)