Hands-on C: argc and argv from main() function explained

Quick Intro

Learning C is is a good way to take our understanding of Operating Systems (OS) to the next level as it touches low-level structures that other languages such as Python often abstract.

I'm covering C here not only because BIG-IP runs on top of Linux but also because understanding how the OS works can help us troubleshoot more complex issues.

Linux has been gaining popularity and DevOps movement certainly gave it a boost as most DevOps tools run natively on Linux.

As C is the nuts and bolts of Linux 'engine', it is also no surprise that we see more Platform (DevOps) Engineers interested in learning C to extend their reach when it comes to diagnosing problems and to have a more in-depth understanding of Linux.

In this article, I'm going to use a simple Hello World in C but those who already had exposure to C will also benefit from understanding more thoroughly the default arguments (argc and argv) in main() function by running the code themselves.

If you already know something about C, you can skip to last section and test the code yourself.

Hello World

This is a hello world in Python:

This is a typical hello world in C:

The main() function is the way we tell C where the execution of our code should start.

It is the way C language signals processor where it should look for the main piece of code to run once our program is loaded in memory.

The printf() function formats and print string characters to standard output.

C doesn't have an interpreter like Python as it's a compiled language.

Because of this, in order to execute above code we need to compile it first, i.e. create the executable file that has the binary code ready to run.

How to Compile our Hello World

We use gcc command with -o parameter pointing to the name of the executable file name.

The gcc command compiles our code and create the executable helloworld.

It's a binary file ready to be executed.

If we need to change our code, we need to re-compile our code and create another executable file.

Understanding #include <library_name>

Adding libraries in C is similar to other programming languages.

Libraries add capabilities to your code where you can find ready-to-use functions that do something that you might need to use.

C libraries end in *.h (short for header) and compiler searches for them in /usr/include path by default on Linux.

In this particular case here, we only added the stdio.h library which is the standard C I/O library with file reading/writing/streaming handling capabilities.

There are also others we might occasionally bump into in most programs:

  • unistd.h
     : The Unix standard library. It has utilities for interacting with the kernel such as system calls.
  • stdlib.h
     : The C standard library with many useful utilities for general data type conversion, memory allocation, etc.
  • ctype.h
     : Character library with for char conversion
  • sys/types.h
     : System types library which contains the most definitions for base types and structures of Linux systems.
  • string.h
     : String library for C string manipulation.

Why does main function have argc and argv?

argc = number of arguments passed when we execute program (integer)

argv = list of arguments we passed when we executed program (array)

Simple answer is that this is useful stuff to have natively.

It's extensively used in command line tools, for example.

Let's say we're creating a command line tool and we need to retrieve the 2nd argument passed to our program.

You can just retrieve it directly from main() function by calling argv[2].

No need to create an extra function for that.

Let's use this simple program as an example:

It should print whatever arguments I pass to it:

Notice that by default, the first argument is always the program's name.

Even if I don't pass any arguments that's still the case:

We can conclude that every program will have at least one argument which is the name of the program itself.

Updated Jun 06, 2023
Version 2.0

Was this article helpful?