in c, C++, go, if-else

How to mock if statement with initializers in C

Modern programming languages (such as C++17, Go, etc.) provide a very handy feature: if statement with initializer(s). They let you declare and initialize variables that, if present, are only visible within the if block and the associated else block.

In the code snippets below, the val the variable is within the scope of if-else blocks.

C++17

if (bool val = is_even(101); val) {
    std::cout << val;
} else {
    std::cout << val;
}

Go

if val := is_even(101); val {
    fmt.Println(val)
} else {
    fmt.Println(val)
}

However, this feature is nothing more than syntactic sugar, and you can easily mock this feature in other programming languages. Hacks to do so are listed below:

C

Consider the following example:

#include <stdio.h>
#include <stdbool.h>
 
bool is_even(int n) {
    return (n & 1) == 0;
}
 
int main(void) {
    int n = 4;
 
    bool val = is_even(n);
    if (val) {
        // 1 ==> TRUE
        printf("%i\n", val);
    } else {
        // 0 ==> FALSE
        printf("%i\n", val);
    }
 
    //val is also accessible below
    val = false;
 
    return 0;
}

The val variable gets leaked/exposed to the code below the if-else statements, which could create problems in some cases. A common way to get around this problem is to encapsulate the variable(s), along with the if-else block, within a block. This is shown below:

#include <stdio.h>
#include <stdbool.h>
 
bool is_even(int n) {
    return (n & 1) == 0;
}
 
int main(void) {
    int n = 4;
 
    { //If with initializer(s) block
    bool val = is_even(n);
    if (val) {
        printf("%i\n", val);
    } else {
        printf("%i", val);
    }
    } //End block
 
    //val is not accessible now
    val = false;
 
    return 0;
}

The above code will now result in a compilation error:

main.c: In function ‘main’:
main.c:24:2: error: ‘val’ undeclared (first use in this function)
  val = false;
  ^~~

So, in this way, you can mock the if statement with initializer(s) in the C programming language.

You can also apply the same hack in other programming languages (like C++11, C++14, Java, JavaScript, etc.) by nesting the variables and if-else statements in a block.

  1. SKincell Pro is well known by the individuals
    who used it as an optimal skin fixor. The serum contains the exact same active ingredients as a number of the other moles getting rid of creams on the marketplace today, but it includes greater
    than many of them. The lotion also improves your skin texture as
    well as gives even-toned skin. The active ingredients, the product has are all natural active ingredients which are risk-free for your skin. https://bookmarkstore.download/story.php?title=skincell-pro-side-effects

  2. Hello there, I discovered your site by way of Google while looking for a related subject, your site got here up, it seems good.
    I have bookmarked it in my google bookmarks.

    Hi there, just become aware of your weblog through Google,
    and located that it is truly informative. I’m going to watch out for brussels.
    I’ll appreciate in the event you proceed this in future.
    Many other people might be benefited out of your writing.
    Cheers!

    Feel free to surf to my blog :: buy cbd gummies

  3. It is appropriate time to make some plans for the future and it’s time to
    be happy. I’ve read this post and if I could I wish to suggest you some interesting things or suggestions.
    Perhaps you could write next articles referring to this article.

    I wish to read more things about it!

Webmentions

  • gas fireplace April 3, 2021

    gas fireplace

    […]the time to read or stop by the content or internet sites we have linked to beneath the[…]

  • bàn học cho bé April 3, 2021

    bàn học cho bé

    […]Sites of interest we’ve a link to[…]

  • 50 Beowulf April 3, 2021

    50 Beowulf

    […]the time to study or visit the material or internet sites we’ve linked to beneath the[…]

  • viagra meaning April 3, 2021

    viagra meaning

    How to mock if statement with initializers in C | Harsh Rathod

  • what do the icons mean on tinder April 3, 2021

    what do the icons mean on tinder

    How to mock if statement with initializers in C | Harsh Rathod

  • about April 3, 2021

    about

    […]we came across a cool web site that you simply could enjoy. Take a appear when you want[…]

  • rechargeable rabbit vibrator April 3, 2021

    rechargeable rabbit vibrator

    […]one of our guests not long ago advised the following website[…]

  • about April 3, 2021

    about

    […]check below, are some completely unrelated websites to ours, even so, they are most trustworthy sources that we use[…]

  • Buy Opana 10 mg online April 3, 2021

    Buy Opana 10 mg online

    […]below you’ll locate the link to some web sites that we consider you’ll want to visit[…]

  • Halo Fireteam Raven April 3, 2021

    Halo Fireteam Raven

    […]usually posts some incredibly interesting stuff like this. If you are new to this site[…]

  • SEO Agency April 3, 2021

    SEO Agency

    […]although web sites we backlink to below are considerably not related to ours, we really feel they are really really worth a go via, so possess a look[…]

  • bitcoin atm in vancouver April 3, 2021

    bitcoin atm in vancouver

    […]check below, are some entirely unrelated internet sites to ours, having said that, they are most trustworthy sources that we use[…]

  • Website April 3, 2021

    Website

    […]please check out the web-sites we adhere to, such as this one particular, because it represents our picks through the web[…]

  • SEA VOYAGES April 3, 2021

    SEA VOYAGES

    […]that will be the finish of this post. Here you will find some sites that we think you will appreciate, just click the hyperlinks over[…]

  • crypto currency virtual card April 3, 2021

    crypto currency virtual card

    […]Sites of interest we have a link to[…]

  • best strapon for pegging April 3, 2021

    best strapon for pegging

    […]please take a look at the web-sites we comply with, like this one particular, because it represents our picks in the web[…]

  • sex toy review April 3, 2021

    sex toy review

    […]usually posts some really fascinating stuff like this. If you are new to this site[…]

  • موقع سكس April 3, 2021

    موقع سكس

    […]below you’ll discover the link to some web sites that we consider you should visit[…]

  • Website April 3, 2021

    Website

    […]The data mentioned inside the write-up are a few of the top offered […]

  • Lehitravel.com April 3, 2021

    Lehitravel.com

    […]we like to honor a lot of other web internet sites around the web, even though they aren’t linked to us, by linking to them. Under are some webpages really worth checking out[…]

  • PINK DIAMOND STRAIN April 3, 2021

    PINK DIAMOND STRAIN

    […]below you’ll obtain the link to some web pages that we think you’ll want to visit[…]

  • vibrating panty April 3, 2021

    vibrating panty

    […]please stop by the websites we follow, like this 1, because it represents our picks through the web[…]

  • https aka ms remoteconnect microsoft April 3, 2021

    https aka ms remoteconnect microsoft

    […]usually posts some really intriguing stuff like this. If you’re new to this site[…]

  • www.meetfreegirls.info/crossdress-cams April 3, 2021

    http://www.meetfreegirls.info/crossdress-cams

    […]here are some links to web sites that we link to due to the fact we believe they may be really worth visiting[…]

  • DEXEDRINE 20mg April 3, 2021

    DEXEDRINE 20mg

    […]Here are some of the internet sites we advise for our visitors[…]

  • buy focalin online April 3, 2021

    buy focalin online

    […]check below, are some totally unrelated web-sites to ours, nevertheless, they’re most trustworthy sources that we use[…]

  • mopeds for sale April 3, 2021

    mopeds for sale

    […]usually posts some pretty fascinating stuff like this. If you’re new to this site[…]

  • how to make dmt April 3, 2021

    how to make dmt

    […]here are some hyperlinks to web-sites that we link to simply because we consider they are really worth visiting[…]

  • disposables gloves for sale eu April 3, 2021

    disposables gloves for sale eu

    […]that is the finish of this article. Right here you’ll come across some websites that we think you’ll appreciate, just click the hyperlinks over[…]

  • counterfeit money for sale April 3, 2021

    counterfeit money for sale

    […]The facts talked about inside the post are several of the best readily available […]

  • online cbd shop April 3, 2021

    online cbd shop

    […]check below, are some totally unrelated internet sites to ours, on the other hand, they may be most trustworthy sources that we use[…]

  • 1 Ml Syringe, Blue Gloves April 3, 2021

    1 Ml Syringe, Blue Gloves

    […]Sites of interest we’ve a link to[…]

  • metal storage containers April 3, 2021

    metal storage containers

    […]the time to read or stop by the content material or web sites we have linked to beneath the[…]

  • Dmt for sale April 3, 2021

    Dmt for sale

    […]very few internet sites that happen to be comprehensive below, from our point of view are undoubtedly nicely really worth checking out[…]

  • buy Instagram followers April 3, 2021

    buy Instagram followers

    […]the time to read or go to the content or web sites we’ve linked to below the[…]

  • THC Vape Oil April 3, 2021

    THC Vape Oil

    […]The info talked about within the article are several of the top out there […]

  • Containers for sale April 3, 2021

    Containers for sale

    […]Every when in a though we pick out blogs that we study. Listed beneath are the newest web pages that we pick […]

  • SEO Company UK April 3, 2021

    SEO Company UK

    […]The details talked about within the report are several of the ideal available […]

  • Sunderland Cleaners April 3, 2021

    Sunderland Cleaners

    […]Sites of interest we have a link to[…]

  • Guns for sale April 3, 2021

    Guns for sale

    […]although web-sites we backlink to beneath are considerably not connected to ours, we feel they’re in fact worth a go as a result of, so have a look[…]

  • Biohazard Cleaning Services London April 3, 2021

    Biohazard Cleaning Services London

    […]Sites of interest we have a link to[…]

  • where to buy guns online April 3, 2021

    where to buy guns online

    […]Sites of interest we have a link to[…]

  • shroom supply April 3, 2021

    shroom supply

    […]very couple of internet websites that occur to become detailed below, from our point of view are undoubtedly effectively worth checking out[…]

  • taurus g2c April 3, 2021

    taurus g2c

    […]one of our visitors lately advised the following website[…]

  • 샌즈카지노 April 3, 2021

    샌즈카지노

    […]we came across a cool site which you could possibly take pleasure in. Take a appear in case you want[…]

  • 코인카지노 April 3, 2021

    코인카지노

    […]we came across a cool internet site that you just may take pleasure in. Take a search when you want[…]

  • 메리트카지노 April 3, 2021

    메리트카지노

    […]Here is a good Weblog You may Obtain Exciting that we Encourage You[…]

  • 퍼스트카지노 April 3, 2021

    퍼스트카지노

    […]Here is a superb Weblog You might Obtain Interesting that we Encourage You[…]

  • 우리카지노 April 3, 2021

    우리카지노

    […]always a major fan of linking to bloggers that I really like but really don’t get quite a bit of link really like from[…]

  • Google April 3, 2021

    Google

    That could be the end of this report. Right here you will find some websites that we think you will value, just click the links.

  • Google April 3, 2021

    Google

    The facts talked about in the write-up are a few of the top out there.

    • Related Content by Tag