// file: /data/courses/ece_1111/current/lectures/lecture_10 // // Description... // // local include files // #include "example.h" // function: main // // This is a simple program that prints to the terminal. // int main(int argc, const char** argv) { // define a floating-point variable that is set from the command line // float sum = atof(argv[1]); //printf("program name: %s, sum = %f\n", argv[0], sum); fprintf(stdout, "program name: %s, sum = %10.4f\n", argv[0], sum); // define an integer // long i = 27; int j = 9; int k = 27; char c = 'a'; char* str = (char*)"this is a string"; // formatted I/O - different format specifications // printf("easy: the value of i is %7d\n", i); printf("easy: the value of i is %10d\n", i); printf("easy: the value of i is %15d\n", i); fprintf(stdout, "hard: the value of i is %7d (j = %7d)\n", i, j); // define a floating-point number // sum = 27.2727272727; fprintf(stdout, "the value of sum is %f\n", sum); fprintf(stdout, "the value of sum is %10.4f\n", sum); fprintf(stdout, "the value of sum is %10.1f\n", sum); // doing math 'inline' // fprintf(stdout, "the value of sum is %f\n", sum + 999); fprintf(stdout, "the value of sum is %10.2f\n", (float)1.0); fprintf(stdout, "the value of sum is %10.2f\n", (float)999); fprintf(stdout, "the value of sum is %10.2f\n", sum); fprintf(stdout, "the value of sum is %15.4e\n", sum); fprintf(stdout, "i = %d, sum = %f\n", i, sum); fprintf(stdout, "i (%d), sum = (%f)\n", i, sum); fprintf(stdout, "********\n"); fprintf(stdout, "%s [%s]\n", "our first string", "today is Wednesday"); fprintf(stdout, "[%20.20s]\n", "1234567890"); fprintf(stdout, "[%20.20s]\n", "123"); fprintf(stdout, "[%.20s] [%.20s]\n", "1234567890", "123"); fprintf(stdout, "[%.20s]\n", "123"); fprintf(stdout, "[%s]\n", "1234567890"); fprintf(stdout, "hello world\n"); fprintf(stdout, "hello Joe\n"); fprintf(stdout, "my program name is [%s]\n", argv[0]); fprintf(stdout, "my first command line argument is [%s]\n", argv[1]); fprintf(stdout, "the number of arguments is [%d]\n", argc); // more advanced print using pointers // const char* ptr = "My name is Joe"; fprintf(stdout, "%s\n", ptr); // command line arguments // fprintf(stdout, "number of arguments = %d\n", argc); fprintf(stdout, "first command line argument: [%s]\n", argv[0]); fprintf(stdout, "second command line argument: [%s]\n", argv[1]); fprintf(stdout, "third command line argument: [%s]\n", argv[2]); // exit gracefully // return(0); }