Greetings C coders
I am new to the world of C and have been trying to compile this
program. I got the result that I wanted but outputed it in a strange way it
put it before the prompt is there a reason in this code. Or is that just a
trait of the KDE Konsole.
/*#I hope that this is a C program*/
/*#define hello-computer the output is hello-computer*/
#include <stdio.h>
main( )
Sould be
int main(void) {
That means: each program returns int to the caller (the shell, other
programs...).
As your program ignores any argument that may be given to it, you
should declare its argumentlist as void - except you're using really a
pre ANSI compiler, but then you should upgrade it immediately to a
more current one.
{
printf("hello-computer")
Here you forgot the declaration of end statement (
. That shows us
you've typed it in instead to copy and paste the program. Please don't
type in code in artikles, always copy it from the soure. This makes it
much easier for us to find the right diagnose.
}
and the output looked like this
hello-computer#
That is because your program lefts the line open, so no as the program
ends the shell puts out simply its prompt in the same line as your
program.
Anyway your program receives the full rights for the console from the
shell. So it is your problem to tell the OS that a line is finished.
So putting '\n' (the char that stands for a newline) in your output at
any position you knows that a line is finished. Here that means
printf("hello-computer\n");
By that, printf contains a security trap. That is it interprets its
first argument. So, ever when that argument contains a format char '%'
it tries to interpret the char thereafter as a command how to convert
the next yet untreated argument.
It is always dangerous to use the first argument to give out text
strings, except unchangeable ones, designed to be format strings. So
if you have to give out something the user has put in or you've readed
from a file you should use
General form:
printf(<format-string>[[, argument][, argument]...]);
Possible forms. delivered from your example:
printf("%s\n", "hello-computer");
or
printf("%s", "hello-computer\n");
The first lefts the whole control to the format string and only pure
text to the arguments following it, the second includes the output
primitives ('\n' and so on) inside the text and hold only the format
control in the format string.
Even as it looks complex, it makes something secure. Now you uses 2
arguments. The first one is the format, containing nothing than a
description of the following arguments and at least one newline char
to start a new line after the printf is done. The second is your
output string. As "%s" says it is a zero terminated string of chars.
That is simply printed out.
In your example is nothing wrong - see this as a hint fur the future
time you have to distinguish between strings you have hardcoded in
your program and strings coming from unsecure sources like console, a
file or somewhere. It is only the first argument of printf that gets
interpreted in that way. '\n' and other escape characters are not
format characters, so they are only text symbols, representing the
unshowable binary characters like newline ('\n'), formfeed ('\f'),
bell ('\b'), tab ('\t') and so on.
Yes, you can put any char in the formatstring as you've already done.
But be sure that you controls really each character in it.
Read your C book for more information about printf.
--
Tschau/Bye
Herbert Rosenau
http://www.pc-rosenau.de eComStation Reseller in Germany
eCS 1.1 GA englisch wird jetzt ausgeliefert