How to use boolean type in C?

    by nikoo28

    Question:

    C does not have the boolean data type by default. Is there a way by which we can implement it?

    In C language, we do not have the boolean datatype by default. That means we cannot do something like

    if(true)
    {
        // perform some commands
    }
    else
    {
        // perform the else commands
    }
    

    We can do this in C++ and JAVA. However, we have an alternative method to achieve the same functionality. Before moving on to the implementation it is important to know that in C, if a value is ‘0 (zero)’ it is treated as false, and any other value other than ‘0’, is treated as true.
    That means if we do something like

    if(2)
    {
        // THIS PORTION WILL RUN
    }
    else
    {
        // this will NOT
    }
    
    if(0)
    {
        // this will NOT RUN
    }
    else
    {
        // THIS PORTION WILL RUN
    }
    
    while(1)
    {
        // an example of an infinite loop
    }
    

    Thus, to implement the boolean data type we can do it by 3 methods:-

    Method 1:-

    typedef int bool;
    #define true 1
    #define false 0
    

    Method 2:-

    typedef int bool;
    enum { false, true };
    

    Method 3:-

    typedef enum { false, true } bool;
    

    All 3 methods, basically perform the same functionality. It assigns the value ‘1’ to true and ‘0’ to false. This way we define a custom data type. and the C pre-processor takes care of the rest of the stuff.

    Here is a sample program that illustrates the use of this custom boolean datatype in C.

    #include<stdio.h>
    
    //defining the boolean datatype
    typedef int bool;
    #define TRUE 1
    #define FALSE 0
    
    //defining the main function to test
    int main(void)
    {
    
    	//testing the methodology
    	if(FALSE)
    	{
    		printf("I CANT PRINT");
    	}
    	else
    	{
    		printf("YOU SUCCESSFULLY IMPLEMENTED boolean DATATYPE");
    	}
    
    	//An example of infinite loop
    	while(TRUE)
    	{
    		printf("This loop will run forever");
    	}
    
    	return 0;
    }
    
    0 comments
    0 FacebookTwitterLinkedinWhatsappEmail
  • Question: Write a program to insert a node in a given sorted Linked List. Given List: 23 -> 32 -> 99 -> 101 -> 2222 Node to add:- 50 Output:- 23 -> 32 -> 50 -> 99 -> 101 -> 2222 To insert a node in a sorted Linked List, we need to perform a basic Linked List operation discussed in this post. Insert a node in a Linked List. In the given post, we knew the position at which we had to insert the node. But in this case …

    0 FacebookTwitterLinkedinWhatsappEmail
  • Question: What is the difference between doing:         ptr = (char **) malloc (MAXELEMS * sizeof(char *)); or:         ptr = (char **) calloc (MAXELEMS, sizeof(char*)); When is it a good idea to use calloc and when to use malloc ? There are two differences:- Firstly it is in the number of arguments, malloc() takes a single argument (memory required in bytes), while calloc() needs two arguments. Secondly, malloc() does not initialize the memory allocated, while calloc() initializes the allocated memory to ZERO. calloc() allocates a memory area, the length …

    0 FacebookTwitterLinkedinWhatsappEmail
  • MiscTheory

    What is an unsigned char?

    by nikoo28
    1 minutes read

    Question: In C, what is an unsigned char used for? How is this different from a regular char? In C, there are three distinct character types: char signed char unsigned char If you are using character types for text, use the unqualified char: it is the type of character literals like ‘a’ or ‘0’. it is the type that makes up C strings like “abcde” It also works out as a number value, but it is unspecified whether that value is treated as signed or unsigned. Beware character comparisons through …

    0 FacebookTwitterLinkedinWhatsappEmail
  • Question: In C, what is the difference between using ++i and i++, and which should be used in the incrementation block of a for loop ? i++ is known as Post Increment whereas ++i is called Pre Increment. ++i will increment the value of i, and then return the incremented value. int i = 1; int j = ++i; printf("i is = %d", i); // i is = 2 printf("j is = %d", j); // j is = 2 i++ will increment the value of i, but return the original …

    0 FacebookTwitterLinkedinWhatsappEmail
  • Question: How can you divide a number by 3 without using *, /, +, – % operator? Input: 48 Output: 16 The method below implements the method using bit-wise operators. //a function to add 2 numbers without using + int add(int x, int y) { int a, b; do { a = x & y; b = x ^ y; x = a << 1; y = b; } while (a); return b; } int divideby3(int num) { int sum = 0; while (num > 3) { sum = add(num …

    0 FacebookTwitterLinkedinWhatsappEmail
  • Given a number ‘n’, how to check if n is a Fibonacci number. A simple way is to generate Fibonacci numbers until the generated number is greater than or equal to ‘n’. Following is an interesting property about Fibonacci numbers that can also be used to check if a given number is Fibonacci or not. A number is Fibonacci if and only if one or both of 5n2+4 or 5×2-4 is a perfect square (Source: Wikipedia). Following is a simple program based on this concept. // C program to check …

    0 FacebookTwitterLinkedinWhatsappEmail

This website uses cookies to improve your experience. We'll assume you're ok with this, but you can opt-out if you wish. Accept Read More