-1

here is a demo code for explaining what I'm actually looking for

I want to call a 'case' of "switch" - statement from somewhere else in the code in C Language.

Here is the source code:

#include <stdio.h>
#include <stdlib.h>
#include <conio.h>

char a;

int main()
{
    printf("Enter a number: ");
    scanf("%c", &a);
    switch(a){
    case 'a':
        printf("This is for A & C");
        break;
    case 'b':
        printf("This is for only B");
        break;
    case  'c':
        // Here i want to call "case 'a':"
    goto case 'a';

        // how can a call another case here?
        
        break;
    default:
        printf("Default");
        break;
    }
    getch();
    return 0;
}

Thanks is Advance :D

Omar Fahim
  • 19
  • 2
  • 1
    `case 'a':` is not a label, but you can put a real label `case_a:` next to it and `goto case_a;`. Whether that's a good idea, here or in general, is another matter. – dxiv Aug 29 '20 at 00:32
  • @dxiv: `case 'a':` is a label per C 2018 6.8.1. The grammar in paragraph 1 says a *labeled-statement* is either “*identifier* `:` *statement*”, “`case` *constant-expression* `:` *statement*”, or “`default` `:` *statement*”, and the text in paragraph 2 refers to it as a label: “A `case` or `default` label shall appear only in…” – Eric Postpischil Aug 29 '20 at 00:35
  • @EricPostpischil It used to be the case (ignore the pun) that case/default were not goto'able labels. If that changed in C 2018 then [this](https://stackoverflow.com/questions/31112750/why-cant-i-goto-default-or-goto-case-x-within-a-switch-selection-structur) could use an update. – dxiv Aug 29 '20 at 00:44
  • @dxiv: Thanks for your real label idea but can you please explain to me how to implement that? – Omar Fahim Aug 29 '20 at 00:56
  • 1
    @OmarFahim Just replace `case 'a':` with `case 'a': case_a:` and `goto case 'a';` with `goto case_a;`. – dxiv Aug 29 '20 at 01:36
  • `goto` can be done through a function, a macro or loops , and, if not, just by repeating a few lines. The reason `goto` seems attractive is because of its *very* simplistic name, creating a false sense of being as easy as though one was connecting dots and 'went-to' a previous dot. Dont be misled, it can be painful and isn't the only. It breaks predictability in ways that are not at all fun or easy, and god forbid you end up debugging a code block with `goto`, you'll realise this quick. Dont't be afraid to rewrite/repeat code again, if that keeps you from using `goto`. – A P Jo Aug 29 '20 at 04:03

1 Answers1

2

You need the so-called "fall-through" case:

So instead of this:

case 'a':
    printf("This is for A & C");
    break;
case 'b':
    printf("This is for only B");
    break;
case  'c':
    // Here i want to call "case 'a':"

Write this:

case 'a':
case 'c':
    printf("This is for A or C");
    break;

case 'b':
    printf("This is for only B");
    break;
artm
  • 17,291
  • 6
  • 38
  • 54
  • Thanks for your answer about using case 'a': case 'c': printf(); but that's just an example I actually want to use this somewhere else where I want to call a case from outside of that switch statement. – Omar Fahim Aug 29 '20 at 00:53
  • @OmarFahim well, make every case a function call? – Martin James Aug 29 '20 at 01:29
  • 1
    @OmarFahim: `goto` doesn't "call". It jumps, but it unlike a call, it never gets returned to. `goto` is certainly part of C, and it is certainly legal to use `goto` to jump into the middle of a selection statement or loop, But doing so will make your code hard to read, and therefore unlikely to pass code reviews. Do try to find a more structured control flow solution. – rici Aug 29 '20 at 01:57