I get segmentation error when I give my program parameter '2' or '3'. It should print That's number two/three. What's the error here?
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char *argv[]) {
if (argc > 1) {
if (*argv[2] == '2') {
printf("That's number two!");
}
else if (*argv[2] == '3') {
printf("That's number 3!");
}
else
printf("Error.\n");
}
else
printf("No input given.\n");
return 0;
}
When I run the program, I type ./Test 2 or 3. I can't find a error, and gcc does not give errors / warnings either.
Thanks.
if (*argv[1] == '2') {
printf("That's number two!");
}
else if (*argv[1] == '3') {
printf("That's number 3!");
}
Index begins with 0.
argv[0] is the name of the program and argv[1] is the first command line parameter.
argv is an array:
argv[0] // the name of the executable
argv[1] // the first argument
argv[...]
When you do:
if (*argv[2] == '2')
and you were called with ./Test 2, there is only a single argument, so argv[2] is an out-of-bounds access.
For more information, see main function.
Related
I have to take an input from command line argument. If the argument is invalid it will print an error message and exit. But whatever argument I am giving it is always producing error message. Even if I give the right argument. I don't know whats wrong.
if (argv[1][0] !='a' || arg[1][0] !='m') {
printf("Error: Invalid arguments!\n");
exit(0);
}
arg[1][0] is not valid. use argv[1][0].
void printError(){
printf("Invalid Parameter\n");
exit(-1);
}
int main(int argc, char**argv){
if(argc < 2)
printError();
switch(argv[1][0]){
case 'a': // your code
break;
case 'm': // your code
break;
default:
printError();
}
return 0;
}
I am having trouble with getting my if statements to work with parameters from the command line. I get an error saying, 'comparison between pointer and integer ('char *' and 'int')' every time. Any help will be greatly appreciated. Here is the description for what I have to do:
This program will read a parameter from the command line and translate it to the name of the corresponding month. I.e. if you run the program with the following parameter:
It will print the following message:
The month is 'April'.
#include <stdio.h>
int main(int argc, char *argv[]) {
if(argv[1] == 1){
printf("You entered the number 1.");
} else {
printf("You entered a different number.");
}
return 0;
}
The parameters passed on the command line are strings, so you need to use strcmp to compare them.
int main(int argc, char *argv[]) {
if(argc > 1 && (strcmp(argv[1],"1") == 0)){
printf("You entered the number 1.");
} else {
printf("You entered a different number.");
}
return 0;
}
Compiler is right - you are comparing char * and int.
You have to either convert argv[1] to int (e.g. with strol()) or convert 1 to char * (by adding quotes - "1") and then compare with strcmp() instead of ==.
And checking number of arguments in argc is also something you should do, when working with arguments. (thanks #Ingo Leonhardt)
Argv returns pointer to array of chars, so your programm must looks like:
#include <stdio.h>
int main(int argc, char *argv[]) {
if(argv[1][0] == '1'){
printf("You entered the number 1.");
} else {
printf("You entered a different number.");
}
return 0;
}
Or you can use atoi (man 2 atoi) function to convert ascii array to int like this:
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char *argv[]) {
if(atoi(argv[1]) == 1){
printf("You entered the number 1.");
} else {
printf("You entered a different number.");
}
return 0;
}
If you want to use your command line parameter like a integer, you must modify the type with atoi
#include <stdio.h>
int main(int argc, char *argv[]) {
if(atoi(argv[1]) == 1){
printf("You entered the number 1.");
} else {
printf("You entered a different number.");
}
return 0;
}
here is a program which printing some information about my self
in this program if the user scanning /n its printing the name and
etc. but when i'm running this program using gcc its printing nothing at all.
i need to scan the parameters using argv and argc.
how can i solve that?
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define SIZE 10
int main(int argc, char** argv)
{
for (int i = 1; i < SIZE; i++)
{
if (argv[i] == ' ')
{
break;
}
if (argv[i] == 'n' || argv[i] == 'b' || argv[i] == 'f' || argv[i] == '?' && argv[i - 1] == '/')
{
switch (i)
{
case 'n':
printf("my name is : Daniel Zingerman \n");
break;
case 'b':
printf("my birth date is: 2/11 \n");
break;
case 'f':
printf("my favorite food is: ice cream \n");
break;
case '?':
printf("the instruction of the program:");
printf("There is a lot of parameters you can scan into the program:");
printf("1. /n - printing the name");
printf("2. /b - printing the birth date");
printf("3. /n - printing the favorite food");
printf("4. /? - printing the instructions");
break;
}
}
}
system("pause");
return(0);
}
You should use argc value to know what is last argument. Or you should test argv[i] == NULL. You seem not to understand argv is char **, so argv[i] is a char *, not a char.
Also learn to use getopt(), or getopt_long().
The argv[] array is created from the program name in argv[0] and then the command line arguments separated by spaces
/n /b /f will be returned in argv[1], argv[2] & argv[3], not as a single 'string' in argv[1]
Use argc to limit how many elements of argv[] you test, so that you do not go out of bounds:
for (int i = 1; i < argc; i++) {
Now you need to check that you have valid command line arguments:
if ((strcmp(argv[i], "/n") == 0) || ...
As you need an integer value for the case tests, use the second element of the argv[i]:
switch (argv[i][1]) {
I'm writing a code which needs to accept command line input. input validation is part of the marking criteria so I'm writing a piece of code to check that exactly one number is entered so I can put it in the main code later (adding an else case for invalid input to bug the user)
#include<stdio.h>
int main(int argc, char* argv[])
{
double a;
a = 75;
if(argc == 2);
{
sscanf(argv[1], "%lf", &a);
printf("%lf \n", a);
}
printf("%d% \n", argc);
return(0);
}
the program prints a when at least one argument is on the command line but it prints a segmentation fault when no argument is entered and still scans when more than one are.
help?
if(argc == 2);
is equivalent to
if(argc == 2)
;
You need to remove the ; to have the block after this be executed conditionally
if(argc == 2)
// ^ no trailing ; now
{
sscanf(argv[1], "%lf", &a); // now only executed if argc == 2
I am supposed to build a program that takes argv[1] and according to it transforms the characters into lower case or upper case.However I am stuck cuz C cant compare a pointer with a string.Any ideas on how to compare a pointer and a string,i dont want to compare them character by character.
Here is the code
#include <stdio.h>
#include <ctype.h>
#include <string.h>
int main (int argc,char *argv[])
{
char c;
if(argc!=2)
printf("Wrong use of program \n");
printf("The Format is Lower or Upper \n");
return -1;
if ((strcmp(argv[1],"Lower"))==0)
{
while((c=getchar())!=EOF)
{
printf("-");
putchar(tolower(c));
printf("\n");
}
}
if ((strcmp(argv[1],"Upper"))==0)
{
while((c=getchar())!=EOF)
{
printf("-");
putchar(toupper(c));
printf("\n");
}
}
if ((strcmp(argv[1],"Lower"))!=0 && ((strcmp(argv[1],"Upper"))!=0))
{
printf("Wrong use of program \n");
printf("The Format is Lower or Upper \n");
return -1;
}
return 0;
}
What you want to do is use the function strcmp or stricmp (for case insensitive).
Firstly, use strcmp which will return 0 if the char arrays match.
if (!strcmp(argv[1], "Lower"))
{
Secondly if more than one statement applies to an if condition, the statements must be encased in {}.
if (argc != 2)
{
printf("Wrong use of program \n");
printf("The Format is Lower or Upper \n");
return -1;
}