This
main()
{
int c;
c = getchar();
while (c != EOF) {
putchar(c);
c = getchar();
}
}
or
main()
{
int c;
while (c != EOF) {
c = getchar();
putchar(c);
}
}
*****************************
only change being getchar(); is not used outside loop.
I just wanted to know which is better.
in the first case we are using same statement outside loop and we are not putting EOF character in file(assume that its a file)
The later case is straight forward; here we are putting character into file and then we are checking for EOF. The only problem being c in not initialized. This problem can be solved by setting c=0 or so when its declared.
But i want to know which is better among there.
Please no jokes :)
thanks
Which is most appropriate? C language. Please read fully?
I would go with the second one, You are going to enter the loop even if the value of C is not declared until the loop has been entered. I would have to say there is a 1 in a 99999999999 chance that the deceleration of int c is going to be assigned the value of EOF when it is declared (if i remember right, as soon as your compiler reads int c, there is a random value assigned to it) ........however (i'm more of a C++ man) i'm pretty sure this is legal in C++ and prolly C
main()
{
while(getchar() != EOF){
putchar(getchar());
}:
};
i'm not %100 on this (cuz i'm drunk) but getchar() is going to return a value, because it is not calling by reference, so if you are setting C = to the value returned by the function getchar, why not just throw the method ghetchar() in the parameter??? the compiler is going to look at the value returned by getchar() before the function....(ex: if x = 1 (before the function call) random_function(++x) is going to take the value 2......
If I was you, I would try and get rid of the variable C....
Reply:In the first program, you check for an EOF after the getchar() function, but before the putchar() function.
The second program 'gets' a character and 'puts' it before checking for the EOF. So, the place where you're putting the characters will also get the EOF tacked onto the end. Also, you need to initialize the 'c' variable.
If you want the EOF included (as if you were outputting text to a file), then you need to add an extra putchar() function after the 'while' loop in the first program.
If you want the EOF included, but you're not sure which program is more efficient, then I can't tell you which program is more efficient. Without re-writing the 'while' loop in the program (like a couple other answerers correctly did), one program uses an extra getchar() and the other program would use an extra putchar() - it is a case of whether getchar() has more code overhead than putchar(). The only way to find out would be to disassemble both functions. I know Assembly language, so I can do this, but I don't feel like doing it right now.
Ha Ha.
Reply:this one is better than above 2 programs:
main()
{
int c;
while ((c = getchar()) != EOF)
{
putchar(c);
}
}
Reply:For a bit of an explanation try this -
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment