Interesting Facts in C Programming

2.5

Below are some interesting facts about C programming:

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

#include <stdio.h>

int main()
{
    int a = 2, b = 2;
    switch(a)
    {
    case 1:
        ;

        if (b==5)
        {
        case 2:
            printf("GeeksforGeeks");
        }
    else case 3:
    {

    }
    }
}

Output :

GeeksforGeeks

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

// 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;    
}

Output :

1

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

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

    return 0;
%>

Output :

1

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

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

Output :

GeeksforGeeks

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

#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;     
}

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.

GATE CS Corner    Company Wise Coding Practice

Recommended Posts:



2.5 Average Difficulty : 2.5/5.0
Based on 11 vote(s)










Writing code in comment? Please use ide.geeksforgeeks.org, generate link and share the link here.