I/O

Standard Streams

printf, scanf only works with stdin/stdout. They use the standard stream.

They don't have to be closed or opened; C handles that You can refer to them by these names, defined in stdio.h:

For example, fprintf can write to stdout like printf:

fprintf(stdout, "Hello World\n");

C File I/O

fprintf, fscanf works with any files. Good for big inputs and outputs.

fopen

fopen("output.txt", "w");

Use relative file path for the first parameter.
The second parameter indicates the modes, which are:

Note: "r" and "w" are common. "w" and "w+" causes the named file to be overwritten if it already exists.

fopen returns a FILE*, a pointer to the FILE struct. Returns NULL and crashes if fopen failed.

FILE* input = fopen("numbers.txt", "r");

if (input == NULL) {
	printf("Error: could not open input file\n");
	return 1; // indicate error 
}

fscanf

fscanf(input, "%d%d", &destination1, &destination2) 

Other Functions

Assertions

assert(boolean expression);

Assertion statements help catch bugs as close to the source as possible.

They make your assumptions clear:

int sum = a*a +b*b;
assert (sum >= 0);

We should use assert when we are verifying something about the logic of your program (checking for something that implies your program is incorrect). Use if statements to check the external environment and for bad user input.