Hello World Program in C using Jupyter Notebook

Creating a Hello World Program in C using Jupyter Notebook

This notebook will guide you through creating, compiling, and running a simple “Hello World” program in C language using Jupyter’s magic commands.

Step 1: Creating the C File

The %%file magic command allows us to create a new file directly from a Jupyter cell. It takes everything in the cell below the command and writes it to the specified filename. This is very useful for creating source code files without leaving the notebook environment.

%%file hello_world.c
#include <stdio.h>

int main() {
    printf("Hello, Risc-v World!\n");
    return 0;
}

Writing hello_world.c

Step 2: Compiling the C Program

The %%bash magic command lets us run bash shell commands directly in the notebook. Here we’re using gcc (GNU Compiler Collection) to compile our C program. The -o flag specifies the output filename for the compiled executable.

%%bash
gcc -o hello_world hello_world.c

Step 3: Running the Compiled Program

Again using %%bash, we execute our compiled program. The ./ before the filename tells bash to look for the executable in the current directory. If compilation was successful, this should display “Hello, World!” as output.

%%bash
./hello_world

Hello, Risc-v World!

Summary

  • %%file: Creates a new file with the content of the cell

  • %%bash: Executes bash shell commands

  • gcc: The GNU C compiler that converts human-readable C code into machine-executable code

  • ./filename: Executes a program from the current directory

You’ve now successfully created, compiled, and run a C program entirely within a Jupyter notebook!

1 Like