1)
#include<stdio.h>
void main()
{
char arr[6]="jaynam" ;
printf("%s",arr);
}
output:- Garbage Value
Explanation:-
Size of a character array should one greater than total number of characters in any string which it stores.In c every string has one terminating null character.this represents end of string.
so in the string "jaynam" there are 6 characters and they are 'j','a','y','n','a','m' and '/o'.size of array six.so array will only store first 6 characters and it will not store null character.
As we know %s in printf statement prints stream of characters until it doesn't get first null characters.since array arr has not store any null character so it will print garbage value...
2)
#include<stdio.h>
void main()
{
int goto=4;
printf("%d",goto);
}
output-Compilation error
Explanation:-
goto is a keyword in c.variable name cannot be any keyword of c language..
3).
#include<stdio.h>
int main(){
int i=1;
i=2+2*i++;
printf("%d",i);
return 0;
}
output-5
Explanation:-
i++ i.e. when postfix increment operator is used any expression the it first assign the value in the expression the it increments the value of variable by one. So,
i = 2 + 2 * 1
i = 4
Now i will be incremented by one so i = 4 + 1 = 5
4)
#include<stdio.h>
int main(){
int a=1,b=2,c=3;
c=a==b;
printf("%d",c);
return 0;
}
output-0
Explanation:-
== is relational operator which returns only two values.
0: If a == b is false
1: If a == b is true
Since
a=2
b=7
So, a == b is false hence c=0
5)
#include<stdio.h>
void main(){
int a=5;
a=a>=4;
switch(2){
case 0:int a=8;
case 1:int a=10;
case 2:++a;
case 3:printf("%d",a);
}
}
output-error
Explanation:-
We can not declare any variable in any case of switch case statement.
6)
#include<stdio.h>
#include<string.h>
void main(){
int i=0;
for(;i<=2;)
printf(" %d",++i);
}
output-1 2 3
Explanation:-
In for loop each part is optional.
7)Program for Subtract two integer numbers without using subtraction operator
#include<stdio.h>
void main(){
int a,b,d;
printf("enter two numbers u want to subtract");
scanf("%d%d",&a,&b);
d=a+~b+1;
printf("ans=%d",d);
}
Output:-enter two numbers u want to subtract
5
4
ans=1
Explanation:-
~ means ones complement
ones complement of 4=1011
add 1 to this that is 1100
binary number for 5=0101
nw add 0101+1100=0001
that is equal to 1
8)
Write a c program without using any semicolon whose output is: jaynam.
#include<stdio.h>
void main(){
switch(printf("jaynam")){
}
}
Output:-jaynam
9)what is the output of the following program??
#include<stdio.h>
void main(){
char arr[10];
arr = "jaynam";
printf("%s",arr);
}
Output:-Compilation error
Explanation:-
Compilation error Lvalue required
Array name is constant pointer and we cannot assign any value in constant data type after declaration.
Translate this page on 8 other languages also
No comments:
Post a Comment