Here is source code of the C Program to Count the total number of words in the sentence using command line argument. The C program is successfully compiled and run on a Linux system. The program output is also shown below.
* C Program to Count the Total Number of Words in the Sentence
* using Command Line Argument
int main(int argc, char *argv)
int i = 0;
/* If no sentence is given in the command line */
if (argc == 1)
printf("\n No sentence given on command line");
printf("\nThe words in the sentence are:");
* From argv to argv[argc -1] calculate the number of arguments
for (i = 1;i < argc;i++)
printf("\n\nTotal number of words:");
printf(" %d", argc-1);
$ gcc arg1.c $ a.out Welcome to C Class The words in the sentence are: Welcome to C Class Total number of words: 4 a.out No sentence given on command line
Sanfoundry Global Education & Learning Series – 1000 C Programs.