The main function should have parameters argc - argument count and argv - argument list as a string array.
int main(int argc, char *argv[])
{
// Program logic goes here
return 0;
}
To execute, assume the program expects three arguments from the user.
$ gcc program.c -o program
$ ./program arg1 arg2 arg3
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char *argv[]) {
if (argc != 3) {
printf("Usage: ./sum <num1> <num2>\n");
return 1;
}
int num1 = atoi(argv[1]);
int num2 = atoi(argv[2]);
int sum = num1 + num2;
printf("Sum: %d\n", sum);
return 0;
}
To execute, assume the file name is sum.c
$ gcc sum.c -o sum
$ ./sum 30 50
#include <stdio.h>
int main(int argc, char *argv[]) {
if (argc != 2) {
printf("Usage: ./file_reader <filename>\n");
return 1;
}
char *filename = argv[1];
FILE *file = fopen(filename, "r");
if (file == NULL) {
printf("Error opening file.\n");
return 1;
}
char line[256];
while (fgets(line, sizeof(line), file) != NULL) {
printf("%s", line);
}
fclose(file);
return 0;
}
To execute, assume the file name is file_reader.c
$ gcc file_reader.c -o file_reader
$ ./file_reader sample.txt