Skip to article frontmatterSkip to article content
Site not loading correctly?

This may be due to an incorrect BASE_URL configuration. See the MyST Documentation for reference.

4) Intro to C

Today

  1. Compiling a C program

  2. Variables and Basic Data Types

1 Compiling a C program

Contrary to the examples you have seen in the previous lectures in Julia, C programs need to be compiled first, before being executed. This is a drawback of a compiled language (e.g., C, Fortran, Java, etc) Vs an interpreted language (e.g., MATLAB, Python, Julia, etc) in which the interpreter compiles and executes at the same time.

To compile a C program, you need a compiler. This may vary accoriding to the system you use. For:

Example in Linux, using gcc:

gcc hello.c -o hello

The gcc command invokes the compiler, then you pass the source file you want to compile (hello.c in this case), and then after the -o target, you can specify the output file for the compiled program (otherwise, the compiler will automatically create a.out) for you.

Inspect the directory after compiling, and (if the compilation was successful) you will an object output file (by default a.out) or the name of the executable you provided after the -o option.

To run/execute your program, simply type ./name_of_your_program in your terminal:

./hello

2 Variables and Basic Data Types

Our first C program: a Hello, World! print statement.

hello.c
#include <stdio.h> // includes the standard input-output header to be able to use standard IO functions, such as printf() and scanf(), to perform input and output operations.

// a "main" program is required in all C programs
int main(void)
{
     printf("Hello, world!\n"); // this printf statement will print formatted output in the standard output (your terminal screen)
}

Displaying Values of Variables:

product.c
#include <stdio.h> // includes the standard input-output header to be able to use standard IO functions, such as printf() and scanf(), to perform input and output operations.

int main(void)
{
    int product; // declaration of an integer variable
    product = 9*2*3;
    printf("The product of 9, 2, and 3 is %i \n", product); // this printf statement will print formatted output in the standard output (your terminal screen)

    return 0;
}

Variables

Note:

Global variables

Basic Data Types

int

float or double

float length = 190.4;
printf("Length= %f \n",length);

char

Bool

Displaying Values of Variables:

print_variables.c
#include <stdio.h>

int main (void)
{
    int integerVar = 230;
    float floatVar = 626.32;
    double doubleVar = 3.24e+3;
    char charVar = 'a';
    _Bool boolVar = 0;

    printf("integer variable= %i \n",integerVar);
    printf("float variable = %f \n", floatVar);
    printf("double variable = %e \n", doubleVar);
    printf("char variable = %c \n", charVar);
    printf("boolean variable = %i \n", boolVar);

    return 0;
}

Output:

integer variable= 230
float variable = 626.320007
double variable = 3.240000e+03
char variable = a
boolean variable = 0

Const variables

const int base = 10;

Any attempt to change its value will generate a compiler error message. Useful to avoid bugs: when you define a variable and use it multiple times in your code, it is also easier this way if you want to change its value, to only change it once where it is defined.