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

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