Close Menu
Technotification
    Facebook X (Twitter) Instagram
    Facebook X (Twitter) Instagram
    Technotification
    • Home
    • News
    • How To
    • Explained
    • Facts
    • Lists
    • Programming
    • Security
    • Gaming
    Technotification
    Home › Programming › 10 Most Interesting C Programming Tricks

    10 Most Interesting C Programming Tricks

    By Vikram Singh RaoFebruary 26, 2023
    Facebook Twitter Reddit LinkedIn
    Interesting C programming tricks

    In this article, I am going to share some rare tricks of the C programming language. To be honest, I collected those tricks from various websites. Some of the tricks are really interesting because I never heard about them before and I’m sure you are too. Next time when your professor starts boasting about his/her knowledge of C programming, simply ask about these tricks, If you get the right answer with an explanation, he/she is the best teacher you’ll ever find.

    Interesting C Programming Tricks

    1. Using the return value of “scanf()” to check for end of file:

    while(~scanf(“%d”,&n)) {
    /* Your solution */
    }

    Very useful at online judges where inputs are terminated by EOF.

    2. “%m” when used within printf() prints “Success”:

    printf(“%m”);

    %m only prints “Success” when “errno == 0”, it’s short for a string representation of the last observed error state. For example, if a function fails before the printf then it will print something rather different. *

    3. Implicit initialization of integer variables with 0 and 1.

    main(i){
    printf(“g = %d, i = %d \n”,g,i);
    }

    4. brk(0); can be used as an alternative for return 0;:

    5. Print Numbers 1 to 200 wihout using any loop, goto, recursion

    #include<stdio.h>
    #define STEP1 step();
    #define STEP2 STEP1 STEP1
    #define STEP4 STEP2 STEP2
    #define STEP8 STEP4 STEP4
    #define STEP16 STEP8 STEP8
    #define STEP32 STEP16 STEP16
    #define STEP64 STEP32 STEP32
    #define STEP128 STEP64 STEP64
    #define STEP256 STEP128 STEP128

    int n = 0;

    int step()
    {
    if (++n <= 200)
    printf(“%d\n”, n);
    }

    int main()
    {
    STEP256;
    return 1;
    }

    7. C++ Sucks?

    #include <stdio.h>
    double m[]= {7709179928849219.0, 771};
    int main()
    {
    m[1]–?m[0]*=2,main():printf((char*)m);
    }

    You will be amazed to see the output: C++Sucks

    Here is the explanation

    8. Do you know “GOES TO-->” operator in C?

    Actually, –> is not an operator. In fact, it’s a combination of two separate operators i.e. — and >. To understand how the “goes to” operator works, go through the below code snippet.

    In the example, there is conditional’s code that decrements variable x, while returning x’s original (not decremented) value, and then compares the original value with 0 using the > operator.

    int _tmain(){
    int x = 10;
    while( x –> 0 ) // x goes to 0
    {
    printf(“%d “, x);
    }
    printf(“\n”);
    }

    Output:
    9 8 7 6 5 4 3 2 1 0
    Press any key to continue . . .

    9. Scanf Magic

    scanf(“%[^,]”, a); // This doesn’t scrap the comma

    scanf(“%[^,],”,a); // This one scraps the comma

    scanf(“%[^\n]\n”, a); // It will read until you meet ‘\n’, then trashes the ‘\n’

    scanf(“%*s %s”, last_name); // last_name is a variable

    10. Add numbers without ‘+’ operator

    int Add(int x, int y)
    {
    if (y == 0)
    return x;
    else
    return Add( x ^ y, (x & y) << 1);
    }

    Share. Facebook Twitter LinkedIn Tumblr Reddit Telegram WhatsApp
    Vikram Singh Rao
    • Website
    • Facebook
    • X (Twitter)
    • LinkedIn

    I am an entrepreneur at heart who has made his hobby turned a passion, his profession now.

    Related Posts

    The Best Python Libraries for Data Visualization in 2025

    April 1, 2025

    Is C++ Still Relevant in 2025 and Beyond?

    February 20, 2025

    5 Best Programming Languages for Machine Learning in 2025

    February 18, 2025

    10 Must-Have Chrome Extensions for Web Developers in 2025

    February 17, 2025

    Difference Between C, C++, C#, and Objective-C Programming

    February 16, 2025

    How to Learn Programming Faster and Smarter in 2025

    February 14, 2025
    Lists You May Like

    10 Best RARBG Alternative Sites in April 2025 [Working Links]

    April 1, 2025

    The Pirate Bay Proxy List in 2025 [Updated List]

    January 2, 2025

    10 Sites to Watch Free Korean Drama [2025 Edition]

    January 2, 2025

    10 Best Torrent Search Engine Sites (2025 Edition)

    February 12, 2025

    10 Best GTA V Roleplay Servers in 2025 (Updated List)

    January 6, 2025

    5 Best Torrent Sites for Software in 2025

    January 2, 2025

    1337x Alternatives, Proxies, and Mirror Sites in 2025

    January 2, 2025

    10 Best Torrent Sites for eBooks in 2025 [Working]

    January 2, 2025

    10 Best Anime Torrent Sites in 2025 [Working Sites]

    January 6, 2025

    Top Free Photo Editing Software For PC in 2025

    January 2, 2025
    Pages
    • About
    • Contact
    • Privacy
    • Careers
    Privacy

    Information such as the type of browser being used, its operating system, and your IP address is gathered in order to enhance your online experience.

    © 2013 - 2025 Technotification | All rights reserved.

    Type above and press Enter to search. Press Esc to cancel.