leowhitehead / c-bool-value

An effective and easy way to use 'true' and 'false' values in standard c.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

C Boolean Value

Build Status Coverage Status Build Status dependencies size usedby downloads standard Version License

What this is

This project is an attempt to bring 'true' and 'false' values to the (GCC) C Programming language. This project may work in C++, but no testing has been done.

Motivations

One of the major factors preventing newer programmers from adopting C is its lack of similarity with higher level languages and constructs. The most noticable difference between C and these more modern languages is likely its lack of reserved true and false values. By making this, I hope to draw more people to use the C Programming Language.

Features

  • True value
  • False value

Installing

This library can be installed a number of ways.

Git

git clone https://github.com/lduck11007/c-bool-value.git
cp c-bool-value/src/cboolvalue.h cboolvalue.h
rm -rf c-bool-value

Curl

curl https://raw.githubusercontent.com/lduck11007/c-bool-value/master/src/cboolvalue.h > cboolvalue.h

Windows CMD

Create a file labled get.bat with the following:

@Echo OFF
SetLocal EnableDelayedExpansion
Set Var=%1
Set Var=!Var:http://=!
Set Var=!Var:/=,!
Set Var=!Var:%%20=?!
Set Var=!Var: =?!
Call :LOOP !var!
Echo.Downloading: %1 to %~p0!FN!
powershell.exe -Command (new-object System.Net.WebClient).DownloadFile('%1','%~p0!FN!')
GoTo :EOF
:LOOP
If "%1"=="" GoTo :EOF
Set FN=%1
Set FN=!FN:?= !
Shift
GoTo :LOOP

In cmd in the same directory or with PATH configured, type

get https://raw.githubusercontent.com/lduck11007/c-bool-value/master/src/cboolvalue.h

Examples

A simple function that returns true or false depending on whether an item is in an array

#include "cboolvalue.h"

int search(char values[], int len, char searchfor){
    int search_at = 0;
    int search_res = false;
    while(search_at < len && search_res == false){
        if(values[search_at] == searchfor)
            search_res = true;
        else
            search_at += 1;
    }
    return search_res;
}

For power users and kernel hackers, this can be rewritten as follows for far easier readability.

#include "cboolvalue.h"
#define bool int                    //TODO: learn how typedef works

bool search(char values[], int len, char searchfor){
    int search_at = 0;
    bool search_res = false;
    while(search_at < len && search_res == false){
        if(values[search_at] == searchfor)
            search_res = true;
        else
            search_at += 1;
    }
    return search_res;
}

It's plain to see that this program is now far easier to read for a programmer of any skill level, and is practically self-documenting.

More examples

Simple function that returns whether an array is sorted or not, made instantly more easy to understand with true and false values.

#include "cboolvalue.h"
#define bool int
bool issorted(char array[], int len){
    bool sorted = true;
    int i;
    for(i = 0; i < len; ++i){
        if(array[i-1] > array[i])
            sorted = false;
    }
    return sorted;
}

Contributing

Most contributing to this project is more than welcome and greatly appreciated. Before contributing, please read contributing.md for guidelines.

FAQ

Q. Why not use <stdbool.h>?

This is faster.

Q. When will you rewrite it in Rust/Go?

A. After I finish porting it to Haskell

Q. What kind of projects can I use this in?

A. Please refer to the license.

About

An effective and easy way to use 'true' and 'false' values in standard c.

License:Do What The F*ck You Want To Public License


Languages

Language:C 100.0%