#include <iostream>
using std::cout;
using std::endl;
int main(int argc, char* argv[])
{
cout << "argc = " << argc << endl;
for(int i = 0; i < argc; i++)
{cout << "argv[" << i << "] = " << argv[i] << endl;}
return 0;
}
/*
g++ CommandLineArgs.cpp -o CommandLineArgs
./CommandLineArgs
argc = 1
argv[0] = ./CommandLineArgs
./CommandLineArgs Hello!
argc = 2
argv[0] = ./CommandLineArgs
argv[1] = Hello!
./CommandLineArgs How are you?
argc = 4
argv[0] = ./CommandLineArgs
argv[1] = How
argv[2] = are
argv[3] = you?
*/
*****************************************************************************************
*****************************************************************************************
*****************************************************************************************
*****************************************************************************************
*****************************************************************************************
#include <stdio.h> // for printf()
#include <stdlib.h> // for atoi()
int main(int argc, char* argv[])
{
int i;
for(i = 1; i < argc; i++) // ignore argv[0], program name
{printf("%d\n", atoi(argv[i]));}
return 0;
}
/*
gcc argstoints.c -o argstoints
./argstoints // no args, prints nothing
./argstoints 10 010 0x10 // ignores octal and hex formats
10
10
0
./argstoints a b c // ignores ASCII
0
0
0
./argstoints .012 1.25 23.4E10 1.9 -0.9 // ignores fractional part
0
1
23
1
0
./argstoints 2147483647 -2147483648
2147483647 // INT_MAX // limits.h
-2147483648 // INT_MIN
./argstoints 2147483648 -2147483649 // 1 past limits
-2147483648 // INT_MIN (overflow, modulo 2 arithmetic)
2147483647 // INT_MAX (underflow)
./argstoints 2147483649 -2147483650 // 2 past limits
-2147483647 // INT_MIN + 1
2147483646 // INT_MAX - 1
./argstoints 12345678901234567890 -12345678901234567890
-1 // overflow, too big to handle
0 // underflow (20 digits)
*/
*****************************************************************************************
*****************************************************************************************
*****************************************************************************************
#include <iostream>
#include <cstdlib> // for atoi()
using std::cout;
using std::endl;
int main(int argc, char* argv[])
{
for(int i = 1; i < argc; i++) // ignore argv[0], program name
{cout << atoi(argv[i]) << endl;}
return 0;
}
/*
g++ ArgsToInts.cpp -o ArgsToInts
./ArgsToInts // no args, prints nothing
./ArgsToInts 10 010 0x10 // ignores octal and hex formats
10
10
0
./ArgsToInts a b c // ignores ASCII
0
0
0
./ArgsToInts .012 1.25 23.4E10 1.9 -0.9 // ignores fractional part
0
1
23
1
0
./ArgsToInts 2147483647 -2147483648
2147483647 // INT_MAX // climits or limits.h
-2147483648 // INT_MIN
./ArgsToInts 2147483648 -2147483649 // 1 past limits
-2147483648 // INT_MIN (overflow, modulo 2 arithmetic)
2147483647 // INT_MAX (underflow)
./ArgsToInts 2147483649 -2147483650 // 2 past limits
-2147483647 // INT_MIN + 1
2147483646 // INT_MAX - 1
./ArgsToInts 12345678901234567890 -12345678901234567890
-1 // overflow, too big to handle
0 // underflow (20 digits)
*/
*****************************************************************************************
*****************************************************************************************
*****************************************************************************************
*****************************************************************************************
*****************************************************************************************
Exercise 3-22. Create two new programs starting from ArgsToInts.cpp (see ch3-ArgsToInts) so they use atol() and atof(), respectively.
#include <iostream>
#include <cstdlib> // for atol()
using std::cout;
using std::endl;
int main(int argc, char* argv[])
{
for(int i = 1; i < argc; i++) // ignore argv[0], program name
{cout << atol(argv[i]) << endl;}
return 0;
}
/*
g++ ArgsToLongs.cpp -o ArgsToLongs
./ArgsToLongs // no args, prints nothing
./ArgsToLongs 10 010 0x10 // ignores octal and hex formats
10
10
0
./ArgsToLongs a b c // ignores ASCII
0
0
0
./ArgsToLongs .012 1.25 23.4E10 1.9 -0.9 // ignores fractional part
0
1
23
1
0
./ArgsToLongs 1234567890 9223372036854775807 -9223372036854775808
1234567890
9223372036854775807 // LONG_MAX // see climits or limits.h
-9223372036854775808 // LONG_MIN
./ArgsToLongs 9223372036854775808 -9223372036854775809 // 1 past limits
9223372036854775807 // LONG_MAX
-9223372036854775808 // LONG_MIN
*/
*****************************************************************************************
*****************************************************************************************
*****************************************************************************************
#include <iostream>
#include <cstdlib> // for atof()
using std::cout;
using std::endl;
int main(int argc, char* argv[])
{
for(int i = 1; i < argc; i++) // ignore argv[0], program name
{cout << atof(argv[i]) << endl;}
return 0;
}
/*
g++ ArgsToDoubles.cpp -o ArgsToDoubles
./ArgsToDoubles // no args, prints nothing
./ArgsToDoubles 10 010 0x10 // ignores octal, but not hex
10
10
16
./ArgsToDoubles a b c // ignores ASCII
0
0
0
./ArgsToDoubles 0xa 0xb 0xc // hex values
10
11
12
./ArgsToDoubles .012 1.25 23.4E10 1.9 -0.9
0.012
1.25
2.34e+11
1.9
-0.9
./ArgsToDoubles 12345678901234567890 -12345678901234567890
1.23457e+19
-1.23457e+19
*/