Output Parameters (output_params.c)

/**
 * C Program demonstrating several examples of using output parameters
 * 
 */

#include <stdio.h>

/* Swaps the values of two integers (using output parameters) */
void swap(int*, int*);

/* Computes the double and square of the input number,
   (storing the results in the output parameters) */
void compute(int, int*, int*);

// Note: Did not include argc and argv since this program
// does not accept command-line arguments
int main() {
    int x = 3;
    int y = 5;
    int d, s;

    // Example of calling swap()
    printf("original values:\n");
    // x = 3; y = 5
    printf("x = %d; y = %d\n", x, y);

    // swap the numbers
    swap(&x, &y);
    printf("swapped values:\n");
    // x is now 5, y is now 3
    printf("x = %d; y = %d\n", x, y);


    // Example of calling compute()
    // Note: at this point, x = 5
    compute(x, &d, &s);
    printf("computed values:\n");
    printf("x*2 = %d; x*x = %d\n", d, s);

    return 0;
}


void swap(int* a, int* b) {
    int temp = *a;
    *a = *b;
    *b = temp;
}

void compute(int val, int* doubled_val, int* squared_val) {
    *doubled_val = val * 2;
    *squared_val = val * val;
}

To compile and run:

gcc -g -Wall -std=c11 -o output_params output_params.c
./output_params

File I/O (file_demo.c)

/*
 * This program counts how many times a specified character appears
 * in a given file. The filename and target character are provided
 * as command-line arguments. It reads the file one character at a time
 * using fread, compares each character to the target, and prints
 * the total count at the end. Errors opening the file or incorrect
 * usage are reported to stderr.
 */


#include <stdio.h>
#include <stdlib.h>

int main(int argc, char* argv[]) {
    if (argc != 3) {
        // Print error msg to stderr
        fprintf(stderr, "Usage: %s <filename> <character>\n", argv[0]);
        exit(EXIT_FAILURE);
    }

    char* filename = argv[1];
    char target = argv[2][0];  // Take the first character of the second argument

    FILE *fp = fopen(filename, "r"); // Opens the file using "read" mode
    if (fp == NULL) { // If the file doesn't exist (or can't be opened)
        // Print error msg to stderr
        fprintf(stderr, "Error: Could not open file '%s'\n", filename);
        exit(EXIT_FAILURE);
    }

    int count = 0;
    char c;
    // Read each character of the file
    while (fread(&c, sizeof(char), 1, fp) == 1) {
        if (c == target) { // Count number of instances of target char
            count++;
        }
    }

    // Good practice: close the file when done using it
    fclose(fp);

    printf("The character '%c' appeared %d times in '%s'.\n", target, count, filename);

    exit(EXIT_SUCCESS);
}

To compile and run:

gcc -g -Wall -std=c11 -o file_demo file_demo.c
./file_demo

More Examples:

static_data_example.c

#include <stdio.h>
#include <stdlib.h>

int counter = 0;    // global var

int main(int argc, char** argv) {
  counter++;
  printf("count = %d\n",counter);
  return EXIT_SUCCESS;
}

stack_data_example.c

#include <stdio.h>
#include <stdlib.h>

int foo(int a) {
  int x = a + 1;     // local var
  return x;
}

int main(int argc, char** argv) {
  int y = foo(10);   // local var
  printf("y = %d\n",y);
  return EXIT_SUCCESS;
}