Skip to content

Latest commit

 

History

History
49 lines (39 loc) · 1.44 KB

File metadata and controls

49 lines (39 loc) · 1.44 KB

C Dotenv

A C program/library to read environment variables from a .env file, similar to other packages/libraries available in various programming languages.

Usage

  1. To use dotenv library in your project simply add the dotenv.c and dotenv.h files in your project along with your .env file as shown below or create a shared library as per your need:
├── .env
├── dotenv.c
├── dotenv.h
└── main.c
  1. Once dotenv.h is included in your file, invoke the load_env function which takes filepath of the .env file as an argument:
  // filename: main.c 
  // other include statements
  #include <stdlib.h>
  #include "dotenv.h"

  int main()
  {
     load_env(".env"); // if the .env file is in the current directory.
      // use the getenv method in the <stdlib.h> to get the value of your environment variable.
      char myVar[] = getenv("myVar");
      // other logic...
  }
  1. Your .env file can look something like this:
myVar="myVarValue"
myvar = "myvarvalue"
#mycommentedvar = "mycommentedvarvalue"
  1. Then compile you're program using gcc or similar compiler:
gcc main.c dotenv.c -o main
  1. Or if you created a shared library:
gcc main.c -ldotenv -o main # or whatever you named your library