0% found this document useful (0 votes)
11 views3 pages

C Assigement

Uploaded by

amitmaurya70688
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views3 pages

C Assigement

Uploaded by

amitmaurya70688
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

Question - Build a command-line interface application in C that interacts

with the user. The program should process command-line arguments,


handle user input, and provide meaningful output based on the user's
choices.

Answer - Building a Command-Line Interface Application in C

Here's a simple implementation of a command-line interface application in


C that interacts with the user. This program will process command-line
arguments, handle user input, and provide meaningful output based on
the user's choices.

Program Overview

The program will have the following features:

Process command-line arguments

Display a menu to the user

Handle user input

Provide meaningful output based on the user's choices

Code c

#include <stdio.h>

#include <stdlib.h>

#include <string.h>

// Function to display the menu

void displayMenu() {

printf("Command-Line Interface Application\n");

printf("-------------------------------\n");

printf("1. Display Command-Line Arguments\n");

printf("2. Ask User for Input\n");

printf("3. Exit\n");

printf("-------------------------------\n");

// Function to display command-line arguments

void displayArguments(int argc, char *argv[]) {

printf("Command-Line Arguments:\n");
for (int i = 0; i < argc; i++) {

printf("Argument %d: %s\n", i, argv[i]);

// Function to ask user for input

void askUserInput() {

char userInput[100];

printf("Please enter some text: ");

fgets(userInput, sizeof(userInput), stdin);

userInput[strcspn(userInput, "\n")] = 0; // Remove the newline


character

printf("You entered: %s\n", userInput);

int main(int argc, char *argv[]) {

int choice;

while (1) {

displayMenu();

printf("Enter your choice: ");

scanf("%d", &choice);

getchar(); // Consume the newline character

switch (choice) {

case 1:

displayArguments(argc, argv);

break;

case 2:

askUserInput();

break;

case 3:

printf("Exiting...\n");
return 0;

default:

printf("Invalid choice. Please try again.\n");

return 0;

Explanation

This program uses a simple while loop to continuously display a menu to


the user and handle their input. The display Menu function displays the
menu, the display Arguments function displays the command-line
arguments, and the ask User Input function asks the user for input.

The main function processes the command-line arguments and handles


the user's input based on their choice.

Compilation and Execution

To compile and execute the program, save it in a file named cli_app.c and
use the following commands:

gcc cli_app.c -o cli_app

./cli_app

You can pass command-line arguments to the program like this:

./cli_app arg1 arg2 arg3

This will display the command-line arguments when you choose option 1
from the menu.

You might also like