We use ANSI C (American National Standards Institute). By definition, C programs commonly contain preprocessor directives, compiler directives, executable code, and comments for the reader. ANSI C must have certain standard libraries in its implementation.
%c | convert | to/from | character |
%d | " | " | integer |
%f | " | " | float |
%lf | " | " | double |
%s | " | " | string |
Examples: |
Example: int x, y; char code; double cost, debt; scanf("%c%lf%lf", &code, &cost, &debt); scanf("%d", &x); scanf("%d%d", &x, &y); printf("%d %d %f %c\n", y, x, cost, code); |
__________________________________
Note:
     scanf("%d%d%lf", &x, &y, &z);
with no spaces between placeholders is the same as
     scanf(" %d %d %lf", &x, &y, &z);
Variables of type char are different!
     scanf("%d%c", &x, &ch);
  on input  
7<blank>A
stores a blank in ch.
     scanf("%d %c", &x, &ch);
   on input  
7<blank>A
stores A in ch.
A blank space before %c
informs the system to ignore white spaces;
without the space the blank is treated as an input character.
__________________________________
/* Read, count, and sum up to N integers. */ #include <stdio.h> #define N 1000 void read_and_sum_it(int *ct, int *sum); int main() { int count, sum; read_and_sum_it(&count, &sum); printf("count: %d sum: %d\n", count, sum); } void read_and_sum_it(int *count, int *sum) { int x; for(*count = *sum = 0; scanf("%d", &x) != EOF && *count < N; (*count)++, *sum += x); } |
Example:
  int x, y, z;
  scanf("%d%d%d", &x, &y, &z);
printf("%d %d %d\n", x, y, z);
printf("%d %d %d\n", z, x, y);
input: 40 -8 6
output: 40 -8 6
    
  6 40 -8
Final values:   x: 40 y: -8 z: 6
__________________________________
Example:
scanf("%d%d%d", &y, &z, &x);
printf("%d %d %d\n", x, y, z);
input: 40 -8 6
output: 6 40 -8
Final values?   x:
y: z:
__________________________
Example:
input: 40 -8 6 23
output: 6 -8 6
Final values?   x:
y:
__________________________
Example:
Final values?   x:
y:
x = 23;
printf("*%d,%3d,%4d\n", x, x, x);
  output: *23,_23,_ _23 (The underscore represents a space.)
Notice that the commas in the format string are displayed. They are part of the literal string.
____________________
printf("%d%4d\n"), x, x);
output: 23_ _23 (The underscore represents a space.) _____________________________________________________
Floating Point Formats
printf("%d %8.2f", age, average);
%8.2f means
If fewer than 8 spaces are needed, blanks are put to left (right justified).
If more than 8 spaces are needed, the system still prints the correct value.
Strings are delimited by double quotes and may be included in printf().
If netcost is 12.5, the output is:
Answer is $ 12.50 net. (two spaces after $)The 7.2 format string prints 7 characters with 2 digits to the right of the decimal.
Note: %.3f and %0.3f print the number with no space, as do %d and %c.
typedef - Used to define mnemonic names for new data structures.
Example
Example
Example:
// Store data for a student.
typedef char STRING[31];  // template, no storage   
STRING name, major; // space for 62 chars, 2 ptrs
char address[31];     // different than type STRING
int age, tickets, hours;
char gender;
double gpa, tuition;
With multiple students, we have to declare multiple parallel arrays -- hard to keep track of related data.
Better Way:
// Create template for structure type. typedef char STRING[31]; // template, no storage    struct student_type { STRING name, major; // space for 62 chars, 2 char ptrs char address[31]; // different type than type STRING int age, tickets, hours; char gender; double gpa, tuition; }; struct student_type student; // allocate storageAnatomy of the record student that is type struct student_type:// Assign values to fields cin >> student.hours >> student.age >> student.gpa >> student.tuition; cin >> student.name; student.gender = 'F'; strcpy(student.major, "Computer Science"); student.tickets = 47;
It is clear the data are related.
// Allocate storage for structure variable
struct student_type student;
Type is   struct student_type.
Variable name is  student.
Fields are   name, major, age, etc.
typedef struct { // template STRING name, major; char address[31]; // different than type STRING int age, tickets, hours; char gender; double gpa, tuition; } STUDENT_TYPE; // allocate storage STUDENT_TYPE student;Assign values as before.
NOTE: C allows many variations, but these are commonly used. Feel free to investigate others, but be uniform in coding for good style.
Assume we declared student as before. Now we say:
   int age;
Is there a conflict?   NO.
   student.age   and   age   are 2 separate variables.
Now we have:
// Assign gpa to 24th student
course[23].gpa = 4.0;
// Assign a blank to 1st space in 68th student's name
course[67].name[0] = ' ';
// Read in the gender of 8th student
cin >> course[7].gender;
Note: To access a struct variable, always reference the variable name, never the type.
Example
typedef struct { // definition char make[21]; char model[21]; int year, mileage; } CAR_TYPE; CAR_TYPE car; // create car CAR_TYPE fleet[100]; // create fleet // Typical assignments car.year = 2007; strcpy(car.make, "Honda"); fleet[3].year = 2005; fleet[0] = fleet[99]; fleet[5] = car;Example
// define templates struct employee_type { char name[21]; double hours, wage, pay; char dept; int units_sold[12]; }; struct business_type { char name[41]; double earnings, expenses; struct employee_type boss; struct employee_type workers[100]; int accidents; }; // Allocate memory space struct business_type business, empire[500];
business.accidents = 0; business.name[40] = '\0'; business.workers[49].pay = 14.60; business.workers[4].units_sold[0] = 600;NOTE: Assignments must be type compatible, and we can say:
empire[15].boss.hours = 70; empire[499] = business; empire[0].boss = empire[2].workers[36]; business.workers[99].dept = empire[291].name[0];
business = empire[7]; // structures of same named type
Example
Example#define LEN 41 struct BOOK { char title[LEN]; char author[LEN]; double price; }; struct BOOK booklist[100], // array of 100 structures *p; // pointer to a structure of type struct BOOK
// Point p to 1st element of the array of structures p = booklist; // -OR- p = &booklist[0]; // Read and echo the title of the 1st element cin >> p->title; cout << p->title;
Create 2 car structures and a pointer to a car structure
void get_info(CAR_TYPE *pcar) { cin >> pcar->make; cin >> pcar->model; cin >> pcar->year; cin >> pcar->mileage; }Very similar to simple variables!
void print_classics(CAR_TYPE A[]) { int i; for(i=0; i<N; i++) if(A[i].year<60) cout << A[i].year << " " << A[i].make << " " <
original:
Example
Assume we have a list of strings that we want to be ordered always.
We might have more sophisticated commands that call functions to process the list. This is an example of information hiding and procedural abstraction. Some possible functions are:
Solution - keep a linked list that is implemented as a stack of available slots in the link
field!
input: 1) add Mel 2) add Linda 3) add Sara 4) add Carol 5) add Tori
6) delete Sara 7) add Belle 8) delete Belle 9) delete Tori
The result after command 5 is this:
We continue with commands 6 through 9: 6) delete Sara 7) add Belle 8) delete Belle 9) delete Tori The final result after commands 6, 7, 8, and 9 is this: