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