C Programming Facts

  • 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

  • We can use ‘’ in place of ‘[,]’ and ‘’ in place of ‘{,}’

#include "stdio.h"
int main()

Output: 1

  • Using #include in strange places.

Let “a.txt” contains (“CPythonian”);

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

Output: CPythonian

  • 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;     
}
  • Using range with switch cases

switch(var)
{
    case 'A' ... 'Z':
        /*Do Something*/
        break;
    
    /*Or I can Write*/
    case 1 ... 5:
        /*Do Something*/
        break;
}
  • Printf() in C allows you to use variables for formatting format specifiers themselves

#include "stdio.h"
int main()
{
    int i = 3;
    float f = 4.1248953;
    printf("%.*f\n", i, f);
    return 0;
}

Output: 4.124
If a = 2 then it will print 4.12

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Create a website or blog at WordPress.com

Up ↑

%d bloggers like this: