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..

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