Interesting Facts in C Programming

Below are some interesting facts about C programming:

1) The case labels of a switch statement can occur inside if-else statements.

filter_none

edit
close

play_arrow

link
brightness_4
code

#include <stdio.h>
  
int main()
{
    int a = 2, b = 2;
    switch(a)
    {
    case 1:
        ;
  
        if (b==5)
        {
        case 2:
            printf("GeeksforGeeks");
        }
    else case 3:
    {
  
    }
    }
}

chevron_right


Output :

GeeksforGeeks

2) arr[index] is same as index[arr]
The reason for this to work is, array elements are accessed using pointer arithmetic.

filter_none

edit
close

play_arrow

link
brightness_4
code

// C program to demonstrate that arr[0] and
// 0[arr]
#include<stdio.h>
int main() 
{
    int arr[10];
    arr[0] = 1;
    printf("%d", 0[arr] );
      
    return 0;    
}

chevron_right


Output :



1

3) We can use ‘<:, :>’ in place of ‘[,]’ and ‘<%, %>’ in place of ‘{,}’

filter_none

edit
close

play_arrow

link
brightness_4
code

#include<stdio.h>
int main()
<%
    int arr <:10:>;
    arr<:0:> = 1;
    printf("%d", arr<:0:>);
  
    return 0;
%>

chevron_right


Output :

1

4) Using #include in strange places.
Let “a.txt” contains (“GeeksforGeeks”);

filter_none

edit
close

play_arrow

link
brightness_4
code

#include<stdio.h>
int main()
{
    printf
    #include "a.txt"
    ;
}

chevron_right


Output :

GeeksforGeeks

5) We can ignore input in scanf() by using an ‘*’ after ‘%’ in format specifiers

filter_none

edit
close

play_arrow

link
brightness_4
code

#include<stdio.h>
int main()
{
    int a;
  
    // Let we input 10 20, we get output as 20
    // (First input is ignored)
    // If we remove * from below line, we get 10.
    scanf("%*d%d", &a);
  
    printf( "%d ",  a);  
  
    return 0;     
}

chevron_right


This article is contributed by Harsh Agarwal. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.



My Personal Notes arrow_drop_up