Introduction To Programming

Why programs ? Programming is nothing but making the computers think the same way a human thinks. Programs are made for obtaining a specific solution which can even be solved by humans which when solved by us may consume time..

Wednesday, December 15, 2010

Coding to create a pyramid using numbers in c program

#include<stdio.h>
void main()
{
 int n;
 do
 {
  printf("How many lines you want to printn");
  scanf("%d",&n);
 }
  while((n<1)||(n>9));
  for(int i=1;i<=n;i++)
  {

   for(int j=1;j<=(n-i);j++)
   printf(" ");

   for(j=1;j<=i;j++)
                        printf("%d",j);    

                        for(j=(i-1);j>=1;j--)
   printf("%d",j);

   printf("n");

  }
}

Sunday, August 15, 2010

Predict the output for this program

int main( )
{
printf("%d",printf("%d %d",5,5)&printf("%d %d",7,7));
return 0;
}

1 7 7 5 5
7 7 5 5 1
5 5 7 7 3
7 7 5 5 3

What is the output of the following program?

int main( )
{
printf(" %d %d",printf("%d %d",7,7),printf("%d %d",5,5));
return 0;
}


7 7 5 5 3 3
5 5 7 7 3 3
3 3 7 7 5 5
3 3 5 5 7 7



Wednesday, August 11, 2010

Produce 2 power n series using bitwise operator

I am printing 2n series up to 210

int main()
{
int i;
for(i=0;i<10;i++)
printf("%d\n",1<<i);

return 0;
}

This will be the output of the above code

1
2
4
8
16
32
64
128
256
512

Tuesday, August 10, 2010

Extern variable :predict the output

int i=2;
int main()
{
extern int i;
clrscr();
printf("%d",i);
getch();
return 0;
}
int i=3;

Saturday, June 12, 2010

predict the output

#include
void main(){
int check=2;
switch(check){
case 1: printf("D.W.Steyn");
case 2: printf(" M.G.Johnson");
case 3: printf(" Mohammad Asif");
default: printf(" M.Muralidaran");
}
}

(A) M.G.Johnson
(B) M.Muralidaran
(C) M.G.Johnson Mohammad Asif M.Muralidaran
(D) Compilation error
(E) None of the above

output - (c)

Wednesday, June 2, 2010

#include
#define a 10
main()
{
#define a 50
printf("%d",a);
}

Answer:
50
Explanation:
The preprocessor directives can be redefined anywhere in the program. So the most recently
assigned value will be taken.



main()
{
char *p;
p="Hello";
printf("%c\n",*&*p);
}
Answer:
H
Explanation:
* is a dereference operator & is a reference operator. They can be applied any number of
times provided it is meaningful. Here p points to the first character in the string "Hello". *p
dereferences it and so its value is H. Again & references it to an address and * dereferences
it to the value H.



main()
{
int i=-1;
+i;
printf("i = %d, +i = %d \n",i,+i);
}
Answer:
i = -1, +i = -1

Explanation:
Unary + is the only dummy operator in C. Where-ever it comes you can just ignore it just
because it has no effect in the expressions (hence the name dummy operator).


Posted by sharmi

static

main()
{
static int var = 5;
printf("%d ",var--);
if(var)
main();
}
Answer:
5 4 3 2 1
Explanation:
When static storage class is given, it is initialized once. The change in the value of a static
variable is retained even between the function calls. Main is also treated like any other ordinary function,
which can be called recursively.

Posted by sharmi

Monday, May 24, 2010

Short circuit operators

Short circuit operator is operator in which first operand of expression is evaluated , the second operand is evaluated if it is necessary.

Short circuit operators in c programming are &&(Logical AND), ||(Logical OR).

Logical AND:
In logical AND operator expression if the first operand is false then it is no need of evaluating the second operand.

Logical OR:
In Logical OR operator expression if the first operand is true then it is no need of evaluating the second operand.

AND operator example
OR operator example

Friday, April 30, 2010

Predict the output

extern int a;
int ss();
int main()
{
int a=10;
ss();
return printf("%d",a);
}
int ss()
{
return printf("%d",a);
}
int a=20;
Answer:
2010
1020
1010
Compilation Error

Monday, April 26, 2010

Predict the Output:

main()
{
float me = 1.1;
double you = 1.1;
if(me==you)
printf("shark selva");
else
printf("Matrix Developer");
}



shark selva
Matrix Developer

Difference between a string copy (strcpy) and a memory copy (memcpy)?

What is the difference between a string copy (strcpy) and a memory copy (memcpy)? When should each be used?


why??

why n++ executes faster than n+1?



Sunday, April 25, 2010

Is these two programs same?

for (i=0;i<5;i++)
{
if(i%3==0)
continue;
}

i=0;
while(i<5)
{
if(i%3==0)
continue;
i++;
}

Answer:

Yes
No



Saturday, April 24, 2010

Additon without Arithmetic Operators:

Here is the program which use bit wise operator and perform the addition operation. There is no arithmetic operator is used in this program.

int main()
{
int a,b,sum,carry;
printf("Enter the values");
scanf("%d %d",&a,&b);
sum=a^b;
carry=a&b;
while(carry!=0)
{

Program without Header File

is it possible to run the program without header file?  yes, it is possible.  consider the following program.

int main()
{
printf("hi");
return 0;
}
 It will work perfectly ,if you save the file.  But the concept is that while compiling itself ,compiler includes stdio.h header file automatically. 

Predict the Output:

int main()
{
fprintf(stdout,"Hello");
return 0;
}
 Answer:


Error,stdout is undeclared
Hello will be printed in console
Hello will be print in stdout.c file

Will it work?

#include<string.h>
int main()
{
printf(strcat("www.newtech4u.co.cc","!");
return 0;

}

exit Function:

This article will teach you what the exit function does.


Definition of exit Function:
exit function terminates the program process. It is called as "exit(error_status)"
 consider this following program:
 #include<stdio.h>
#include<stdlib.h>

Difference between Normal function and Macro Funcion

Normal function is defined :
return_type function_name(arguments)
{
//coding
}
Macro function:
#define funtion_name(arguments) coding

Difference:
while compiler expanding the code, the macro function is replaced in called place, but normal function is just called by . 


Macro function example source code and expanded code:

Source Code:
#define tech() printf("hi")
int main()

Run the coding after exiting main function

Do you think that it is possible to run the code after exiting main function? Yes ,it is possible to run the codes after exiting main function also.

Consider this Simple Program:
#includ<stdio.h>
#include<stdlib.h>
void after_main()
{

Pointer vs Array

Let you teach what is difference between pointer and array. Firs of all we should know the definition of pointer and array.

Pointer:
Pointer is a variable that capable of storing the address
Array:
Array is collection of same elements

Difference between #include and #include "stdio.h"

Many of you confused with this two method of specifying the include files. This article will teach you what the difference between these two is.

#include <stdio.h>

This method of specifying is used to include the standard header files.

Sunday, April 18, 2010

Predict The Output:

int main()
{
int *p=(int *)1000;
*p=2;
printf("%d",*((int*)1000));

return 0;
}

Predict The Output:

int main()
{
char a[]="shark";
(*a)++;
printf("%s\n",a);
printf("%s",a+1);
return 0;
}

Output:
thark
hark

Predict The Output:

int main()
{
return printf("shark ");
}
int x=main();

Don't think that it will result in compilation error. It will run perfectly and prints "shark shark".

Output:
shark shark

Predict the Output

int main()
{
int a=1;
a+=a++;
printf("%d",a);
a+=++a;
printf("%d",a);
return 0;
}

Output:
3 8

Logic:
a+=a++ is equal to a=a+a++;
watch out the value of 'a' in this expression.
a=a + a++
3  1   1
a+a=2 and the post increment will increment the value of 'a' so it will result in a=3.(to understand increment operator see this post also Lparts,Pre and post,Post 18).

Now we will consider the second operation i.e., a+=++a;. This expression is equal to
a=a+ ++a;
This expression is manipulated as follows:
a = a+  ++a;
8   4     4    
Pre increment will increment the value of a first so 'a' has now 4. and a+a is 8 is assigned to a. so a=a+a=8.

What is Null?

Two type of Null , "NULL pointer","NULL character. NULL means "nothing".
NULL pointer usually assigned to pointers. This will make pointer to point nothing i.e, pointer reference to no variable.
NULL character('\0') usually added automatically at the end of each string used in c program. It defines end of string.

Predict the Output

#define tech(a,b) a##b##a
int main()
{
if(printf("%d",tech(4,0)))
{
}
return 0;
}

Output:
404

Read about Macro

Saturday, April 10, 2010

74

Predict the output:
int main()
{
int d;
int s();
d=s();
}
s()
{
int i=0,j=i<3,k=j<2>=i;
return i++*++k-j++;
}

Saturday, March 27, 2010

73

Predict the Output:
int main()
{
if(1||printf("Logical Programs"))
printf("C Interview Questions");
return 0;
}

Output:
C Interview Questions

for explanation read this post

72

Predict the output:
int main()
{
if(-1)
printf("hi");
if(1)
printf("Bye");
return 0;
}

Output:
hiBye

71

Prdict the output:

int incr(int *pt);
int main()
{
int value=2;
int *ptr;
ptr=&value;
printf("%d %d",*ptr,value);
incr(ptr);
printf("\n%d %d",*ptr,value);
return 0;
}
int incr(int *pt)
{
(*pt)++;
return 0;
}

Output:
2 2
3 3

Friday, March 26, 2010

Display bits of signed numbers

int main()
{
int a=-3,i=0,j;
int b[32];
clrscr();
printf("%d\n",a);
for(j=0;j<32;j++)
b[j]=0;
i=0;
while(a!=0)
{
if(a&01)
{
b[i]=1;
}
else
{
b[i]=0;
}
i++;
a>>=1;
}
for(j=31;j>=0;j--)
{
printf("%d",b[j]);
}
getch();
return 0;
}

Binary to Decimal

int main()
{
int a[8],i,j=1,b=0;
clrscr();
for(i=0;i<8;i++)
scanf("%d",&a[i]);
for(i=7;i>=0;i--)
{
b+=(a[i]*j);
j=j*2;

}
printf("%d",b);
getch();
return 0;
}

Wednesday, March 24, 2010

70

#define les(a,b)     a<b?a:b
int main()
{
int i=1,j=3,k;
k=les(i++,++j);
printf("%d %d %d",i,j,k);
return 0;
}

Output:
3  4   2


Logic:
While compilation happens ,macro identifier is replaced with associated character sequence.
i.e.,
les(i++,++j) is replaced by i++<++j?i++:++j
so now

k=i++<++j?i++:++j;
1 4 2


In this statement 1<4,so i++ is assigned to k(statement is true so 1st variable is returned i.e,i++).
k=i++;
2

so k=2 and 'i' is increment later(post increment) .so 'i' will became 3.

69

int main()
{
int i=0,j;
j=(i++,++i);
printf("%d %d ",i,j);
return 0;
}

Output
 2  2 

Logic:
As per the Associativity of the brackets ,j=++i(brackets associativity is left to right).The statements is equal to i++,j=++i;

These statements will executed like this:

line 1:     i++,         //'i' is equal to 1.
line 2:     j=++i;         //j=++i(preincrement) so j=2 and i=2



','(comma)operator is used to group one or more statements.

68

int main()
{
int i=0,j=0;
int k=2;
(i,j)=k;
printf("%d %d %d", i,j,k);
return 0;
}


Output:
0     2     2
Note:
gcc compiler will show one warning.
Logic:
We know that associativity of brackets always be left to right(i.e <-- in this direction). so j=k is assigned . The statement is equal to i,j=k;

67

int main()
{
int i,j;
i=3<3+2;
j=4<<1+1;
printf("%d %d ",i,j);

return 0;
}





Output

1     16

Logic:
In c program arithmetic operators (/,*,+,-) has higher precence than the conditional operators( < , > ,< =, > =,!=,==) and shifting operators( << , >> ). so addition operation is performed first.
i=3<5 results truth value 1.
j=4<<2 results 16.(read shifting post to know about the shifting operator).

66

int main()
{
printf("%d ",5>6==0);
printf("%d",5>6>0);
return 0;
}

Output:

1
0

Logic:

I think no need to explain about the operators > and == .

In 1st printf function:
5>6 gives false truth value(0).
0==0 gives true truth value(1).
so it'll print 1 in buffer.
in 2nd printf function:
5>6 gives false truth value (0)
0>0 gives false truth value(0)
so it'll print 0 in buffer.

Monday, March 22, 2010

65

Predict the Output:
int main()
{
int *p=(int *)2000;
scanf("%d",2000);
printf("%d",*p);
return 0;
}

if input is 20 ,what will be print

Output:
20
Logic:
we know that pointer stores address.
we can specify address directly like this
"(datatype *)address"

pointer 'p' is pointing the address 2000.
scanf("%d",2000) will write the data which is entered by user into the address 2000.
so 20 will store in the address 2000.
now *p will points the value at the address,so 20 will print.

printf %d

This is infosys question:
Write a program to print %d.

int main()
{
printf("%%d");
return 0;
}

Logic:
%% will print the % character in buffer.
d will print in buffer as usual.

63

write a c program for print your name .but,your name may be small letter mean print a capital letter or your name may be capital letter mean print a small letter .example \\enter ur name : sankar The name is: SANKAR (or) enter your name:SAnkar The name is:saNKAR

Program:
int main()
{
char a[]="Shark Srini";
int i;
for(i=0;a[i] ! ='\0';i++)
{
if(a[i]==' ')
continue;
else if(a[i] < 97)
a[i]+=32;
else
a[i]-=32;
}
printf("%s",a);
return 0;
}

Output:
sHARK sRINI

62

Write a program to write a data at the 1000 address.

Program:
int main()
{
scanf("%d",1000);
return 0;
}

61

Predict the output:
int main()
{
struct shark
{
int b;
#define selva() b
}s;
s.selva()=4;
printf("%d",s.b);
return 0;
}

Output:
4
Logic:
we know that macro processor is constant and replace with value while expanding.
selva() is equal to b.
when expanding the code ,s.selva is replaced by s.b .
so s.b=4.

60

Predict the output:
int main()
{
int i=2,b=24;
printf("%d");
return 0;
}

Output:
24
Logic:
%d will returns the Current Garbage value.
24 is stored at last so 24 will be in garbage.

59

Predict the output:
int main()
{
printf(5+"sharksrini");
return 0;
}

Output:
srini
Logic:
consider a string operation.
char a[]="sharksrini";
a+1 will specifies address in which 'h' is stored .if we print a+1 ,it will print "harksrini"
a+2 will specifies address in which 'a' is stored. if we print a+2,it will print "arksrini"
likewise
a+5 will print "srini"

In this program 5+"sharksrin" will print "srini" in the same procedure above specified

Variable stored in memory

Local/auto variables, which is declared in stack area of memory
global variable-->data memory
register variable-->CPU registers
static variable-->main memory

Frequency of numbers in a number

Write a program to print frequency of number in a number.

Reverse of an Number

write a program to reverse an number.

58

#define ft(a,b) (a)*b
int main()
{

printf("%d",ft(2+1,3+1);

return 0;
}


Output:
10
Logic:
function is replaced with constant expression like this
(2+1)*3+1=3*3+1=9+1=10

Sunday, March 21, 2010

57

5
4
7
6
1
0
3
2
13
12

  • Write a program to print the above sequence.

  • Logical Hint: Use bitwise operator.

    Program:
    int main()
    {
    int i;
    for(i=0;i<10;i++)
    {
    printf("%d",i^5);
    }
    return 0;
    }

    Some Interview Questions

  • Minimum no. of queues needed to implement the priority queue?
    Ans:Two

  • What are the notation used in Evaluation of Arithmetic Expressions using prefix and prefix forms?
    Ans: Reverse polish notations and Polish

  • In tree construction which is the suitable efficient data structure?
    Ans: Linked List

  • In tree structure ,which is,efficient considering space and time complexities?
    Ans:Complete Binary Tree

  • What Operator performs pattern matching?
    Ans:LIKE

  • What are the different phases of transaction?
    ans: Redo &undo
  • Bit counting

    int main()
    {
    int function(int n);
    int i,n=2,b;
    clrscr();
    for(i=0;i<100;i++)
    {
    b=function(i);
    if(b==n)
    {
    printf("%d",i);
    }
    }
    getch();
    return 0;
    }
    int function(int n)
    {
    int i=0;
    while(n!=0)
    {
    if(n&01)
    {
    i++;
    }
    n>>=1;
    }
    return i;
    }

    Output :


    3
    5
    6
    9
    10
    12
    17
    18
    20
    24
    33
    34
    36
    40
    48
    65
    66
    68
    72
    80
    96

    Constant Pointer Vs Pointer Constant

    Do you think that the terms Pointer Constant and Constant Pointer are the same?

    If your answer is 'YES' then I can Prove it wrong through The below example...!



    int main()
    {
    int i=2;
    int const *p;
    p=&i;
    printf("%d",*p);
    return 0;
    }

    int main()
    {
    int i=2;
    int *const p=&i;
    *p=3;
    printf("%d",*p);
    return 0;
    }

    OUTPUT:






    2

    3


    Logic:

    Constant Pointer:

    Syntax: datatype const *ptr
  • In case of a Constant Pointer , the value remains constant where the address can be varied.
  • In case you provide any modification in the value it will result in an error.

  • Pointer Constant:

    Syntax: datatype *const ptr
  • Incase of a Pointer Constant Address remains constant.
  • Pointer must be initialized (i.e., int *const p=&i)
  • If you modify the address, it will result in an error where as you can modify the value of the pointer.


  • NOTE:
    In const *p , *p points value so value is constant.
    In *const p, p points address so address is costant.

    But the following programs make error:




    int main()
    {
    int i=2;
    int const *p;
    p=&i;
    *p=3
    printf("%d",*p);
    return 0;
    }

    int main()
    {
    int i=2,j;
    int *const p=&i;
    p=&j;
    printf("%d",*p);
    return 0;
    }

    Sunday, March 14, 2010

    56

    In the following program which one will run without any errors?:





    #define a 2
    int main()
    {
    int b=2;
    switch(b)
    {
    case a:
    printf("%d",b);
    break;
    }
    return 0;
    }

    int main()
    {
    int a=2,b=2;
    switch(b)
    {
    case a:
    printf("%d",b);
    break;
    }
    return 0;
    }


    Answer


    The first program.
    we can use only constants in case.
    in first program a is defined using #define so it's constant.
    in second program a is defined using integer datatype(obviously it's variable not constant).

    Saturday, March 13, 2010

    pointer operation with &,*

    int main()
    {
    int *p;
    int a=2;
    p=&a;
    printf("%d",&p); //prints the address of p.
    printf("%d",*(&p));//prints the address of a.
    printf("%d",*(*(&p));//prints the value a.
    printf("%d",&a); //prints the address of a.
    printf("%d",*(&a));// prints the value of a.

    return 0;
    }

    Logic:
    & operator specifies the address of the variable.
    * operator specifies the value at the address. ( *(1000) gives the value at the address 1000).

    For this program:
    assume address of p is 1000 and a is 1002.
    &p will print the 1000 in output screen.
    *(&p) is equal to *(1000) . This will give the value at address 1000(i.e address of a because p stores the address of a) .so now *(&p) is equal to 1002(address of a).

    *( *(&p) ) is equal to *(1002) . so this will give the value at the address 1002. i.e.,2


    &a results the address of a.
    *(&a) results the value at address .so results the value of a.

    Saturday, March 6, 2010

    55

    #include< stdio.h >
    main()
    {
    int a=1;
    a=a+++a;
    printf("%d",a);
    }


    output


    3
    Logic:
    while reading the token , it is read by character by character from left to right(->)
    so, the operation is equal to a++ +a.
    op.jpg

    break statement

    break statement terminate loop.

    int main()
    {
    int i;
    for(i=0;i<2;i++)
    {
    printf("Before break statement");
    break;
    printf("After break statement");
    }
    return 0;
    }

    Output


    Before break statement

    Continue Statement

    The remaining loop statements after continue statement are skipped and the computation proceeds directly to the next pass through the loop.

    consider this eg:

    int main()
    {
    int i;
    for(i=0;i < 2;i++)
    {
    printf("Before continue statement");
    continue;
    printf("After continue statement");
    }
    return 0;
    }

    Output


    Before continue statement
    Before continue statement


    Logic


    In this program,compiler show one warning "Unreachable Code".
    all statement which are after continue statement will be skipped.
    so printf("After continue statement"); is unreachable code(This statement will not be execute ).
    so the operation will be like this,



    Friday, March 5, 2010

    54

    int main()
    {
    int i;
    for(i=1;i<10;i++)
    {
    if(i<4)
    {
    continue;
    printf(" %d",i);
    }
    else
    {
    printf("%d",i);
    break;
    }
    }
    return 0;
    }

    Output:


    4
    Logic
    To understand the logic of the program,
    know what is meant by break , continue statement.
    Break Tutorial
    Continue Tutorial

    53

    int main()
    {
    int a=13;
    if((a=a--<<4>>++a))
    printf("\n%d ",a);
    else
    printf("\n %d",a);
    return 0;
    }

    Output


    0
    Logic
    To understand the logic of this program read this tutorials
    Shifting Tutorial
    Pre &post increment
    post increment
    assignment operation as condition

    52

    int main()
    {
    static int a=4;
    if(a!=0)
    {
    --a;
    main();
    printf("%d",a);
    }
    return 0;
    }

    Output


    0000


    Logic:
    "static variables retain their values even after the function terminates.
    as a result if a function terminates and then is re-entered later,static variables retain their former values."


    21-4.jpg
    until the 'a' becomes zero the main will be called .
    when the condition if(a!=0) is false i.e,a=0, the main function come to end and returned to previous main function.
    in previous main function printf("%d",a); will work so 0 will be printed(static variable so a remains zero).such that all main will be return like this

    main->main->main->main->main
    0 0 0 0 a values

    51

    char ch(char a,char b)
    {
    if(a,b)
    return a;
    else
    return b;
    }
    int main()
    {
    char a='s',b='t';
    char *p=&a;
    char c;
    c=ch(a++,++*(*(&p)));
    printf("\n %c",c);
    return 0;
    }

    Output

    t

    Logic:
    Know what is &,* operator
    post increment

    50

    #ifdef NULL
    #define NULL 2
    #endif
    #ifndef NULL
    #define NULL 4
    #end if
    int main()
    {
    printf("%d",++NULL^NULL|0);
    return 0;
    }

    Output:


    Compilation Error

    Logic:
    we can't modify constant variables. all variables defined in the define are constants.
    click hereto know about the #define macro.
    #ifdef macro is like the if control statment. #ifdef checks the constant is defined before.if so it'll execute the loop.
    #ifndef macro checks the constant is not defined before. if so it'll execute the loop.

    49

    #define shark(a,e,i,o,u,s,r) u##r##i##e
    #define srini shark(z,n,i,t,m,k,a)
    int srini(int a)
    {
    printf("%d",a);
    return 0;
    }
    int main()
    {
    char a=56;
    srini(a+32);
    }

    Output:


    Compilation Error

    Logic:


    we can't define two main functions. but now you may ask where the another main function. srini function is equal to main function. How?
    to know click here

    48

    int main()
    {
    float a=1;
    switch(a)

    {
    case 1:
    printf("%f",a);
    break;
    defualt:
    printf("Default");
    }
    return 0;
    }

    Output:


    Compilation Error

    LOGIC


    we can use only integer constants or character constants in switch case. We can't use float values in switch case.

    47

    #define function(a) printf("Answer: %d",a)
    int main()
    {
    extern int a;
    printf("%d",printf("%d",function(scanf("%d",&a))));
    return 0;
    }
    int a=20;

    if your input is '2' ,then predict the output.

    Output:


    Answer: 1 9 1

    Logic:
    To understand the logic read the followingpost
    printf function returns
    Scanf inside printf

    46

    #define float char
    int function(char a)
    {
    if(a<48)
    return function(a+8);
    printf("%c",a+32);
    return 0;
    }
    int main()
    {
    float b='\n';
    function(b);
    return 0;
    }

    Output:


    R

    45

    int main()
    {

    int a[]={40,41,50};
    char str="shark srini";
    int *ptr;
    *(a+1)=*(a+2)-45;
    ptr=a;
    printf("%s",&1[a][str]);
    }

    Output:


    srini

    45

    int main()
    {
    int **pt;
    char ***ptr;
    double ****ps;
    printf("%d",sizeof(pt));
    printf("%d",sizeof(**pt));
    printf("%d",sizeof(****pt));
    return 0;
    }

    Output


    2 2 8

    44

    int main()
    {
    int a[][2]={1,2,3,4,5,8,9,10,11}
    int (*p)[2],*pt[2];
    p=a;
    pt[0]=a[0];
    printf("%d %d",(*(p+3))[1],++*(++pt[0]+3));
    return 0;
    }

    Output


    10 6

    43

    typedef struct ss
    {
    char *a;
    long b;
    double c;
    }srs;
    int main()
    {
    srs r,*s;
    s=&r;
    s+=4;
    printf("%d",s);
    return 0;
    }

    Question:
    If address of 'r' is 1000 then what would be the output?

    Output:


    1056

    42

    int main()
    {
    int *a=(int *)2000;
    *a=1;
    a++;
    printf("%d",*((int *)2000));
    }

    Output:


    1

    41

    int main()
    {
    int i=1,j=2,k=3;
    i=i^j&j<<1|++k>>i;
    printf("%d",i);
    }


    Output:


    3

    40

    int main()
    {
    int a=2,*p;
    p=&a-1;
    printf("%d %d",*++*&p,&*&a);
    }

    Output:


    2 -12



    here -12 is address of 'a'.

    39

    int main()
    {
    int a=1,b=2;
    a=++a < < b++;
    b=b++ > > ++a;
    printf("%d %d",a,b);
    }

    Output:


    9 1

    38

    union shark
    {
    int a;
    char *b;
    double d;
    long l;
    }s;
    int main()
    {
    printf("%d %d",sizoef(s),sizeof(s.b));
    return 0;
    }

    Output


    8 2

    37

    int main()
    {
    int *const p;
    int i=2;
    p=&i;
    printf("%d",*p);
    return 0;
    }

    Output


    Compilation Error.

    36

    #define max 4
    int main()
    {
    int i;
    for(i=0;i < max;i++)
    {
    printf("%d",i);
    max=max+i;
    }
    }

    Output


    compilation Error.

    35

    #define s(b) b*2-1
    int main()
    {
    printf("%d",s(3)*2);
    return 0;
    }

    Output:


    4

    34

    int main()
    {
    int a,b,c,d;
    a=(1,2,3);
    b=1,2,3;
    c=1,(2,3);
    d=(1,2),3;
    printf("%d %d %d %d",a,b,c,d);
    return 0;
    }

    Output:


    3 1 1 2

    33

    int main()
    {
    int a;
    scanf("%d",&a)+1;
    a=printf("%d",a++)+a;
    printf("%d",a);
    }
    if you give input as '1 what 's output?

    Output


    1 3

    32

    int main()
    {
    printf("%c",printf("hi")["shark srini"]);
    return 0;
    }

    Output:


    hia

    Friday, February 26, 2010

    31

    Predict the Output:
    int main()
    {
    int i=40,j=14;
    int *const p=&i;
    p=&j;
    printf("%d %d",*p,*p++);
    return 0;
    }

    Wednesday, February 24, 2010

    30

    #include< stdio.h >
    int main()
    {
    int a;
    printf("enter no.");
    scanf("%d",&a)+1;
    a=printf("%d",a++)+a;
    printf("%d",a);
    return 0;
    }


    Output:


    enter no.
    2


    2
    4

    Note: This program will show one warning i.e.,"Code Has no effect" (scanf("%d",&a)+1; this line does nothing).

    29

    #includedeny stdio.h >
    #define shark(s) printf("%d",s);
    #define srini(b) b-1
    int main()
    {
    shark(2*srini(2))
    }


    Output:


    3

    Tuesday, February 23, 2010

    Frequency of characters

    Finding frequency characters in a string. Eg: "shark srini" in this string
    s:2
    h:1
    a:1
    r:2
    k:1
    i:2
    n:1


    #include< stdio.h >
    #include< string.h >
    int main( )
    {
    char a[]="shark srini",c[26];
    int k,i,j,n,l,s;
    k=0;
    n=strlen(a);
    c[k]=a[k];
    k++;
    for(i=0;i < n-1;i++)
    {
    s=1;
    for(j=0;j < k;j++)
    {
    if(c[j]==a[i])
    {
    s=0;
    }
    }
    if(s!=0)
    {
    c[k]=a[i];
    k++;
    }
    }
    for(j=0;j < k;j++)
    {
    l=0;
    for(i=0;i < n;i++)
    {
    if(c[j]==a[i])
    {
    l++;
    }
    }
    printf("%c :\t %d\n",c[j],l);

    }
    return 0;
    }



    Output:


    s:2
    h:1
    a:1
    r:2
    k:1
    i:2
    n:1

    Saturday, February 20, 2010

    Scanf inside printf

    #include < stdio.h >
    int main()
    {
    int a,b;
    printf("%d",scanf("%d %d",&a,&b));
    }


    Output:


    2


    Logic:


    To know what'll return by scanf function and printf function , read these post:

    scanf returns
    printf function returns

    28

    #include < stdio.h >
    #include < string.h >
    int main( )
    {
    char a[ ]="shark srini";
    char *b;
    int i;
    b=&a[0];
    for(i=0;i<4;i++)
    {
    printf("%c",*a);
    b++;
    }
    }


    Output:


    s
    s
    s
    s

    s

    Logic:


    click here to get the logic.

    Friday, February 19, 2010

    Reverse Number

    Reverse of 12345 is 54321. should not use string.
    #include < stdio.h >
    int main()
    {
    int n,a,b;
    printf("ENTER THE Number");
    scanf("%d",&n);
    b=0;
    while(n!=0)
    {
    a=n%10;
    b=(b*10)+a;
    n=n/10;
    }
    printf("REVERSED NUMBER:");
    printf("%d",b);
    }


    Output:


    ENTER THE NUMBER:
    54321

    REVERSED NUMBER:
    12345

    Thursday, February 18, 2010

    27

    #include < stdio.h >
    int main()
    {
    int a=2;
    printf(&a["shark srini"]);
    return 0;
    }


    Output:


    ark    srini


    Logic:


    To understand the logic of this program, please read the following post:

    NUMBER[ARRAY]

    string different operation

    26

    #include < stdio.h >
    int main()
    {
    char a[]="shark selva";
    printf("%c",a[0]);
    printf("%d",&a[1]);
    printf("%s",&a[1]);
    printf("%c",*(&a[1]));
    printf("%c",*a);
    printf("%s",&a);
    return 0;
    }


    Output


    s
    -14
    hark selva
    h
    s
    shark selva


    Logic:


    string array 'a' stores characters like this





















    space








    a[0]

    a[1]

    a[2]

    a[3]

    a[4]

    a[5]

    a[6]

    a[7]

    a[8]

    a[9]

    a[10]

    a[11]

    s

    h

    a

    r

    k

    s

    e

    l

    v

    a

    \0

    so obviously a[0] prints 's' character .
    in case of &a[1] ,it specify the address of a[1].
    so printf("%d",&a[1]); prints the address of a[1].
    in case of printf("%s",&a[1]) ,it'll print all character starting from address a[1]
    i.e., it 'll print "hark selva"(because i give %s format specifier instead of %d).
    printf("%c",*(&a[1]));
      '*' is known as indirection operator(indirectly specifies 
    the value in specified address).

    so *(&a[1]) specifies the 'h' character.
    *a is equal to a[0]
    it will print the 's' character.
    &a is equal to &a[0].
    this will print the "shark selva" string.

    Tuesday, February 16, 2010

    25

    #include< stdio.h >
    main()
    {
    int **p,*a,b=1;
    a=&b;
    p=&a;
    printf("%d",*p);
    }

    Output


    -14


    -14 is address of 'a'

    24

    #include < stdio.h >
    main()
    {
    int a=0;
    if(a=0)
    {
    printf("hi");
    }
    printf("hello");
    }

    output


    what'll be the output?

    Monday, February 15, 2010

    23

    #include< stdio.h >
    #define merge(a,b) a##b
    main()
    {
    int c=merge(4,0);
    c=c+1;
    printf("%d",c);
    return 0;
    }

    Output:


    41


    Logic:


    in previous post i said about merging operator. Again i'm going to explain what it's.
    Merging Operator:
    ## this will merge given strings and produce concatenation of that strings.
    in case of numbers it gives result in the form of numbers.
    in this program, i defined a function merge, that'll merge the a and b using merging
    operator(a##b). so it'll gives result as 40.

    22

    #include< stdio.h >
    main()
    {
    char a;
    a='A'-17;
    printf("%c",a);
    return 0;
    }


    Output


    0


    Logic:


    It's simple logic.
    ASCII value of character 'A' is 65.
    65-17=48
    48 is ASCII value of '0'.

    21

    #include < stdio.h >
    main()
    {
    float a=1;
    switch(a)
    {
    case 1:
    printf("%f",a);
    break;
    case 1.0:
    printf("%f",a);
    break;
    default:
    printf("%f",a);
    }
    return 0;
    }


    Output


    Compilation Error:
    Switch Selection expression must be integral type.
    Constant expression required


    Logic:



    Switch Selection expression must be integral type:
    we can give only integer or characters variable to switch case.

    Constant expression required:
    we can use only integer constants or characters in case.

    Sunday, February 14, 2010

    20

    #include< stdio.h >
    main()
    {
    char a[]="srini selva";
    printf("%c\n",*(&a[1]));
    printf("%s\n",a+6);
    printf("%c\n",*(a+6));
    }


    output


    r
    selva
    s

    19

    #include< stdio.h >
    main( )
    {
    int a=1,b=2;
    (a+b)++;
    printf("%d %d",a,b);
    }


    Output:


    compilation error:
    Lvalue required.


    Logic:


    we can increment the value of variables not constant. In this program (a+b)++ is given. (3)++ is not valid. We can't store values in constant. i.e., 3=3+1 is wrong. In left side we should use variable not a constant. So it shows "Lvalue require."

    18

    #include < stdio.h >
    main()
    {
    int a=1;
    void function(int ,int ,int ); //Function Declaration
    function(++a,a,a++); //function call
    }
    void function(int i,int k,int j)
    {
    printf("%d %d %d",i,k,j);
    }


    Output


    3      2       1


    Logic:


    a++     post increment
    ++a     pre increment
    ( ) associativity(in which direction operation done) is right to left( i.e., <--)
    so first it starts from right i.e.,a++ (post increment) so a=1 is passed to j.
    and a will be incremented to 2,then a=2 is passed to k.and finally ++a is preincrement so a=3 and it's passed to i.

    Saturday, February 13, 2010

    17

    #include< stdio.h >
    main()
    {
    int i=1;
    if(++i < 1 && ++i >1)
    {
    printf("False");
    }
    printf("%d",i);
    }

    Output


    2


    How?


    &&   Logical AND operator.
    As far as AND operator concerned if both expression true ,it'll return true otherwise false.if first expression is false ,no need of checking the next expression.
    In this program ++i < 1 is false (2 < 1, so false). so no need of checking the next expression.++i > 1 will not work. so 'i' will incremented for 1 time.
    so i=2.

    16

    #include< stdio.h >
    main()
    {
    int s=1,r=2;
    {
    int s;
    s=r+4;
    }
    printf("%d",s);
    }


    output


    1


    How?


    Though i declare 's' variable for 2 times,there's no error. How?
    Block scope( '{ }' ):
    if you declare a variable a block ,it's valid for that particular block only.
    so
    {
    int s; //valid for this block
    s=r+4;
    }
    when it come outside of the block, above declared 's' only valid. so s=6 is inside the block and s=1 is outside of the block.

    To understand clearly consider this program.,
    #include < stdio.h >
    main()
    {
    int a=2;
    {
    int a=3;
    printf("%d",a);
    }
    printf("%d",a);
    }

    OUTPUT


    3   2

    15

    #include< stdio.h >
    #define a 4
    main()
    {
    a++;
    printf("%d",a);
    }

    output


    Compilation Error:
    Lvalue Required.


    How?


    #define defines a constant not a variable and also 'a' is replaced with '4' while expanding code.
    a++; will be 4++;. we can't increment constant value(4=4+1).
    NOTE:constant won't changes its value.
    variable can change its value.

    14

    #include< stdio.h >
    #define a (5+2)
    main()
    {
    int c;
    c=2*a;
    printf("%d",c);
    }


    Output


    14


    how?


    as said in last post the variable defined using macro is replaced with that constant.
    Here c=2*a; will become as c=2*(5+2);
    '()' has higher precedence than the '*' so, 5+2 will be added and multiplied to 2.i.e
    b=2*7=14;

    Friday, February 12, 2010

    13

    #include< stdio.h >
    #define a 3+2
    main()
    {
    int b;
    b=a*2;
    printf("%d",b);
    }


    click Output


    How?


    #define is used to define a constant.
    in this program 3+2 is defined for 'a'.
    so whenever you use a variable,it'll replace the a with 3+2.

    b=a*2; in this expression a is replaced by 3+2 while creating expanded code.
    so it'll become like this b=3+2*2;

    we know that '*' has high precedence than +.
    2*2 will multiplied and it'll added to 3.
    so b=7.

    Escape Sequence characters

    #include < stdio.h >
    main()
    {
    printf("sharksr");
    printf("\bselvan");
    printf("\rsabari");
    printf("\n shark selva");
    printf("\t tab");
    }

    OUTPUT


    sabariselva
    sharkselva     

    How?


    this escape sequence operators are used in printf function to design our output in
    certain structure.
    \n Next line
    \t Tab
    \r Carriage return( returns to the first of the line).
    \b Backspace(backspace one character )

    12

    #include < stdio.h >
    main()
    {
    char r[]="orns"
    printf(" %c",r[1]);
    printf(" %c",1[r]);
    }

    Sunday, February 7, 2010

    10

    #include < iostream.h >
    main()
    {
    int a=1,b=2;
    a=a,b;
    b=(a,b);
    cout < < a < < b;
    }
    OUTPUT

    1    2
    How?

    as said in last post 'a=a,b;' is equal to
    a=a;
    b;
    so a=1.
    in next line i.e 'b=(a,b)':
    in this expression we use brackets(brackets has 'right to left associativiy' i.e
    operation will be in this direction <-- )
    so b=b will be assigned.i.e.,
    b=b;
    a;
    so b=2.

    Saturday, February 6, 2010

    ',' operator:

    ',' operator is used in c program for separating two variables and separate the two
    statements and some other.
    eg:
    int a,b;
    and we can use ',' operator like this also
    printf("hi"),printf("hi");
    These two statements will be written like this also

    int a,
    b;

    and

    printf("hi"),
    printf("shark");

    EXAMPLE CODING:
    #include< stdio.h >
    main()
    {
    int a=1,
    b=2;
    printf("%d",a),
    printf("%d",b);
    }

    OUTPUT


    1   2

    Friday, February 5, 2010

    EFocus Business - Earn money

    "Need your investment in just 4 days and want to earn from 5th day without working ."
    Hi friends , i'm currently doing an Efocus business and earning more from that. In this business, no need of hard work. Invest just 600rs , and earn more money by referring friends (just like me).
    For more details:
    cellphone: 9445710457

    Thursday, February 4, 2010

    Logical Operator and Bitwise Operator

    #include< stdio.h >
    main()
    {
    int i=4,j=8;
    printf("%d %d %d",i|j,i|j&&j|i,i&j);
    }

    output:


    12      1      0


    How?


    if you familiar with bitwise operator and Logical operators,you can easily understand the logic of this program.

    Logical Operators and it's operations

    || (OR Operator)- this will check any one expression is true. if so,it'l
    return true value (1) otherwise false(0) will be return.
    eg:
    a=1 > 2||2 < 3;

    here 'a' value will be 1 because 2 < 3 expression is true.
    && (And Operator)- This will check both expression is true. if so ,it'll
    return true value otherwise false will be return.
    Eg:
    a=1 > 2&&2 < 3;
    here 'a' value will be 0 because 1 > 2 is false though 2 < 3 is true so it'll return
    false(0).
    Biwise operator

    | (bitwise OR operator)- This will do OR operation to given numbers and give a resultant
    number.
    Eg:
    a=1 | 3 ;
    here a=3 because this will OR the 1 and 3 like this

    1: 0001
    3: 0011
    _____
    result: 0011 -- > 3
    & (Bitwise AND operator)- This will do AND operator to given number and give a
    resultant number.
    Eg:
    a=1 & 3;
    here a=1 .
    The And Operation:
    1: 0001
    3: 0011
    ________
    result: 0001 - > 1

    ^ (Bitwise X-OR operator)-Normal ex-or operation.
    Eg:
    a= 1 ^ 3;
    here a=2.
    The Ex-or operation:
    1: 0001
    3: 0011
    ________
    result: 0010 -- > 2.
    > > (Right shift operaot). shifting bits to right side(simply LSB will be removed
    and MSB will be 0 .
    eg:
    a=2 > > 1;
    here '2' bits are shifted 1 bit right side.

    2: 0010 > > 1
    the result 0001.
    so a=1.

    < < ( Left Shift Operator). Shifting bits to left side ( MSB will be removed and LSB
    will be 0.
    Eg:
    a=2 < < 1;
    here '2' bits are shifted 1 bit left side.
    2: 0010 < < 1
    the result 0100.
    so a=4.

    Wednesday, February 3, 2010

    NUMBER[ARRAY] -part 2

    CODING:

    #include< stdio.h >
    main()
    {
    printf("%c",2["INISROH"]);
    }

    OUTPUT


    I



    Refer the previous post to know logic of this program.

    Number[character array]

    Today i'm going to give you a interesting logical programs.

    CODING:

    #include< stdio.h >
    int main()
    {
    char a[]="INISROH";
    printf("%c",4[a]);
    return 0;
    }

    OUTPUT


    R


    How?


    a[i]=*(a+i) (we can represent a[i] using the ptr like this)
    *(a+i)=*(i+a)
    *(i+a)=i[a]
    here 'i' is 0,1,2,...n
    n is length of string

    Friday, January 29, 2010

    Scanf Returns a value

    #include< stdio.h >
    main()
    {
    int b,a;
    printf("Enter the Inputs");
    a=scanf("%d %d",&a,&b);
    printf("\n a value is :\t");
    printf("%d",a);
    }


    OUTPUT


    Enter the Inputs
    1
    2
    a value is: 2


    what! you amazed with this program?!
    In previous post i explained about printf returns a value.
    Then i think about that what will be returned by scanf() function.
    when i run the program,i got output like this. Go through this program ,definitely
    you'll understand the logic.


    #include< stdio.h >
    main()
    {
    int a;
    printf("Enter the value");
    a=scanf("%d",&a);
    printf("a value is %d",a);
    }


    OUTPUT


    Enter the value
    2
    a value is 2


    you may get the logic from above programs.

    scanf function will return "No of successful inputs getting from user".
    for eg:
    i=scanf("%d",a);
    In this statement scanf function gets one successful input from the user. so it will return 1.

    Tuesday, January 26, 2010

    OR operator in expression

    Logical OR(||) operator expression doesn't check second operand, if the first operand is true. i.e., d=a > b||b > c. In this expression if first operand is true then no need of checking second operand. Because though second operand is true or not, the expression will be true.
    consider this program:
    #include< stdio.h >
    main()
    {
    int a=1,b=2;
    if(a < b++||++a < b)
    printf("%d %d",a,b);
    }


    output:
    1 3

    in this program a < b++(1 < 2 post increment so b will increment after checking) is true. So the second operand ++a < b isn't checked by OR . Now a=1,b=3 Because of second operand is not checked a won't be incremented.

    For loop logical Program

    We already know that, for loop syntax:
    for(initialization;condition;reinitialization)
    {
    }
    see this program.

    #include< stdio.h >
    main()
    {
    int i=1;
    for(i=0;i<2;)
    {
    i++;
    printf("%d",i);
    }
    }


    output:
    0 1

    Size of variable - #define

    If you create variable using #define , size of that variable is 2 bytes.

    #include< stdio.h >
    #define a 1
    main()
    {
    printf("%d",sizeof(a));
    return 0;
    }

    Printf() as a argument of another printf()

    #include< stdio.h >
    main()
    {
    printf("%d",printf("selva"));
    }

    output:
    selva 5

    for explanation see last post. If you can't able to understand, comment your queries.

    Printf( ) function returning value.

    Printf function returns "Number Of successful characters printed by printf function which includes white space ".


    For eg:
    a=printf("hi");
    Obviously the printf function will print "hi".It prints two characters in console. So the printf function will return value 2.

    int main()
    {
    int a=printf("selva");
    printf("%d",a);
    return 0;
    }

    selva 5

    In the above program "selva" is printed in the console. The printf returns 5 to a.

    Monday, January 25, 2010

    #define Macroprocessor

    One of macroprocessor #define used to define value for variable. The value will be constant for that variable. Can't change in run time.
    eg:
    #define a 10
    'a' value is constant has always 10.

    #define can be used before all functions or inside functions like usual variable declaration.


    #include< stdio.h >
    #define a 1
    main( )
    {
    #define b 2
    printf("%d %d", a,b);
    }

    not only used for define values but also define function.
    #define f(a,b) a+b
    this function will get two arguments a and b and return addition of them.


    #include< stdio.h >
    #define fn(a,b,c) a+(b*c)
    main()
    {
    printf("%d",fn(1,2,3));
    }


    output:
    7

    Left shift - Right Shift

    #include< stdio.h >
    int powerof2(int a)
    {
    int p=0;
    for(int i=0;i p+=2;
    return p;
    }
    main()
    {
    int n,shift,ln,rn;
    printf("enter number and shifting number");
    scanf("%d %d",&n,&shift);
    ln=n*powerof2(shift);
    rn=n/powerof2(shift);
    printf("Left shift: %d", ln);
    printf("Right shift: %d", rn);
    }

    Lpart5

    There are two type of bit shifting.
  • Left shift: shifting bits in left side. < < left shift operator. Use left shift like this number< < shift.
    For Eg:
    4<<1
    This will shift one bit left side of number 4. I.e., 4 is 0100. If we shift one bit of 4 in left side, 1000 will be the answer i.e 8.
    Simply we can calculate the value after left shifting by formula i.e., "number*2^shift". 4*2=8.

  • Right shift: > > this will shift bits in right direction. 4 > > 2. The value after shifting is 0001. Simply we can calculate right shift by this formula "number/2^shift".
    4/2^2 =4/4=1.
  • Lpart4

    #include< stdio.h >
    main()
    {
    int a=1,b;
    b=++a*2*++a;
    printf("%d %d", a,b);
    }
    Output
    3 18


    in this program there's two preincrement for 'a'. So 'a' will be incremented two times before multiplication. So now a= 3. so b=3*2*3=18.

    Lparts

    #include< stdio.h >
    main()
    {
    int a=1,b;
    b=a++*2*++a;
    printf("%d %d",a,b);
    }

    output:
    3 8

    as a explained in last post , this program also works.

    Saturday, January 23, 2010

    #include< stdio.h >
    main()
    {
    int a=1,b;
    b=a++*2*--a;
    printf("%d %d",a,b);
    }


    Output:

    1 0

    How it works?
    refer precedence operator table
    pre decrement(--) will decrement value of 'a'. now a=0.
    a++ is post increment, so a will be incremented after multiplication. so
    b=0*2*0=0
    but after this a will incremented and a=1(because of post increment).

    Lparts

    #include< stdio.h >
    main()
    {
    int a=1,b;
    b=a++*2;
    printf("%d %d",a,b);
    }




    Output:

    2 2

    How it works?
    we use post increment so increment operation of 'a' will be done after multiplication . so b=2. then a value will be incremented so a=2

    Find odd or even without Relational operators

    #include< stdio.h >
    main()
    {
    int a;
    scanf("%d",&a);
    if(a&1)
    printf("odd");
    else
    printf("even");
    }

    How it works?
    '&' logical 'and' operator, this will 'and' a with 1.
    all odd numbers have last bit value '1'. for example








    Odd
    1 have 0001
    3 have 0011
    5 have 0101








    Even
    2 have 0010
    4 have 0100
    6 have 0110


    from above table we can come to one decision i.e., odd numbers will have last bit value as '1'. so we'and' the number with '1'. if result is 1, then 'if' will work and print "odd". if result is 0, then 'else' will work and print "even"

    Tuesday, January 5, 2010

    1

    #include < iostream.h >
    int a=0;
    int fn1()
    {
    return(a+=1);
    }
    int fn2(int a)
    {
    return(a++);
    }
    main()
    {
    int a=40;
    cout< < a;
    fn1();
    cout< < a;
    fn2(a++);
    cout< < a;
    }

    output:

    40

    40

    41

    Scope of variable

    #include < iostream.h >
    int a=10;
    main()
    {
    int a=2;
    cout < < a < < ::a;
    }

    what'll be the answer.
    In last post i wrote about scope of variables. It'll print local variable value 2 first and print 10. For use global variable we use scope of variable operator i.e '::' .

    Global variable vs Local variable

    Today i'm going to explain you about scope of variable. Score of Variable is area of variable exists.
    Local Variable:

    local variable exists only in certain function. Outside of that function, variable is unknown. Simply local variable is for local.

    Eg: function()
    {
    var declaration
    }
    Global variable

    Global variable is possessed by all function. It's exists in global level of the program. Eg:
    var declaration
    function();
    main()
    {
    }

    Sunday, January 3, 2010

    Addition of numbers without arithmetic operator

    Hi friends today i'm going to explain about a program to add numbers without arithmetic operators.

    #include< iostream.h >
    int add(int a,int b)
    {
    if(!a)
    {
    return(b);
    }
    else
    {
    return add((a&b) < < 1,(a^b));
    }
    }
    main()
    {
    int c;
    c=add(1,2);
    cout < < c;
    }

    How it works ?

    This is one of question asked in interview. This program based on half adder circuit. In half adder , summation is produce result of ex-or of two bits carry produce result of and of two bits. Now the logic in this program is if carry exists, we should do ex-or operation with addition and carry which is shifted 1 position.

    in this program
    when add function is called a=1 b=2. If(!a) won't true. So else will work. It call add function itself (recursive function) with values add operation of a and b and shifted one position left.add operation is
    a= 0001
    b= 0010
    ------------
    0000 and this will shifted one position left. Now it's same zero.
    and ex-or operation is
    a=0001
    b=0010
    ----------
    0011
    now this two two values passed to add function. Now if(!a) work. Because not of 0's will 1's so it'll return b as sum of a and b.

    Saturday, January 2, 2010

    Lpart1

    #include< stdio.h >
    void main()
    {
    static int a=4;
    printf("%d",a--);
    if(a)
    main();
    }
    what'll be the answer?

    1. run continuously

    2. compile time error

    3. print 3 2 1 0

    4. print 4 3 2 1












    For Static variable, memory is statically allocated .
    At first a is declared and initialize to 4 .
    a=4 is printed and then a is decreament to 3.
    Then if(a) will work main will called itself . But note that now a is 3. Then it'll print 3 and a is decreament to 2. Like that it 'll print 4 3 2 1. When a=0 then if(a) is false . So it won't call main function.