Wednesday, 15 April 2015

Contents

CONTENTS

PROGRAMS IN C


        Programs on arrays



     
       Programs on linked list





Tips & Tricks in C language




NUMBERS 



      The 0's story

      Beauty of maths

Interview Experiences




Interesting facts 


     
     Why GOOGLE used specific colors in its logo

     How to watch youtube videos without ads

     Access your WhatsApp from your Desktop Browser

Securuty


Famous quotes




Tuesday, 14 April 2015

Bit Fields in C Language

Bit Fields allow the packing of data in a structure or a Union with specified number of bits. This is especially useful when memory or data storage is at a premium or you have data with sizes different from normal datatypes

Example:
IP header can be declared as follows

typedef struct
{
      unsigned Vers                              : 4  ;/* Version number.*/
      unsigned IHL                               : 4  ;/* ethernet Header Length*/
      unsigned TOS                             : 8  ;/* Type of Service.*/
      unsigned TotalLength                  : 16 ;/* Total Length is the length of the datagram.*/
      unsigned Identification                 : 16 ;/* identifying fragments of a datagram.*/
      unsigned Flags                            : 3  ;/* Control Flags (Bit0:res,Bit1:1 = Don't Fragment,Bit2:0 = Last Fragment)*/
      unsigned FragmentOffset             : 13 ;/* Indicates where in the datagram this fragment belongs */
      unsigned TimeToLive                  : 8  ;/* Time to Live */
      unsigned Protocol                       : 8  ;/* Indicates the next level protocol used*/
      unsigned HeaderChecksum         : 16 ;/* checksum on the header only*/
      unsigned SourceAddress            : 32 ;/* Source address*/
      unsigned DestinationAddress      : 32 ;/* Destination address*/
} S_IPv4Header;


the above code will create a structure IPv4Header  which will contain elements
ver                             whose size is 4  bits,
IHL                           whose size is 4  bits,
TOS                          whose size is 8  bits,
TotalLength               whose size is 16 bits,
Identification              whose size is 16 bits,
Flags                         whose size is 3  bits,
Flags                         whose size is 3  bits,
FragmentOffset          whose size is 13 bits,
TimeToLive               whose size is 8  bits,
Protocol                    whose size is 8  bits,
HeaderChecksum      whose size is 16 bits,
SourceAddress          whose size is 32 bits,
DestinationAddress    whose size is 32 bits,

Monday, 13 April 2015

Write a C macro PRINT(x) which prints x , where x can be of any data type (string or char or int etc..)


At the first look, it seems that writing a C macro which prints its argument is very easy  Following program should work i.e. it should print x

#include<stdio.h>
#define PRINT(x) printf("%s",(x));
main()
{
PRINT(x);
}

OR

#include<stdio.h>
#define PRINT(x) (x)
int main()
  printf("%s",PRINT(x));
  return 0;
}


But it would issue compile error because the data type of x, which is taken as variable by the compiler, is unknown. Now it doesn’t look so obvious. Isn’t it? 

Guess what, the followings also won’t work
#define PRINT(x) ('x')
#define PRINT(x) ("x")

magic of Stringizing Operator

But if we know one of lesser known traits of C language, writing such a macro is really simple. In C, there’s a # directive, also called ‘Stringizing Operator’, which does this magic. Basically # directive converts its argument in a string. Voila! it is so simple to do the rest. So the above program can be modified as below.


#include<stdio.h>
#define PRINT(x) printf("%s",(x));
main()
{
PRINT(x);
}

OR 

#include<stdio.h>
#define PRINT(x) (#x)
int main()
  printf("%s",PRINT(x));
  return 0;
}
or 

Now if the input is PRINT(x), it will print x and PRINT(aravind) will print aravind

Wednesday, 8 April 2015

How to read a string with spaces using scanf

How to read a string with spaces using scanf

#include<stdio.h>
void main()
{
char str[100];
printf("Enter any string\n");
scanf("%[^\n]s",str);         //<----- Imp step--------
printf("\n You entered string %s \n",str);

}

Tuesday, 7 April 2015

COMMA operator


// PROGRAM 1

#include<stdio.h>


int main(void)

{

    int a = 1, 2, 3;

    printf("%d", a);

    return 0;

}
The above program fails in compilation, but the following program compiles fine and prints 1.

// PROGRAM 2

#include<stdio.h>


int main(void)

{

    int a;

    a = 1, 2, 3;

    printf("%d", a);

    return 0;

}
And the following program prints 3, why?

// PROGRAM 3

#include<stdio.h>


int main(void)

{

    int a;

    a = (1, 2, 3);

    printf("%d", a);

    return 0;

}
In a C/C++ program, comma is used in two contexts:
                           (1) A separator
                           (2) An Operator.
Comma works just as a separator in PROGRAM 1 and we get compilation error in this program.
Comma works as an operator in PROGRAM 2. Precedence of comma operator is least in operator precedence table. So the assignment operator takes precedence over comma and the expression “a = 1, 2, 3″ becomes equivalent to “(a = 1), 2, 3″. That is why we get output as 1 in the second program.
Comma works as an operator in PROGRAM 3. Precedence of comma operator is least in operator
precedence table, but as we applied ( for comma operator, So the comma operator takes precedence over assignment and the expression “1, 2, 3″  is calculated first (result of this is 3 ) then that result is assigned to a . That is why we get output as 3 in the third program.

PLEASE WRITE COMMENT WHERE I AM WRONG   OR ANY NEW IDEA IN YOUR MIND.......!!!!!

Wednesday, 1 April 2015

Interesting facts about betwise operators in C

There are 6 types bitwise operators in C which will work at bit-level:

& (bitwise AND)  This operator will take two numbers as operand and does AND on every bit of two numbers. The result of AND is 1 only if both bits are 1.

| (bitwise OR)  This operator will take two numbers as operand and does OR on every bit of two numbers. The result of OR is 1 any of the two bits is 1.

^ (bitwise XOR) This operator will take two numbers as operand and does XOR on every bit of two numbers. The result of XOR is 1 if the two bits are different.

<< (left shift) This operator will take two numbers, left shifts the bits of the first operand, the second operand decides the number of places to shift.

>> (right shift) This operator will take two numbers, right shifts the bits of the first operand, the second operand decides the number of places to shift.

~ (bitwise NOT) This operator will take one number and inverts all bits of it.

Now look at the example which demonstrate the use of all bitwise operators

/* C Program to demonstrate use of bitwise operators */
#include<stdio.h>
int main()
{
    unsigned char a = 5, b = 9; // a = 4(00000101), b = 8(00001001)
    printf("a = %d, b = %d\n", a, b);
    printf("a&b = %d\n", a&b); // The result is 00000001
    printf("a|b = %d\n", a|b);  // The result is 00001101
    printf("a^b = %d\n", a^b); // The result is 00001100
    printf("~a = %d\n", a = ~a);   // The result is 11111010
    printf("b<<1 = %d\n", b<<1);  // The result is 00010010
    printf("b>>1 = %d\n", b>>1);  // The result is 00000100
    return 0;
}

Facts about bitwise operator: 
No negative number should be used for the left shift and right shift operators We will get an undefined result if we take negative operands for << and >>. For example, results of both -3 << 3 and 3 << -3 are undefined.

The most useful operator from technical interview is  the bitwise XOR operator Wheniniagven set of numbers where all elements occur even number of times except one number, finds the odd occurring number, this problemcanbeefficientlysolvedbyjustdoingXOR of all numbers.
// Function to return the only odd occurring element

int findOdd(int arr[], int n){
   int res = 0, i;
   for (i = 0; i < n; i++)
     res ^= arr[i];
   return res;
}
int main(void) {
int arr[] = {12, 12, 14, 90, 14, 14, 14};
int n = sizeof(arr)/sizeof(arr[0]);
printf (“The odd occurring element is %d “, findOdd(arr, n));
return 0;
}
// Output: The odd occurring element is 90

Bitwise operators should not be interchanged with logical operators Because bitwise operators return an integer value and result of logical operators (&&, || and !) is either 0 or 1, Also the logical operators consider any non-zero operand as 1.

int main()
{
   int x = 2, y = 5;
   (x & y)? printf("True ") : printf("False ");
   (x && y)? printf("True ") : printf("False ");
   return 0;
}
// Output: False True
The left-shift and right-shift operators are equivalent to multiplication and division by 2 respectively.
As mentioned in point 1, it works only if the numbers are positive.
int main()
{
   int x = 19;
   printf ("x << 1 = %d\n", x << 1);
   printf ("x >> 1 = %d\n", x >> 1);
   return 0;
}
// Output: 38 9

We can check whether the number is odd or even with bitwise operators The number is odd if the value of expression (x & 1) would be non-zero, otherwise the value would be zero.

// Note that the output of following program is compiler dependent
int main()
{
   unsigned int x = 1;
   printf("Signed Result %d \n", ~x);
   printf("Unsigned Result %ud \n", ~x);
   return 0;
}
/* Output:
Signed Result -2
Unsigned Result 4294967294d */


Carefully use the ~ operator The result of ~ operator can  be a negative number if the result is stored in a signed variable (assuming that the negative numbers are stored in 2’s complement form where leftmost bit is the sign bit) and result on a small number can be a big number if the result is stored in a unsigned variable.

Program to print File name, Date, Time, and line number in C

There are some standard macros which can be used to print program file (__FILE__), Date of compilation (__DATE__), Time of compilation (__TIME__) and Line Number in C code (__LINE__)
#include <stdio.h>

int main()
{
   printf("Current File :%s\n", __FILE__ );
   printf("Current Date :%s\n", __DATE__ );
   printf("Current Time :%s\n", __TIME__ );
   printf("Line Number :%d\n", __LINE__ );
   return 0;
}
/* Output:
Current File :C:\Users\GfG\Downloads\deleteBST.c
Current Date :Feb 15 2014
Current Time :07:04:25
Line Number :8 */