What will be output
(1) void main()
{
clrscr();
printf("%d",sizeof(3.8));
getch();
}
output: 8
Explanation: 3.8f is float constant, 3.8 is double constant and 3.8L is long double constant .Here are finding size of double constantan which is 8.
For more detail click here (data type)
(2) What will be output ?
void main()
{
char *str1="powla";
char *str2="er";
clrscr();
printf("%s\b\b%s",str1,str2);
getch();
}
output: power
Explanation: \b escape sequence back the cursor one position left .We are using two /b so after writing str1 cursor is at the position of l of powal .So when it write er it will override the la so output will be power.
(3)What will be output ?
void main()
{
int a=270;
char *p;
p=(char *)&a;
clrscr();
printf("%d",*p);
getch();
}
output: 16
(4)What is missing statement of in the following program ?
Void main()
{
int sort(int,int);
int I;
i=sort(5,6);
}
Int sort(int a,int b)
{
int c;
c=a;
a=b;
b=c;
return a;
}
Ans: Function sort returning a value but we are not using return value so there is weastge of two byte memory. So missing statement is ,there should statement which uses the return value.
(5)Write following in term of if and else :
void main()
{
int a=1,b=2,c=3;
clrscr();
if(a==5&&b==6&&c==7)
printf("india");
else
printf("pak");
getch();
}
Ans:
void main()
{
int a=1,b=2,c=3;
clrscr();
if(a==1)
{
if(b==2)
{
if(c==3)
{
printf("india");
}
else
{
printf("pak");
}}
else
{
printf("pak");
} }
else
{
printf("pak");
}
getch();
}
(q)
Give the memory representation of
Struct xxx
{
char a;
int b;
char c;
};
Ans: Memory representation :
More detail click here (union)
(q) Write the following program in term of switch and case ?
void main()
{
int a=3;
if(x>2)
{
printf(“INDIA IS BEST”);
}
else{
printf(“PAK IS BEST”);
}
}
Ans: if condition always return two value.
1 if condition is true.
0 if condition is false.
So program is
void main()
{
int x=3;
switch(x>2)
{
case 0:printf("India is best");
break;
case 1:printf("Pak is best");
}
getch();
}
(q)
void main()
{
int far *a=(int far*)0x50000011;
int far *b=(int far*)0x50010001;
int huge *c=(int huge*)0x50000011;
int huge *d=(int huge*)0x50010001;
clrscr();
if(a==b)
printf("I know C");
else
printf("I don't know C");
if(c==d)
printf("\nI know C");
else
printf("\nI don't know C");
getch();
}
Output:
I don’t know C
I known C
Explanation: far pointer always compare its whole far address.Since both or not eaual so first output is :I don’t know C
Huge pointer always compare its physical adderss both c and d are representing same physical address so a and b are equql.
More detail click here
(q) What will be output ?
#define power(a) #a
void main()
{
clrscr();
printf("%d",*power(432));
getch();
}
Output : 52
Explanation:
# is stringzinging operator. It make the string constant of any data. So 432 is converted into “432” by macro power .Now *”432” means first char which is 4.Since we are using %d so it will print ascii value of char 4 i.e 52
More detail click here (prepro)
(q) What will be output ?
void main()
{
int arr[]={1,2,3,4,5,6};
void xxx(int[5]);
xxx(arr);
getch();
}
void xxx(int ch[5])
{
clrscr();
printf("%d",-1[ch]);
}
Output : -2
Explanation:
We are passing the array by xxx function. 1[ch] means *(ch+1) which is ch[1] =2.
(q) What is difference between a,b,c and in following declaration ?
#define xxx char *
typedef char * yyy;
void main()
{
yyy a,b;
xxx c,d;
}
Ans: Both and b are char * type but c is char * type while d is char type.
(q) Write a c program to find the HCF of any two number ?
Ans:
void main()
{
int a,b,c;
scanf(“%d%d%d”,a,b,c);
clrscr();
while((c=a%b)!=0)
{
a=b;
b=c;
}
printf("%d",b);
getch();
}
(q) What will be output ?
Ans:
void main()
{
int a=5;
{
a++;
}
clrscr();
printf("%d",a);
getch();
}
Ans :6
q) What will be output ?
Ans:
void main()
{
int a=5;
{
int a=7;
a++;
printf(“%d”,a);
}
clrscr();
printf("%d",a);
getch();
}
Output: 8 5
Explanation: Scope of the auto variable is within {} if it is declared in {}.Also local variable has more priority than global variable.
More detail click here (function)
Saturday, June 21, 2008
Monday, January 7, 2008
If else condition in c
Control structure:
if else
Syntax:
1.
If()
{
Statement1;
Statement 2;
……………
…………….
}
2.
If()
{
Statement1;
Statement 2;
……………
…………….
}
else
{
Statement1;
Statement 2;
……………
…………….
}
3.
If()
{
Statement1;
Statement 2;
……………
…………….
}
else If()
{
Statement1;
Statement 2;
……………
…………….
}
else If()
{
Statement1;
Statement 2;
……………
…………….
}
………………..
………………...
else
{
Statement1;
Statement 2;
……………
…………….
}
If is non zero then condition is true and if part of statement will execute , if is zero then only else part will execute.
e.g
void main()
{
if(-12)
{
printf(“c”);
else
{
printf(“c++);
}
}
Output: c
Explanation: Here expression is -12 it is non zero so condition is true hence only if statement will execute .
Note. If there is only one statement is if or else part then { } is optional
Above program can be written as:
void main()
{
if(-12)
printf(“c”);
else
printf(“c++);
}
Both program has same meaning.
Relation operator (>,>=,<,<=,==,!=) always return
0 if condition is true
1 if condition is false
e.g
void main()
{
int a=0;
if(a==0)
printf(“I know c very well”);
else
printf(“I am c programmer”);
}
Output: I know c very well.
Explanation : Relation expression a==0 i.e 0==0 is true so it will return 1 So only if part will execute.
Logical error : Suppose any good c programmer when he is writing a very long program by careless he forget to write two equal sign :
void main()
{
int a=0;
if(a = 0)
printf(“I know c very well”);
else
printf(“I am c programmer”);
}
In this program there is not any compiler error but its out put will else part ie I am c programmer ,due to logical error.
To remove such type error try to write the if expression in the following way:
void main()
{
int a=0;
if(0==a)
printf(“I know c very well”);
else
printf(“I am c programmer”);
}
Output: I know c very well.
Now if you will by mistake forget to write two equal sign then it is compiler error.
void main()
{
int a=0;
if(0 = a)
printf(“I know c very well”);
else
printf(“I am c programmer”);
}
Output: compiler error
What will be output:
void main()
{
int a=60;
clrscr();
if(a>20)
printf("c");
else if(a>40)
printf("c++");
else if(a>=60)
printf("java");
else
printf("pascal");
getch();
}
Output: c
Explanation : In if else control statement if if statement is true then it does not look else i part event that is true.So in this program all if condition is true but only statement of first if has executed.If statement can be nested i.e we can write if statement within another if statement i.e
if()
{
Statement1;
Statement 2;
……………
…………….
if()
{
Statement1;
Statement 2;
……………
…………….
}
else
{
Statement1;
Statement 2;
……………
…………….
}
}
else
{
Statement1;
Statement 2;
……………
…………….
if()
{
Statement1;
Statement 2;
……………
…………….
}
else
{
Statement1;
Statement 2;
……………
…………….
}
}
What will be output:
void main()
{
int a=4,b=6c=9;
if(a==4)
if(b==6)
if(9==c)
printf(“Yes”);
}
Output: Yes
It is better to use logical and (&&) operator instead of nested if for easier gettable.
void main()
{
int a=4,b=6c=9;
if(a==4&&6==b&&c==9)
printf(“Yes”);
}
Output: Yes
if else
Syntax:
1.
If(
{
Statement1;
Statement 2;
……………
…………….
}
If(
{
Statement1;
Statement 2;
……………
…………….
}
else
{
Statement1;
Statement 2;
……………
…………….
}
If(
{
Statement1;
Statement 2;
……………
…………….
}
else If(
{
Statement1;
Statement 2;
……………
…………….
}
else If(
{
Statement1;
Statement 2;
……………
…………….
}
………………..
………………...
else
{
Statement1;
Statement 2;
……………
…………….
}
If
e.g
void main()
{
if(-12)
{
printf(“c”);
else
{
printf(“c++);
}
}
Output: c
Explanation: Here expression is -12 it is non zero so condition is true hence only if statement will execute .
Note. If there is only one statement is if or else part then { } is optional
Above program can be written as:
void main()
{
if(-12)
printf(“c”);
else
printf(“c++);
}
Both program has same meaning.
Relation operator (>,>=,<,<=,==,!=) always return
0 if condition is true
1 if condition is false
e.g
void main()
{
int a=0;
if(a==0)
printf(“I know c very well”);
else
printf(“I am c programmer”);
}
Output: I know c very well.
Explanation : Relation expression a==0 i.e 0==0 is true so it will return 1 So only if part will execute.
Logical error : Suppose any good c programmer when he is writing a very long program by careless he forget to write two equal sign :
void main()
{
int a=0;
if(a = 0)
printf(“I know c very well”);
else
printf(“I am c programmer”);
}
In this program there is not any compiler error but its out put will else part ie I am c programmer ,due to logical error.
To remove such type error try to write the if expression in the following way:
void main()
{
int a=0;
if(0==a)
printf(“I know c very well”);
else
printf(“I am c programmer”);
}
Output: I know c very well.
Now if you will by mistake forget to write two equal sign then it is compiler error.
void main()
{
int a=0;
if(0 = a)
printf(“I know c very well”);
else
printf(“I am c programmer”);
}
Output: compiler error
What will be output:
void main()
{
int a=60;
clrscr();
if(a>20)
printf("c");
else if(a>40)
printf("c++");
else if(a>=60)
printf("java");
else
printf("pascal");
getch();
}
Output: c
Explanation : In if else control statement if if statement is true then it does not look else i part event that is true.So in this program all if condition is true but only statement of first if has executed.If statement can be nested i.e we can write if statement within another if statement i.e
if(
{
Statement1;
Statement 2;
……………
…………….
if(
{
Statement1;
Statement 2;
……………
…………….
}
else
{
Statement1;
Statement 2;
……………
…………….
}
}
else
{
Statement1;
Statement 2;
……………
…………….
if(
{
Statement1;
Statement 2;
……………
…………….
}
else
{
Statement1;
Statement 2;
……………
…………….
}
}
void main()
{
int a=4,b=6c=9;
if(a==4)
if(b==6)
if(9==c)
printf(“Yes”);
}
Output: Yes
It is better to use logical and (&&) operator instead of nested if for easier gettable.
void main()
{
int a=4,b=6c=9;
if(a==4&&6==b&&c==9)
printf(“Yes”);
}
Output: Yes
Sunday, January 6, 2008
Switch case control statement in c
It is used when we have to choose one option from list of many options.
Syntax:
switch ()
{
case :
Statement 1;
Statement 2;
………………
………………
case :
Statement 1;
Statement 2;
………………
………………
……………………………………….
……………………………………….
default :
Statement 1;
Statement 2;
………………
………………
}
Note :
1. Here default is optional i.e It is possible a switch case control statement which has not default.If not any case is matching then default will execute. Also all case is optional i.e it is possible a switch case which has not any case and not any default .
Example:
void main()
{
int a=6;
switch(a)
{
}
}
2. must be int type and unique
Example:
void main()
{
int a=3;
clrscr();
switch(a)
{
case 1,2,3: printf("I know java");
break;
case 3,2,1: printf("I know c++");
break;
case 2,1,3: printf("I know c");
break;
default: printf("I know pascal");
break;
}
getch();
}
Output : compiler error
Explanation : Here in each constant expression has three integers which are separated by comma operator .Comma operator always return right most expression ( here integer ).
So 1,2,3 will return 3
3,2,1 will return 1
2,1,3 will return 3
Since here two case has 3 but must be unique.
What will be output:
void main()
{
clrscr();
switch(2)
{
case 4: printf("I knwon java");
case 2: printf("I know c++");
case 5: printf("I know c");
default: printf("I know pascal");
}
getch();
}
Output :
I know c++
I know c
I know pascal
Explanation:
In switch case control statement after executing matching case is control dose not come outside of switch but it transfer to the next case and execute its statement till it does not encounter switch ending }.
Note: To exit from switch use break keyword.
Example;
void main()
{
clrscr();
switch(2)
{
case 4: printf("I knwon java");
break;
case 2: printf("I know c++");
break;
case 5: printf("I know c");
break;
default: printf("I know pascal");
}
getch();
}
Output: I know c++;
What will be output:
void main()
{
clrscr();
switch(2)
{
case 1: printf("I knwon java");
break;
case 2: printf("I know c++");
break;
case 3: printf("I know c");
continue;
default: printf("I know pascal");
continue;
}
getch();
}
Output:
Compiler error.
Explanation: continue keyword cannot be used in switch case. It is used in loop. If there is any loop in switch case then we can use continue.
Nesting of case is possible i.e a switch case can be inside another switch case.
(1) WAP to convert the following program in switch case?
void main()
{
int a=6;
if(a>27)
printf(“c);
else
printf(c++);
}
Ans: If else statement has only two case either true or false. Expression a>27 will return 1 if is true and 0 when expression is false .So we can write :
void main()
{
int a=6;
switch(a>27)
{
case 1: printf(“c”);
case 2: printf(“c++”);
}
}
Syntax:
switch (
{
case
Statement 1;
Statement 2;
………………
………………
case
Statement 1;
Statement 2;
………………
………………
……………………………………….
……………………………………….
default :
Statement 1;
Statement 2;
………………
………………
}
Note :
1. Here default is optional i.e It is possible a switch case control statement which has not default.If not any case is matching then default will execute. Also all case is optional i.e it is possible a switch case which has not any case and not any default .
Example:
void main()
{
int a=6;
switch(a)
{
}
}
2.
Example:
void main()
{
int a=3;
clrscr();
switch(a)
{
case 1,2,3: printf("I know java");
break;
case 3,2,1: printf("I know c++");
break;
case 2,1,3: printf("I know c");
break;
default: printf("I know pascal");
break;
}
getch();
}
Output : compiler error
Explanation : Here in each constant expression has three integers which are separated by comma operator .Comma operator always return right most expression ( here integer ).
So 1,2,3 will return 3
3,2,1 will return 1
2,1,3 will return 3
Since here two case has
What will be output:
void main()
{
clrscr();
switch(2)
{
case 4: printf("I knwon java");
case 2: printf("I know c++");
case 5: printf("I know c");
default: printf("I know pascal");
}
getch();
}
Output :
I know c++
I know c
I know pascal
Explanation:
In switch case control statement after executing matching case is control dose not come outside of switch but it transfer to the next case and execute its statement till it does not encounter switch ending }.
Note: To exit from switch use break keyword.
Example;
void main()
{
clrscr();
switch(2)
{
case 4: printf("I knwon java");
break;
case 2: printf("I know c++");
break;
case 5: printf("I know c");
break;
default: printf("I know pascal");
}
getch();
}
Output: I know c++;
What will be output:
void main()
{
clrscr();
switch(2)
{
case 1: printf("I knwon java");
break;
case 2: printf("I know c++");
break;
case 3: printf("I know c");
continue;
default: printf("I know pascal");
continue;
}
getch();
}
Output:
Compiler error.
Explanation: continue keyword cannot be used in switch case. It is used in loop. If there is any loop in switch case then we can use continue.
Nesting of case is possible i.e a switch case can be inside another switch case.
(1) WAP to convert the following program in switch case?
void main()
{
int a=6;
if(a>27)
printf(“c);
else
printf(c++);
}
Ans: If else statement has only two case either true or false. Expression a>27 will return 1 if is true and 0 when expression is false .So we can write :
void main()
{
int a=6;
switch(a>27)
{
case 1: printf(“c”);
case 2: printf(“c++”);
}
}
Subscribe to:
Posts (Atom)