What is the output of the program if the input is 12 ?
#include "stdio.h"
char line[80] /*input line*/
int balance_owed ; /*amount owed*/
int main ( )
{
printf ("Enter number of rupees owed:");
fgets(line,sizeof(line), stdin);
sscanf(line, "%d", &balance_owed);
if (balance_owed = 0)
printf("You owe nothing.\n");
else
printf("You owe %d dollars.\n", balance_owed);
return(0)
}
Yesterday's program still needs an answer.
So, we effectively have two problems to solve !
Bye, bye
ADVISORY TO STUDENTS (Answer to "Sir, College is there today ?")
Quote :
Previous Posts
Wednesday, July 07, 2010
Subscribe to:
Post Comments (Atom)
Sir,
ReplyDeletethe output is :
You owe 0 dollars.
Sir the answer posted by mr sai kumar is absolutely right!
ReplyDeletebut i would like to point that the statement terminator ; is missing after the statement
char line[80] and also return(0).
and coming to the yesterday's question assuming that the array starts at memory address 4000 its op would be
4000 4004
4000 4004
4000 4004
exp:
basically the syntax to access the element in 2D array is array[row][col] but here when we say
array[0,0] it is same as array[0]which gives out the address in memory of row no: 0
and array[0,1] is same as array[1] which gives out the address in memory of row no: 1
and array[1,0] is same as array[0] which gives out the address in memory of row no:0
and array[1,1] is same as array[1] which gives out the address in memory of row no:1
and array[2,0] is same as array[0] which gives out the address in memory of row no:0
and array[2,1] is same as array[1] which gives out the address in memory of row no:1
Now the million dollar question is why and what about the first subscript in each of those statement for example array[2,0] is same as array[0] what abt 2 here?
the answer is when compiler goes on to compile the statements like array[1,0] or array[2,0] as per its syntax it expects first the row number in first rectangular [] brackets here there are two separated by comma operator ( , ) the comma operator in c is used to combine two statements in one line say
i++,b++; so here in statement like array[2,0] and array[1,1] the numbers 2 and 1 are ignored bcuz they dont hav any effect and hence it would be evaluated as array[0] and array[1].
that is why we get the op which is addresses of row 0 and row1 in the given array. which would be different on different computers and its dynamic even on same computer each time u run the addresses might be different!
ya had there been any statement like this
printf("%d",array[printf("C is deep C!"),0);
the o/p would have been
C is deep C! 4000
because here there's a valid statement which is a function printf()which on execution prints the string "C is deep C!"
which is a real fact!
oops i made a typo mistake in the last printf() used in my explanation in previous post which i meant
ReplyDeleteprintf("%d",array[printf("C is deep C!"),0]); note ']'
that's it!
Manoj
ReplyDeleteI am happy with your 'approach' towards programming. Keep it up.
Sai Kumar,
You are correct.
Can you analyse and bring out the vital observation from this program. Meaning : Why was this question asked in the first place. What is the lesson one has to learn ?
Well done though