Thread: How to pass float type argument from the command line

  1. #1
    Registered User 3Nex's Avatar
    Join Date
    May 2010
    Location
    Croatia
    Posts
    13

    How to pass float type argument from the command line

    I want to be able to call my program as ./program 2.5 and then read that argument from my main function into a variable and have the variable's value to be 2.5.

    All I get from the internets are either confusing examples or single character (instead of float) examples. Could you post a sample code for this plz?

  2. #2
    Registered User
    Join Date
    May 2010
    Location
    Naypyidaw
    Posts
    1,314
    Code:
    int main(int argc,char *argv[] ) {
       if( argc > 1 ) {
         double x;
         x = atof(argv[1]);  // alternative strtod
       }
    
    
      return 0;
    }

  3. #3
    Registered User 3Nex's Avatar
    Join Date
    May 2010
    Location
    Croatia
    Posts
    13
    Oh it works
    Thank you!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Model Rocket Altitude predictor...
    By kalor_alros in forum C++ Programming
    Replies: 11
    Last Post: 09-04-2009, 12:27 AM
  2. Can you check what is wrong with this code
    By Ron in forum C++ Programming
    Replies: 4
    Last Post: 08-01-2008, 10:59 PM
  3. Replies: 28
    Last Post: 07-16-2006, 11:35 PM
  4. ras.h errors
    By Trent_Easton in forum Windows Programming
    Replies: 8
    Last Post: 07-15-2005, 10:52 PM
  5. Could somebody please help me with this C program
    By brett73 in forum C Programming
    Replies: 6
    Last Post: 11-25-2004, 02:19 AM