Introduction

Tom Kelliher, CS 320

Jan. 26, 2005

Administrivia

Announcements

Assignment

Read 1.1--4.

Outline

  1. Syllabus.

  2. Terminal I/O in C.

  3. Accessing command-line arguments.

Coming Up

Structures, pointers, and memory allocation in C

Syllabus

Terminal I/O in C

  1. printf
    #include <stdio.h>
    
    int main()
    {
       /* Variable declarations must occur at the _start_ of a block. */
       int sum = 12;
       double min = 0.1, max = 5.5;
       char name[] = "Tom Kelliher";
    
       printf("I am a constant string.\n");
    
       printf("The sum is: %d\n", sum);
    
       printf("Min is: %g.  Max is: %g.\n", min, max);
    
       printf("Your name is %s.\n", name);
    
       return 0;
    }
    
    Refer to printf(3C) on phoenix. (man -s 3C printf)

  2. scanf
    #include <stdio.h>
    
    int main()
    {
       int i, age;
       double weight;
       char name[80];
    
       printf("Enter your age: ");
       scanf("%d", &age);
       printf("You entered %d.\n", age);
    
       printf("Enter sample weight: ");
       scanf("%lg", &weight);
       printf("You entered %g.\n", weight);
    
       printf("Enter your name: ");
       scanf("%s", name);
       printf("Your name is %s.\n", name);
    
       /* Eliminate whitespace following previous name. */
       while (getc(stdin) != '\n')
          ;
    
       printf("Enter your name: ");
       fgets(name, 80, stdin);
    
       /* Eliminate the newline following name */
       i = 0;
       while (name[i] != '\n')
          i++;
    
       name[i] = '\0';
    
       printf("Your name is %s.\n", name);
    
       return 0;
    }
    
    Refer to scanf(3C).

Command-Line Arguments

  1. Command-line arguments in Unix:
    foo arg1 arg2 arg3
    

  2. Command-line arguments in Visual C++: Open Project menu, choose Settings, choose Debug tab, enter arguments on Program Arguments line.

  3. Example:
    #include <stdio.h>
    #include <stdlib.h>
    
    int main(int argc, char *argv[])
    {
       int sum, current, i;
    
       if (argc <= 1)
       {
          printf("No arguments!\n");
          return 1;
       }
    
       sum = 0;
       for (i = 1; i < argc; i++)
       {
          current = atoi(argv[i]);
          sum += current;
          printf("Arg %d: %d\n",  i, current);
       }
    
       printf("\nThe sum is %d.\n", sum);
    
       return 0;
    }
    

Practice

  1. Creating a console application in Visual C++:
    1. Open the File menu and select New.

    2. Select Win 32 Console Application, fill-in Project Name and Location (folder).

    3. Select An empty project.

  2. Creating new source files: Open the File menu and select New. Choose either the C/C++ Header File or C++ Source File. Fill-in a file name (no extension).

  3. Practice program: Write a C program which accepts exactly three integer command-line arguments and prints the largest of them.



Thomas P. Kelliher
Tue Jan 25 14:44:35 EST 2005
Tom Kelliher