strlen(*argv) produces an unusual result - c

I ran the following just for fun, but cannot account for the result. Assume ./test WTF? was run at the command line, and the output produced was
WTF? 6 2. Why is there such a vast discrepancy between the reported value of argc (2 - as expected) and strlen(*argv), which came up as 6. I know an array of strings isn't exactly what strlen is looking for, however, I thought the value it produced (if it produced anything) would be reasonably close to argc. Thoughts?
#include <stdio.h>
#include <ctype.h>
#include <string.h>
int main(int argc, char**argv)
{
for (int i = 1; i < argc; i++)
{
for (int j = 0; j < strlen(argv[i]); j++)
{
printf("%c", argv[i][j]);
}
printf(" ");
}
printf("\t%lu\t%d", strlen(*argv), argc);
printf("\n");
}

Nothing wrong here. The argv array contains two strings -
program name as invoked --> "./test"
"WTF?".
So in your example strlen(argv[0]) will print 6 and strlen(argv[1]) will print 4.

For example, if you run gcc -o myprog myprog.c in command line, you will get the followings:
argc
4
argv[0]
gcc
argv[1]
-o
argv[2]
myprog
argv[3]
myprog.c
Therefore, for your case you will get 6 (length of ./test for argv[0]) instead of 4 (length of WTF? argv[1]) as you run ./test WTF? in command line.

Related

What if there are different numbers of command line arguments; how can I deal with it in C?

// test.c
int main(int argc, char* argv[])
{
printf("%s \n", argv[1]);
printf("%s \n", argv[2]);
}
what if I wanna enter
gcc -o test test.c
./test gs -ef
or
./test ls
and so on
When I put in only one argument (ex. ./test date) there is an error message like this (Segmentation fault (core dumped))
When there can come two arguments or one argument in kind like this multiple situations,
How can I code that doesn't print error messages?
argc: Number of arguments passed.
argv: Two-dimensional array that has the arguments passed.
Note that the zeroth position in argv is reserved for the program name, so for example you compiled using gcc -o test test.c, argv[0] will contain test. You could then use argc to loop through argv like so:
while (argc >= 0) {
printf("%s \n", argv[argc]);
argc--;
}
This will of cause print the list of arguments backwards ending with the program name.
Use the argc argument to check the number of arguments and use elements of argv only if there are enough arguments.
#include <stdio.h>
int main(int argc, char* argv[])
{
if (argc > 1) printf("%s \n", argv[1]);
if (argc > 2) printf("%s \n", argv[2]);
}

Operator == doesn't work in C

I'm getting a weird error while writing my program in C.
Here is my code:
#include <stdio.h>
#include <stdlib.h>
#include "defs.h"
int main (int argc, char* argv[])
{
int boardSize = atoi(argv[2]);
int generations = atoi(argv[4]);
int gamesort = atoi(argv[1]);
printf("2 is %d 1 is %d 4 is %d name of file is %s \n",boardSize,gamesort,generations,argv[3]);
if (1==1)
{
printf("yes");
ZeroPlayersGame(boardSize, generations,argv[3]);
}
else//(gamesort==2)
{
TwoPlayersGame(boardSize, generations,argv[3]);
}
return 0;
}
And here is the error im getting from the Terminal:
ise#ubuntu:~/Desktop$ make
gcc -c main.c defs.c gameIO.c zeroPlayer.c twoPlayer.c
gcc gameIO.o defs.o zeroPlayer.o main.o twoPlayer.o -o prog
ise#ubuntu:~/Desktop$ ./prog 1 2 "l.txt" 3
2 is 2 1 is 1 4 is 3 name of file is l.txt
Segmentation fault (core dumped)
Very strange, as you can see my program dosen't enter my first "if",
but you can see it prints the line before the if statment.
thanks for any help!
I think your problem is not related to your question title "Operator == doesn't work in C". As seen in your output, printf works well and there may be possibly a problem in functions "ZeroPlayersGame" and "TwoPlayersGame". If you gave us more information about these two functions, solving the problem would be easier.
Also, using "1==1" in logical statement is so weird. Why don't you eliminate it and write your statement just after the printf statement. Because "1==1" is always true.

Change something in C program with Shell Script

Is it possible to write a script to run this code for different values of A;
#include <stdio.h>
#define A 3
int main (){
printf("In this version A = %d\n", A);
return(0);
}
I guess something like for loop?
Is it possible to write a script to run this code for different values of A;
Not as it is because the macro A has a fixed value defined in your code. Instead you can pass the value as an argument:
#include <stdio.h>
int main(int argc, char **argv){
if(argc == 2) {
printf("In this version A = %s\n", argv[1]);
}
return 0;
}
(The code doesn't check if its input is an integer -- which you can test if necessary).
and you can run it via script. For example, compile the above (gcc -Wall -Wextra test.c -o test) using a for loop of bash:
$ for ((i = 0; i < 10; i++)); do ./test $i; done
In this version A = 0
In this version A = 1
In this version A = 2
In this version A = 3
In this version A = 4
In this version A = 5
In this version A = 6
In this version A = 7
In this version A = 8
In this version A = 9
$
No. But you can make A a command line arg:
#include <stdio.h>
int main (int argc, char *argv[]) {
int a;
if (argc != 2 || sscanf(argv[1], "%d", &a) != 1) return 1;
printf("In this version A = %d\n", a);
return 0;
}
Compile to a binary named foo, then
foo 42
will print
In this version A = 42
You can also compile different versions by defining A in the compilation command line. From your original program, remove the #define. Then
gcc -DA=42 foo.c -o foo
./foo
will print the same as above.
DO you need run program repeated from script? why not to make program that accepts arguments from command line?
1)The main() function actually takes arguments, you can compile program once and pass different parameters, as shown in answers above
2) If you need to change some code parameters from make script, I'd say, create separate header that would contain defines and write script that would echo into that file (> for start, >> to continue writing).
3) Alternative way you can call you compiler with flag that would be equal to #define macro-command. For gcc it's -D, for example -DA=3 instead of #define A 3.
Most programs use makefile to be compiled. For that case you can script make file to use 2) or 3) Former is preferable because you do not need to pass that argument to all compilation targets, reducing time or re-compiling. There are tools for more advanced manipulations, like autoconf.

Read an integer from stdin (in C)

I want to write a C program that takes as input an integer, and outputs its square. Here is what I have tried.
However,
./a.out < 3 outputs 32768, and
./a.out < 4 also outputs 32768.
Where am I wrong? Thanks.
#include <stdio.h>
int main(){
int myInt;
scanf("%d", &myInt);
printf("%d\n",myInt*myInt);
}
It looks like what you're trying to do is
echo 4 | ./a.out
the syntax for < is
program < input_file
whereas | is
command_with_output | program
./a.out < 4
This tries to read the file named 4 and use it's content as input to a.out You can do it whichever way, but understand the < operator isn't for inputting the character you type quite literally.
One way to do this would be:
echo "4" > 4
./a.out < 4
I just run your program and its works great.
If you want the program receive an integer input you should use argc , argv as folowed and not use scanf.
*The code for argc argv: *
#include <stdio.h>
#include <stdlib.h>
int main(int argc , char** argv)
{
int myInt;
myInt = atoi(argv[1]);
printf("%d\n",myInt*myInt);
}
atoi - convert char* to integer.
If you want to run the program and then insert an integer, you did it right!
you can read about atoi
To run this program you should comile and run from terminal:
gcc a.c -o a
./a 3
and you will receive:
9
On the right hand side of "<", there should be a file containing the input.
try this thing:
$ echo "3" > foo
$ ./a.out < foo
Read this for more information (Specially section 5.1.2.2):
http://www.tldp.org/LDP/intro-linux/html/chap_05.html

How do I find out how many arguments are being passed

I am writing some C code on Linux. I want to loop through the *argv[] parameter being passed to main, but I don't know how to stop before getting a seg fault.
i = 0;
while (i < sizeof(argv)) {
printf("%s\n", argv[i]);
i ++;
}
This produces a seg fault because sizeof always returns 8. How do I get the actual number of elements in argv, or apply some kind of test to stop at the end of argv?
The first argument.. argc should already have the CLI argument count.. Any reason why that isn't used?
int main(int argc, char** argv)
{
if(argc > 1)
}
The first argument of main is argc which is the number of arguments passed to your program. This is at least 1, which is the name of the executable.
#include <stdio.h>
int main(int argc, char *argv[])
{
printf("%d\n", argc);
return 0;
}
Invoked as:
$ ./a.out
1
$ ./a.out 1 2 3 4
5
$ ./a.out 1 2 3
4
argc contains the count of parameters in argv.

Resources