Error in handling command line arguments in C [duplicate] - c

This question already has answers here:
How to read asterisk( * ) as an argument from the command line in C [duplicate]
(3 answers)
argv: Sanitizing wildcards
(2 answers)
Closed 4 years ago.
I am trying to write a C code that will take three arguments from the command line in the form of {num1} {operator} {num2}. It is working fine in case of all operator symbols other than *.
This is the code to verify this:
#include <stdio.h>
int main(int argc, char const *argv[]) {
printf("%d\n", argc);
}
I am compiling this as gcc test.c -o test.
Then when I run it using ./test 5 + 9, it gives the expected output 4. But then when I run ./test 5 * 9, it gives 25 as output. What am I doing wrong here?

The * is expanded by the shell into a list of all files in the current directory. If you don't want shell-expansion you need to (single) quote the argument:
./test 5 '*' 9

Related

How do you add command line arguments for specific use to your terminal in Ubuntu? [duplicate]

This question already has answers here:
Parsing command-line arguments in C
(14 answers)
Closed 9 months ago.
I'm a bit confused as to how I could add certain parameters in the Ubuntu terminal when using the GNU C compiler to compile the program. For example:
gcc -o question question.c
./question -e -f someFile.txt
where -f would open this specific file 'someFile.txt' (any file) and -e would let me access a specific function inside my code.
I tried this with void main(int argc, char* argv[]) but with that I would have to specify the number of arguments I would have to pass i.e. ./question 3 -e -f resources.txt, which I would not like to do.
Is there any other way I could attempt this?
Thank you in advance!!!
#include <stdio.h>
int main(int argc, char **argv) {
printf("program was supplied %d arguments.\n", argc - 1);
for (int k = 0; k < argc; k++) printf("argv[%d] is %s\n", k, argv[k]);
if (!strcmp(argv[1], "-e")) printf("The first argument provided is -e\n");
}
For advanced usage you may want to read about getopt

Redirection ambiguity [duplicate]

This question already has answers here:
printf anomaly after "fork()"
(3 answers)
Closed 4 years ago.
I have a file named test.c (Its contents are given below)
#include <stdio.h>
#include <unistd.h>
int main()
{
printf("T\n");
fork();
printf("F\n");
}
The expected result is:
T
F
F
Compiling with gcc and executing ./a.out ,the output is:
T
F
F
which matches the expected answer.
But ambiguity arises when I redirect the output to another file.
$ ./a.out > Output.txt
The Output.txt has the following data:
T
F
T
F
Why is there an additional T in my Output.txt file when I use redirectors?
1) I've check this in multiple PC's running on ubuntu with gcc installed.
2) Tried deleting the Output.txt and moving all the files to a different location, but this still persists.
P.s. this works fine without the redirector.
I think it's because of the buffer, give a try with the follow code:
#include <stdio.h>
#include <unistd.h>
int main() {
printf("T\n");
fflush(stdout);
fork();
printf("F\n");
}

syntax error near unexpected token [duplicate]

This question already has answers here:
Beginner: syntax error before int main ()
(2 answers)
Closed 5 years ago.
Tish is the program:
#include <stdio.h>
int main()
{
printf("77777");
return 0;
}
yaki#ubuntu:~/Desktop/yakima$ gcc yakima.c -o yakima.o
yaki#ubuntu:~/Desktop/yakima$ ./yakima.c
This is the error:
./yakima.c: line 3: syntax error near unexpected token `('
./yakima.c: line 3: `int main()'
What can you do with this problem?
You are trying to execute source file.
After you create object file you have to link object file(s) to binary like
gcc -c yakima.c -o yakima.o
gcc yakima.o -o yakima
and execute binary
./yakima

Weird behavior when passing * as a command line parameter on linux [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about programming within the scope defined in the help center.
Closed 9 years ago.
Improve this question
I have this very simple c program:
#include <stdio.h>
int main (int argc, char ** argv){
printf ("%s\n",argv[1]);
}
When running it on Linux/bash like so:
./a.out *
I get the following output:
a.c
why?
Because * is a glob character that expands to the list of files in the current directory.
If you want to pass a literal * you will need to quote or escape it:
./a.out '*'
I didn't know that, but when running a command line that has a glob character , such as * or ?, the command line interpreter first expands the character and only then run the program.
For example, if your program is:
#include <stdio.h>
int main (int argc, char ** argv){
int i;
printf ("argc=%d\n",argc);
for (i=0;i<argc;i++){
printf("%d: %s\n",i,argv[i]);
}
}
and you run it like so:
./a.out *
, then the output will be:
argc=4
0: ./a.out
1: a.c
2: a.c~
3: a.out
Of course, the output will depend on the content of the current directory.

cpp preprocessor output not able to understand? [duplicate]

This question already has answers here:
What is the meaning of lines starting with a hash sign and number like '# 1 "a.c"' in the gcc preprocessor output?
(3 answers)
Closed 2 years ago.
Sorry if my question is very basic. I would like to understand the output produced by the preprocessor cpp. Let's say i have a very basic following program.
#include <stdio.h>
#include <stdlib.h>
int x=100;
int main ()
{
printf ("\n Welcome..\n");
}
I execute the following command.
cpp main.c main.i
in main.i
# 1 "/usr/include/stdio.h" 1 3 4
What is the meaning of the above line ?..
The gcc documentation explains the C preprocessor output aptly.
Here are the relevant sections:
The output from the C preprocessor looks much like the input, except that all preprocessing directive lines have been replaced with blank lines and all comments with spaces. Long runs of blank lines are discarded.
Source file name and line number information is conveyed by lines of the form
# linenum filename flags
These are called linemarkers. They are inserted as needed into the output (but never within a string or character constant). They mean that the following line originated in file filename at line linenum. filename will never contain any non-printing characters; they are replaced with octal escape sequences.
After the file name comes zero or more flags, which are 1, 2, 3, or 4. If there are multiple flags, spaces separate them. Here is what the flags mean:
1 This indicates the start of a new file.
2
This indicates returning to a file (after having included another file).
3
This indicates that the following text comes from a system header file, so certain warnings should be suppressed.
4
This indicates that the following text should be treated as being wrapped in an implicit extern "C" block.

Resources