Lecture 8: Getting ready for program 1: unsigned chars and command line inputs
Reading command line inputs
Key points:
- To read command line input, define main with two input arguments: number of command line arguments and list of command line arguments, e.g.
int main(int argc, char** argv) { }
- Here
argc
is the number of command line argument sandargv
is an array of character pointers containing all of the arguments. - To read one argument as an integer, use
atoi(argv[1])
.atoi
is part of the standard library, so use#include <stdlib.h>
Unsigned char data type
Key points:
char
data type is actually one of many integer data types (see “Intege Types” here: https://www.tutorialspoint.com/cprogramming/c_data_types.htm)unsigned char
stores integers 0 to 255 using only 1 byte- use
%hhu
placeholder in strings forprintf
andscanf