Reader small image

You're reading from  Learn C Programming. - Second Edition

Product typeBook
Published inAug 2022
PublisherPackt
ISBN-139781801078450
Edition2nd Edition
Right arrow
Author (1)
Jeff Szuhay
Jeff Szuhay
author image
Jeff Szuhay

Jeff Szuhay is the principal developer at QuarterTil2 which specializes in graphics-rich software chronographs for desktop environments. In his software career of over 35 years, he has engaged in a full range of development activities from systems analysis and systems performance tuning to application design, from initial development through full testing and final delivery. Throughout that time, he has taught computer applications and programming languages at various educational levels from elementary school students to university students, as well as developed and presented professional, on-site training.
Read more about Jeff Szuhay

Right arrow

Chapter 20: Getting Input from the Command Line

Up until this point, we have not read any input for any of our programs from any source. All program data has been hardcoded in the program itself. In this chapter, we will begin exploring programming input with one of the simplest available methods—inputting from the console's command line.

We will revisit the main() function with our added knowledge of function parameters and arrays of strings. We will then explore how to retrieve strings via an argument to the main() function.

The following topics will be covered in this chapter:

  • Understanding the two forms of main()
  • Understanding how argc and argv are related
  • Writing a program to retrieve values from argv and printing them
  • Understanding how to use getopt() for simple command-line processing

So, let's dive right in!

Technical requirements

Continue to use the tools you chose in the Technical requirements section of Chapter 1Running Hello, World!.

The source code for this chapter can be found at https://github.com/PacktPublishing/Learn-C-Programming-Second-Edition/tree/main/Chapter20.

Revisiting the main() function

The main() function is, as we have seen, the first place our program begins execution. Recall that before execution begins, various kinds of memory are allocated within our program space. Once the memory is allocated and initialized, the system calls main(), pretty much as we might call any other function. In this regard, main() is a function like any other.

The special features of main()

On the other hand, main() is not just another function in C. The main() function is a special C function with the following unique properties:

  • main() cannot be called by any other function.
  • main() is activated as the first function called when the program is invoked to begin execution.
  • When we return from the main() function, execution stops, and the program is terminated.
  • There are only two forms of the main() function prototype:
    • Has no arguments...

Using argc and argv

While we could give alternate names for the argc and argv parameter names, we will use these two names throughout this chapter for consistency.

When we invoke a program, we now see the following:

  • Memory is allocated in the program space.
  • Command-line arguments are processed into function parameters passed into main(), or ignored if those parameters are absent.
  • The execution begins with a call to main().

The first thing to note is that every argument from the command line is broken up into strings. A pointer to the beginning of each string is placed in argv, and the argc counter is incremented. In many cases, string input is sufficient without any further processing. We will explore converting string input into other values in the next chapter, Chapter 21Exploring Formatted Input.

The program name itself is always placed in argv[0]. Therefore, ...

Summary

We have explored the simplest way to provide input to our programs via the command line. We first specified how the main() function can receive arguments that contain the count and values of arguments given to the program. We saw how argc and argv are related, and how to access each argv string. A simple program to print out arguments given to it was provided for further experimentation. We noted how all arguments are passed into main() as strings. Once we access those arguments, we can perform further processing on them to alter the behavior of our program. Finally, a very simple command-line processor was provided to demonstrate the use of the getopt() C Standard Library function.

In the next chapter, we will explore a more comprehensive way to receive input from the user while a program is running. Just as printf() writes formatted data from program variables to the console (screen), the&...

Questions

  1. How many forms of main() are there?
  2. What does argv provide to our program?
  3. Is there a single standard command-line processor to parse and process argv?
lock icon
The rest of the chapter is locked
You have been reading a chapter from
Learn C Programming. - Second Edition
Published in: Aug 2022Publisher: PacktISBN-13: 9781801078450
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
undefined
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at €14.99/month. Cancel anytime

Author (1)

author image
Jeff Szuhay

Jeff Szuhay is the principal developer at QuarterTil2 which specializes in graphics-rich software chronographs for desktop environments. In his software career of over 35 years, he has engaged in a full range of development activities from systems analysis and systems performance tuning to application design, from initial development through full testing and final delivery. Throughout that time, he has taught computer applications and programming languages at various educational levels from elementary school students to university students, as well as developed and presented professional, on-site training.
Read more about Jeff Szuhay