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

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