Skip to main content

Command Palette

Search for a command to run...

main Function Variants, Unused Variables & Real-World Use Cases

Published
4 min read
D

Heya! 👋 I love helping people, and one of the best ways I do this is by sharing my knowledge and experiences. My journey reflects the power of growth and transformation, and I’m here to document and share it with you.

I started as a pharmacist, practicing at a tertiary hospital in the Northern Region of Ghana. There, I saw firsthand the challenges in healthcare delivery and became fascinated by how technology could offer solutions. This sparked my interest in digital health, a field I believe holds the key to revolutionizing healthcare.

Determined to contribute, I taught myself programming, mastering tools like HTML, CSS, JavaScript, React, PHP, and more. But I craved deeper knowledge and practical experience. That’s when I joined the ALX Software Engineering program, which became a turning point. Spending over 70 hours a week learning, coding, and collaborating, I transitioned fully into tech.

Today, I am a Software Engineer and Digital Health Solutions Architect, building and contributing to innovative digital health solutions. I combine my healthcare expertise with technical skills to create impactful tools that solve real-world problems in health delivery.

Imposter syndrome has been part of my journey, but I’ve learned to embrace it as a sign of growth. Livestreaming my learning process, receiving feedback, and building in public have been crucial in overcoming self-doubt. Each experience has strengthened my belief in showing up, staying consistent, and growing through challenges.

Through this platform, I document my lessons, challenges, and successes to inspire and guide others—whether you’re transitioning careers, exploring digital health, or diving into software development.

I believe in accountability and the value of shared growth. Your feedback keeps me grounded and motivated to continue this journey. Let’s connect, learn, and grow together! 🚀

So far, you’ve learned:

  • How programs receive input (argc, argv)

  • How to access and process arguments

Now we complete the picture.

This lesson answers three important questions:

  • Why does main sometimes look different?

  • What do we do with unused variables?

  • Where is this actually used in real life?

1. Two Ways to Write main

You may see main written in two ways:

Version 1

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

Version 2

int main(int argc, char **argv)

2. Are They Different?

No.

They are exactly the same.

Why?

Because in C:

char *argv[]  ≡  char **argv

Both represent:

An array of strings (arguments)

Which One Should You Use?

Use whichever is clearer to you.

Most beginners prefer:

char *argv[]

because it clearly shows that argv is an array.

3. Handling Unused Variables

Sometimes you won’t use argc or argv.

Example:

int main(int argc, char *argv[])
{
    return 0;
}

The compiler may warn you:

unused parameter 'argc'
unused parameter 'argv'

4. How to Fix This

Option 1: Use (void)

(void)argc;
(void)argv;

Option 2: Use Attribute

int main(int argc __attribute__((unused)), char *argv[] __attribute__((unused)))

Which Should You Use?

👉 For beginners:

(void)argc;
(void)argv;

Simple and clear.

5. Why This Matters

Your compiler is strict (especially with flags like -Wall -Werror).

Warnings can stop your program from compiling.

So you must handle unused variables properly.

6. Real-World Use Cases of argc and argv

Now let’s connect everything to real programs.

🔹 1. File Processing

Example:

./program input.txt

Your program reads the file name from argv.

🔹 2. Command-Line Tools

You already use tools like:

gcc file.c -o output

Here:

  • file.c

  • -o

  • output

are all arguments.

🔹 3. Flags and Options

Example:

./program -v

Your program can behave differently based on input.

🔹 4. Simple Calculators

Example:

./program 5 10

Your program can process values passed from the terminal.

7. Connecting to Your Project

From your project tasks , you will need to:

  • access the program name

  • count arguments

  • loop through arguments

  • process user input

  • validate inputs

8. Thinking Like a Programmer

Before writing code, ask:

1. How many arguments should I expect?

argc tells you this

2. What should happen if they are missing?

handle errors properly

3. What type of data am I receiving?

all arguments are strings

4. Do I need to convert the input?

use atoi() or similar

9. Common Beginner Mistakes

❌ Forgetting argv[0] is the program name

Students often assume:

argv[0] = first argument ❌

Correct:

argv[0] = program name

❌ Not checking argc

Accessing arguments blindly can crash your program.

❌ Ignoring compiler warnings

Warnings matter, especially with strict compilation flags.

10. Practice Thinking

Try to reason through these:

  1. If you run:

    ./program start stop
    

    What is argc?

  2. What is stored in argv[1]?

  3. How would your program behave differently based on input?

Key Ideas to Remember

  • main has two valid forms

  • argc counts arguments

  • argv stores arguments

  • Unused variables must be handled properly

  • Command-line arguments make programs dynamic

Final Thoughts

This week marks an important shift.

Before, your programs were:

fixed and predictable

Now, they become:

dynamic and user-driven

You are no longer just writing code.

You are building programs that respond to users — just like real tools.

What Comes Next

You now have everything you need to approach the project.

Focus on:

  • understanding input

  • validating arguments

  • structuring your program properly

Take your time.

This is where your programs start to feel like real software.

C Programming

Part 4 of 40

From today, I will be starting lessons on C programming in my ALX Software Engineering class and I look forward to sharing with you everything I learn through this series.

Up next

Working With Command-Line Arguments in Practice

In the previous lesson, you learned: What command-line arguments are What argc and argv mean How programs receive input from the terminal Now we move to the practical side: How do we actually us