Skip to content
This repository was archived by the owner on Aug 31, 2025. It is now read-only.

computablee/gosub_in_c

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

10 Commits
 
 
 
 
 
 
 
 

Repository files navigation

This project will be deleted soon!

Due to GitHub's unfortunate new practice of using repositories to train Microsoft LLMs, I have made the difficult decision to migrate all of my projects to Codeberg.

Because of this project's low visibility, I have decided that it is not important enough to keep on GitHub. Therefore, I have decided to delete this project at some point during the month of September. This project's new home can be found here.

GOSUB in C

GOSUB is a keyword in certain BASIC dialects which allows you to jump to a label, then return from it later on. This is a convenient way to incorporate a call stack in a language which lacks functions.

I wrote it in C because someone asked if it was possible.

You probably shouldn't actually use this library, if you can even call it that. Use functions, please.

Compilation

This library is a header-only library, meaning the only thing you need to do is include it into your program.

Usage

Here is an example use case of this library.

#include <stdio.h>
#include "gosub.h"

int main()
{
    GOSUB(subroutine_1);
    GOSUB(subroutine_2);
    GOSUB(subroutine_2);

    printf("This should be called last.\n");
    return 0;

subroutine_1:
    printf("This should be called first.\n");
    RETURN;

subroutine_2:
    printf("This should be called twice.\n");
    RETURN;
}

This results in the following output:

This should be called first.
This should be called twice.
This should be called twice.
This should be called last.

Limitations

To maintain portability across different platforms, no inline assembly is used. The downside is that the && operator is used from the GNU compiler extensions to get the address that a label references. Therefore, this library must be used only with a GNU C compiler (or other compilers which support this use-case).

This library creates dummy labels in order to return to the callsite. To eliminate label reusage, labels are created based on the current line number. Therefore, you can only have one GOSUB call per line in your source file.

Finally, the stack size is currently fixed at 256. Compile with -DGOSUB_STACK_SIZE to change.

About

An implementation of the GOSUB keyword from BASIC for the C programming language.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages