Coder Social home page Coder Social logo

my-notes-about-main-args's Introduction

Main arguments

(argc, argv)

Why do we use them?


Let’s say we want to create a C program that displays “Hello World !”, what should we do?

We can just do this and it works fine:

(print_str is just a function that takes a string and displays it using write)

Screen Shot 2022-07-30 at 3.33.21 PM.png

after compilation and execution this is the result :

Screen Shot 2022-07-30 at 3.32.36 PM.png

Nice, we did it. Now what if we want to display “HELLO WORLD!”, what should we do?

We need to open the file with a text editor, change the value of str, then recompile and execute it, isn’t that a little bit too much work? What if there’s a simpler way to do it? Actually, there is a way. Using main arguments.

First of all, let’s change the main function definition.

Now it takes 2 parameters :

Screen Shot 2022-07-30 at 4.14.00 PM.png

The first one is argc => its type is int The second one is argv => its type is char ** which means it's a pointer to char * and char * is a pointer to char in other words argv is a pointer to pointer to char

Screen Shot 2022-07-30 at 4.44.57 PM.png

argv

No how can argv make our previous task easier? As we’ve seen argv points to strings but what strings exactly, the answer is the parameters we enter alongside the executable file in the execution.

Screen Shot 2022-07-30 at 4.37.08 PM.png

argv allows us to access those parameters using indexes, argv[0] is the executable file argv[1] is the next parameter, and so on.

This is an example of how to use it

Screen Shot 2022-07-30 at 6.05.30 PM.png

The result after compilation and execution :

Screen Shot 2022-07-31 at 2.04.39 PM.png

The utility of argv here is to help us run multiple tests just by executing the code without modifying it.

argc

argc is just the number of arguments we entered in the execution including the executable file, which means argc can never be 0 because there's always an executable file but if we are talking about parameters we exclude the executable file.

Let's write a program that tells us whether we entered a parameter or not, what would it look like?

Screen Shot 2022-07-31 at 2.26.20 PM.png

After compilation and execution :

Screen Shot 2022-07-31 at 2.25.54 PM.png

This was a short explanation of one of the utilities of main args, and if you want to dive deeper into them you’ll find a third arg which is envp, and it’s quite interesting.

my-notes-about-main-args's People

Contributors

yabtaour avatar

Stargazers

Amine Reda Saf avatar luna avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.