Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
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.
Closed 2 years ago.
Improve this question
I have a pretty simple program:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int main(int argc, char *argv[]) {
// argc is amount of elements that the user inputs, check if 1234 isn't in code
if (strcmp(argv[argc-1],"1234" != 0)) {
exit(-1);
}
for (int i = 0; i < argc; i++) {
if (strcmp(argv[i],"-h")) {
// do whatever
printf("Hello world!");
} else if (strcmp(argv[i],"-f")) {
// argv[i+1] will be the file, print out the file to console
}
}
}
It allows the user to enter something, say ./my_kill -h 1234. If 1234 isn't the last thing, it's supposed to just exit. Then in the for loop, if -h is used it prints hello world. For some reason it is giving me a segmentation fault and I don't understand why.
You have a parenthesis in the wrong position.
if (strcmp(argv[argc-1],"1234" != 0))
should be
if (strcmp(argv[argc-1],"1234") != 0)
Also, the first thing you should do is check if there is any information inside of argv.
Related
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 2 years ago.
Improve this question
I am coding something and this should print "Good Job" but instead it is just saying "invalid lol,".
Here is my code:
#include <stdio.h>
#include <string.h>
fgets(line, 69, stdin);
if (strcmp(line, "stats") == 0)
{
printf("Good Job\n");
}
else
{
printf("Invalidlol\n");
{
I suspect the newline is getting added to your string. Try
if (strcmp(line, "stats\n") == 0)
For a quick fix
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
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.
Closed 2 years ago.
Improve this question
This is my program:
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
int main()
{
int a, b, i;
scanf("%d\n%d", &a, &b);
// Complete the code.
for(i=a;i<=b;i++)
{
if (i=1)
{
printf("one");
}
else if(i=2)
{
printf("two");
}
else if(i=3)
{
printf("three");
}
.
.
.
}
}
return 0;
}
it goes till 10.
Even if a=8 the i=1 condition is executed and doesn't terminate.
The output is oneoneoneoneoneoneoneoneone.....
To compare values, do :
if (i == 1)
With what you did :
if (i = 1)
You assign 1 to i and the if condition check if it succeded, which is true.
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
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.
Closed 6 years ago.
Improve this question
My program compiles successfully. It reads all the integer values but whenever I try to input the character , it gives segmentation fault error. I can't figure out what is the problem. Here is my code
#include <stdio.h>
struct soldier
{
int skill;
int superior;
};
struct soldier army[1000000];
int n;
int skillSum=0;
void findSkillSum(int);
int main()
{
int m,parent,child,s,update,i;
char command;
scanf("%d %d",&n,&m);
// struct soldier army[n+10];
for(i=1;i<=n;i++)
scanf("%d",&army[i].skill);
army[1].superior =0;
for(i=1;i<n;i++)
{
scanf("%d %d",&parent,&child);
army[child].superior = parent;
}
for(i=1;i<=m;i++)
{
scanf("%c",&command);
// s=command[2]-48;
if(command=='Q')
{
scanf("%d",&s);
findSkillSum(s);
printf("%d",skillSum);
printf("\n");
}
else
{
scanf("%d",&s);
scanf("%d",&update);
army[s].skill=update;
}
}
}
void findSkillSum(int s)
{
int i;
for (i=1;i<=n;i++)
{
if (army[i].superior==s)
{
skillSum+=army[i].skill;
findSkillSum(i);
}
}
}
A space should be added before %c to get the next non-whitespace character
scanf(" %c",&command);
Seeing that you've already solved the segmentation fault, I'll just give you a tip to understand what causes it.
GDB:
If you want to know what is provoking a segmentation fault, you just need to debug your program with gdb. That's easy as:
gdb -q ./program.out
This will start gdb, then just run the program with run command, and insert the input. At the moment I inserted a character insted than a number the program stoped with a SIGSEGV fault.
Program received signal SIGSEGV, Segmentation fault.
0x0000000000400637 in main () at 1.c:29
29 army[child].superior = parent;
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
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.
Closed 6 years ago.
Improve this question
while understanding the parameters of main function i.e, int argc, char* argv[]
i wrote a piece of code to understand these parameters.
#include <stdio.h>
int main(int argc,char*argv[])
{
printf("test\n");
printf("%d %c",argc,*argv[argc-1]);
return 0;
}
This prints
test
1 F
here I don't understand why there is F as output. I mean how this is executed to result in output as F ?
I read about these perameters and main function at here and here. But still I don't understand how these works.
please explain .
EDIT: as mentioned in comments if I change the code to
printf("%d %s",argc,argv[argc-1]);
Now i'm getting the whole path of the file F:\file path
so does it mean argv[0] is the location of the file in drive?
It is not defined in the C standard, but on Unix argv[0] is the name of the executable. Then argv[1] the first argument, etc. I think that this is also true, most of the time, on Microsoft's Dos and their Windowing OSes.
In simple words:
When you give your commandline the instruction to run your program, you can append some text, which can be accessed in your program.
#include <stdio.h>
int main(int argc,char*argv[])
{
printf("This is the path or name of your programm: ");
printf("%s\n", argv[0]);
if(argc > 1) {
printf("This is the first argument you gave your programm: ");
printf("%s\n", argv[1]);
}
if(argc > 2) {
printf("This is the second argument you gave your programm: ");
printf("%s\n", argv[2]);
}
return 0;
}
Try to run this example with:
<path_to_the_programm> Hallo Welt
You will see argc is an integer, which tells you how many arguments you gave the program. I hope it helped you.
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
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.
Closed 7 years ago.
Improve this question
I am trying to create a call to system using custom parameters. However I think that I am incorrectly malloc-ing the size of the final char*.
So I instead get a Failed to Open source file error during the system call. Am I doing something wrong in terms of syntax?
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main(void)
{
char program_name[] = "/usr/local/bin/some_program";
char argument_1[] = "foo";
char argument_2[] = "foo2";
char space[] = " ";
char *runProgram = malloc( strlen(program_name) + strlen(argument_1)+
strlen(argument_2) + 2*strlen(space) + 1);
strcpy(runProgram, program_name);
strcat(runProgram, space);
strcat(runProgram, argument_1);
strcat(runProgram, space);
strcat(runProgram, argument_2);
system(runProgram);
free(runProgram);
exit(0);
}
When I run your code, your string appears to contain exactly what it needs to to be called. I do have a suggestion to simplify building it though:
...
int ret=0;
int len = strlen(program_name) + strlen(argument_1)+ strlen(argument_2) + 2*strlen(space) + 1;
char *runProgram = malloc( len);
ret = snprintf(runProgram, len, "%s %s %s", program_name, argument_1, argument_2);
if(ret < 0)
{
//handle if truncation occurred (returns -1 for truncation)
}
if(ret >= len)
{
//use runProgram buffer
}
system(runProgram);
free(runProgram);
//exit(0);
return 0;
...
Have you verified the chmod settings on the file to be executed are correct?
chmod +x /usr/local/bin/some_program
EDIT:
To address concern in comments regarding variations in implementation/documentation for snprintf(). When discussing standard C functions, the hope would be that implementations of the same function would be equal across platforms, but for snprintf(), it appears that some implementations are more equal than others...
1) snprintf some have referred to this as Microsoft's broken implementation.
2) snprintf from skrenta.com
3) snprintf from opengroup.org.
4) snprintf from linux.die.net, (includes glibc references, macros and other comments)
Your code should work fine, printf("cmd is '%s'\n", runProgram); to check if generated command can be run in shell and produces expected results:
command string is correct
/usr/local/bin/some_program exists and has correct permissions
some_program does not fail