Golgappa.net | Golgappa.org | BagIndia.net | BodyIndia.Com | CabIndia.net | CarsBikes.net | CarsBikes.org | CashIndia.net | ConsumerIndia.net | CookingIndia.net | DataIndia.net | DealIndia.net | EmailIndia.net | FirstTablet.com | FirstTourist.com | ForsaleIndia.net | IndiaBody.Com | IndiaCab.net | IndiaCash.net | IndiaModel.net | KidForum.net | OfficeIndia.net | PaysIndia.com | RestaurantIndia.net | RestaurantsIndia.net | SaleForum.net | SellForum.net | SoldIndia.com | StarIndia.net | TomatoCab.com | TomatoCabs.com | TownIndia.com
Interested to Buy Any Domain ? << Click Here >> for more details...


void main()
{
char c;
while(c=getchar()!='\n')
printf("%d",c);
}
o/p=11 why?

Answers were Sorted based on User's Feedback



void main() { char c; while(c=getchar()!='\n') printf("%d",c); } ..

Answer / biren

11

Is This Answer Correct ?    9 Yes 5 No

void main() { char c; while(c=getchar()!='\n') printf("%d",c); } ..

Answer / suman halder

test cases:
1.
i/p:hi
o/p:11

2.
i/p:hello
o/p:11111

actually,getchar reads from stdin and is line buffered which means it will not return until you press ENTER.

so,look at the evaluation of the expression(c=getchar()!='\n')
here,
getchar()!='\n' evaluates to true until and unless u'll hit enter..that is why,the actual evaluation would look like
(c=(getchar()!='\n'))
so,
1 will be stored into c each time u press a key except enter key as (getchar()!='\n') evaluates to 1(true value of an expression).

finally,content of the buffer would get printed..
thats it..

Is This Answer Correct ?    4 Yes 1 No

void main() { char c; while(c=getchar()!='\n') printf("%d",c); } ..

Answer / hari.11

above friend has posted correct answer,
11 is not the correct answer,
it will take all characters into buffer and will not output any answer until we press '\n' character.
So it would print 1 as many times as number of character pressed before '\n'.

e.g.:
s
o/p: 1

sd
o/p: 11

gdfdd
o/p: 11111

111
o/p: 111


for further queries and discussions, visit..

http://forum.campusmaniac.com/
http://www.campusmaniac.com/

Is This Answer Correct ?    2 Yes 1 No

void main() { char c; while(c=getchar()!='\n') printf("%d",c); } ..

Answer / sumeet saini

It says %d which means it prints integer value. If we want to print character we need to write %c. So the answer is right. If we replace %d with %c . This program will print what ever character we hit from keyboard until we press enter

Is This Answer Correct ?    1 Yes 0 No

void main() { char c; while(c=getchar()!='\n') printf("%d",c); } ..

Answer / abdul qadir

11 is a garbage value at address in memory

Is This Answer Correct ?    6 Yes 6 No

void main() { char c; while(c=getchar()!='\n') printf("%d",c); } ..

Answer / satya

I Think ascii value of \n is 92.

Is This Answer Correct ?    2 Yes 2 No

void main() { char c; while(c=getchar()!='\n') printf("%d",c); } ..

Answer / prashant

The answer would have been 11 only if the while statement would have had a semicolon to finish with , i.e. "while(c=getchar()!='\n');" According to the above code it will generate the ascii of all the characters entered except the newline character.

Regards
Prashant

Is This Answer Correct ?    0 Yes 1 No

void main() { char c; while(c=getchar()!='\n') printf("%d",c); } ..

Answer / deepshree sinha

the while loop is continue until the expression is not false.
when the expression is false that is when c='\n' it will come
out from the loop.then it will print c which is equal to '\n'
whose integer value is 11.so it will print 11.

Is This Answer Correct ?    0 Yes 1 No

Post New Answer

More C Interview Questions

Write a C/C++ program that connects to a MySQL server and checks intrusion attempts every 5 minutes. If an intrusion attempt is detected beep the internal speaker to alert the administrator. A high number of aborted connects to MySQL at a point in time may be used as a basis of an intrusion.

1 Answers  


What is #pragma directive?how it is used in the program? what is its advantages and disadvantages?

2 Answers  


What is data type long in c?

0 Answers  


int main() { Int n=20,i; For(i=0;i<=n;i--) { Printf(“-“); Return 0;

0 Answers  


What is malloc() function?

0 Answers  


please give me answer with details #include<stdio.h> main() { int i=1; i=(++i)*(++i)*(++i); printf("%d",i); getch(); }

3 Answers  


Why can’t we compare structures?

0 Answers  


How we can set and clear bit in a byte using macro function?

2 Answers   L&T, Samsung,


Explain what header files do I need in order to define the standard library functions I use?

0 Answers  


What is a Deque?

2 Answers  


write a program to arrange the contents of a 1D array in ascending order

4 Answers  


Explain the difference between #include "..." And #include <...> In c?

0 Answers  


Categories