Results 1 to 2 of 2

Thread: long integer question

  1. #1
    Join Date
    Jan 2012
    Beans
    161

    long integer question

    Lets say I have: long max_data, max_rows;How does this work? (If you need more of the code im looking at, please let me know.) max_data = (argc >= 4) ? strtol(argv[3], NULL, 10) : 512; max_rows = (argc >= 5) ? strtol(argv[4], NULL, 10) : 100;

  2. #2
    Join Date
    Aug 2011
    Location
    47°9′S 126°43W
    Beans
    2,172
    Distro
    Ubuntu 16.04 Xenial Xerus

    Re: long integer question

    You gave a ternary operator (? that evaluates a condition (argc >= 4), and if true return the item before the semicolon (strtol(argv[3], NULL, 10)) or otherwise returns the second item (512). So if you gave at least 4 arguments (so argv[3] exists) then it returns the conversion of the string in argv[3] to a long integer, interpreting it as a base 10 number. In other words this is shorthand for
    Code:
    long max_data;
    if (argc >= 4) {
        max_data=strtol(argv[3], NULL, 10)
    } else {
           max_data = 512
    }
    Or slightly better IMHO:

    Code:
    long max_data=512; // default value made more visible and unmissable
    if (argc >= 4) {
        max_data=strtol(argv[3], NULL, 10)
    }
    Warning: unless noted otherwise, code in my posts should be understood as "coding suggestions", and its use may require more neurones than the two necessary for Ctrl-C/Ctrl-V.

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •