Thread: can we use default keyword without switch

  1. #1
    Registered User
    Join Date
    May 2021
    Posts
    56

    can we use default keyword without switch

    I am curious to know can we use default keyword without switch ? If it can't be done. so can anyone tell why it can't be done.

    If it's possible to do so what's the problem in my code it shows error before default

    Code:
     #include<stdio.h> 
    
    #define Var  3
    
    
    int main()
    {
        
        if ( Var  == 2 )
        {
               printf(" 2 \n" );
        }
        
        if ( Var  == 5)
        {
            printf(" 5 \n" );
        }
        
        
        if ( Var  == 4)
        {
               printf(" 4 \n" );
        }
        
    
    
        default
        {
            printf(" Not Valid \n" );
        }
         
        
         return 0;
    }

  2. #2
    Registered User
    Join Date
    Feb 2019
    Posts
    891
    Did you tried?

  3. #3
    Registered User
    Join Date
    May 2021
    Posts
    56
    Quote Originally Posted by flp1969 View Post
    Did you tried?
    Yes compiler shows following error

    Code:
    error: expected ':' before '{' token     {
         ^
    I know default is use with switch case but this question is for my curiosity

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,362
    The simple answer is: no, because it is invalid syntax.

    There may be some edge case or whatever where this is not true, but I don't feel like researching it, especially not when it'll probably confuse you anyway. Just stick to default with switch.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Registered User
    Join Date
    May 2021
    Posts
    56
    Quote Originally Posted by laserlight View Post
    The simple answer is: no, because it is invalid syntax.

    There may be some edge case or whatever where this is not true, but I don't feel like researching it, especially not when it'll probably confuse you anyway. Just stick to default with switch.
    Thank you laserlight for answering my question

  6. #6
    Registered User
    Join Date
    Apr 2021
    Posts
    69
    The default keyword is also used in the _Generic() expression selector. It still has to be inside the _Generic construct, so there's no way to just randomly scatter default around your code.

    However, the syntax <identifier> : is how a label is declared:

    Code:
    any_valid_identifier: {
        ... code ...
        }
    So you might be happy with something like:

    Code:
        otherwise: {
            printf("I got nuffink\n");
        }
    But you could achieve the same result using a #define if you don't like adding the colon. Just #define otherwise /*empty*/ and use it:

    Code:
        otherwise {
            ...
        }
    Last edited by aghast; 09-28-2021 at 08:19 PM.

  7. #7
    Registered User
    Join Date
    May 2012
    Location
    Arizona, USA
    Posts
    793
    Quote Originally Posted by aghast View Post
    So you might be happy with something like:

    Code:
        otherwise: {
            printf("I got nuffink\n");
        }
    But you could achieve the same result using a #define if you don't like adding the colon. Just #define otherwise /*empty*/ and use it:

    Code:
        otherwise {
            ...
        }
    But note that adding the label, by itself, doesn't actually do anything special. The code block would still be executed unconditionally. You'd need to change the previous if statements to a chain of "if"/"else if" and then change the "default" block to "else".

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Array-Switch-Default
    By Gkendoyan in forum C Programming
    Replies: 4
    Last Post: 10-24-2014, 02:11 PM
  2. C: Default case in switch not working
    By LunaLux in forum C Programming
    Replies: 5
    Last Post: 04-24-2011, 08:46 AM
  3. switch () {default: <something>; case ...
    By Kennedy in forum C Programming
    Replies: 4
    Last Post: 11-30-2010, 12:40 PM
  4. Switch statement / default:
    By kcpilot in forum C Programming
    Replies: 4
    Last Post: 12-02-2008, 03:14 PM
  5. default switch statement not working
    By gL_nEwB in forum C++ Programming
    Replies: 3
    Last Post: 05-13-2006, 10:13 AM

Tags for this Thread