The Code...
/* File: scanf1.c * Created by: Daniel J. Hood * Created on: Thursday, October 24, 2002 * SSN: xxx-xx-xxxx * Section: 0101 & 0102 * Email: dhood2@cs.umbc.edu * * Description: Basic scanf example */ #include <stdio.h> #define LENGTH 80 int main () { int myInt; float myFloat; double myDouble; char myString[LENGTH]; int num1, num2, num3, num4; /* read in data */ printf("\nScan in Data\n------------\n"); printf("Enter int:\n"); num1 = scanf("%d", &myInt); printf("Enter float:\n"); num2 = scanf("%f", &myFloat); printf("Enter double:\n"); num3 = scanf("%lf", &myDouble); printf("Enter string:\n"); num4 = scanf("%s", myString); /* no & */ /* print out data */ printf("\nPrint out Data\n--------------\n"); printf("int: %d, num read: %d\n", myInt, num1); printf("float: %f, num read: %d\n", myFloat, num2); printf("double: %f, num read: %d\n", myDouble, num3); printf("string: %s, num read: %d\n\n", myString, num4); /* exit without error */ return 0; }
The Output...
linux1-(9:21am): gcc -Wall -ansi scanf1.c linux1-(9:21am): a.out Scan in Data ------------ Enter int: 12345 Enter float: 12345.67890 Enter double: 1234567890.12345 Enter string: HelloWorld Print out Data -------------- int: 12345, num read: 1 float: 12345.678711, num read: 1 double: 1234567890.123450, num read: 1 string: HelloWorld, num read: 1 linux1-(9:22am): a.out Scan in Data ------------ Enter int: a Enter float: Enter double: Enter string: Print out Data -------------- int: 134518764, num read: 0 float: 2.332875, num read: 0 double: 0.000000, num read: 0 string: a, num read: 1 linux1-(9:22am): a.out Scan in Data ------------ Enter int: 123 Enter float: 123.456 Enter double: 123456.7890 Enter string: Hello World Print out Data -------------- int: 123, num read: 1 float: 123.456001, num read: 1 double: 123456.789000, num read: 1 string: Hello, num read: 1 linux1-(9:25am): a.out Scan in Data ------------ Enter int: /* Note: It does not show, but I pressed Control-D */ Enter float: /* to simulate EOF for each of these 4 scanfs */ Enter double: Enter string: Print out Data -------------- int: 134518764, num read: -1 float: 2.332875, num read: -1 double: 0.000000, num read: -1 string: m@s@, num read: -1 linux1-(9:25am):
Some Observations...
The Code...
/* File: scanf2.c * Created by: Daniel J. Hood * Created on: Thursday, October 24, 2002 * SSN: xxx-xx-xxxx * Section: 0101 & 0102 * Email: dhood2@cs.umbc.edu * * Description: Another basic scanf example */ #include <stdio.h> int main () { int first, middle, last; int read; printf("\nPlease enter a Social Security Number in the format: nnn-nn-nnnn\n"); printf("Where n is a number between 0 and 9\n"); /* read in data */ printf("\nScan in Data\n------------\n"); printf("Enter SSN: "); read = scanf("%d-%d-%d", &first, &middle, &last); /* print out data */ printf("\nPrint out Data\n--------------\n"); printf("SSN: %d-%d-%d, num read: %d\n\n", first, middle, last, read); /* exit without error */ return 0; }
The Output...
linux1-(9:41am): gcc -Wall -ansi scanf2.c linux1-(9:41am): a.out Please enter a Social Security Number in the format: nnn-nn-nnnn Where n is a number between 0 and 9 Scan in Data ------------ Enter SSN: 123-45-6789 Print out Data -------------- SSN: 123-45-6789, num read: 3 linux1-(9:41am): a.out Please enter a Social Security Number in the format: nnn-nn-nnnn Where n is a number between 0 and 9 Scan in Data ------------ Enter SSN: 123 45 6789 Print out Data -------------- SSN: 123-1075138004-134513681, num read: 1 linux1-(9:41am): a.out Please enter a Social Security Number in the format: nnn-nn-nnnn Where n is a number between 0 and 9 Scan in Data ------------ Enter SSN: no Print out Data -------------- SSN: 134518596-1075138004-134513681, num read: 0 linux1-(9:41am): a.out Please enter a Social Security Number in the format: nnn-nn-nnnn Where n is a number between 0 and 9 Scan in Data ------------ Enter SSN: 123-de-6789 Print out Data -------------- SSN: 123-1075138004-134513681, num read: 1 linux1-(9:46am): a.out Please enter a Social Security Number in the format: nnn-nn-nnnn Where n is a number between 0 and 9 Scan in Data ------------ Enter SSN: 1-2-3 Print out Data -------------- SSN: 1-2-3, num read: 3 linux1-(9:46am):
Some Observations...