I got it to compile but it did somthing strange

V

Victor Lamberty

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( )
{
printf("hello-computer")
}

and the output looked like this

hello-computer#

I thought that it would be

#hello-computer
 
V

Victor Lamberty

Burne said:
You should use "int main( )"
Read the FAQ 11.12 to 11.15
http://www.eskimo.com/~scs/C-faq/top.html



Did you want to print it on a single line and open a new line ?

try this printf("hello-computer\n");

The "\n" insert the new line characters for you.


HTH
Thank you for all of your ideas and I will try them all in time.
I wanted it to print the greeting then return control to the OS(FreeBSD)
like so:

hello-computer
# <- that's my shell prompt

and after using the \n swich it worked. In the future can anyone tell me if
either way has benifets

again Thank-you
 
C

Chris Dollin

Victor said:
Greetings C coders
#include <stdio.h>
main( )
{
printf("hello-computer")
}

and the output looked like this

hello-computer#

I thought that it would be

#hello-computer

Why did you think that? [I'm assuming your shell prompt is '#'.]

You presumably compiled your program, let's call it `hello`, and
wrote

./hello

The newline after the command means that any output will start on
the next line. No prompt will appear, because the shell is still
waiting for the program to finish.

The program outputs `hello-computer`. It doesn't output a newline,
which means, if I recall, that there's no guarantee the output will
appear at all, but supposing it does, there's no newline output so
no newline appears [at this point we are implementation-specific],
the shell says "goody, he's finished" and outputs a prompt, no newline,
so the # appears after the `r`.
 
R

Ryan Molden

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( )
{
printf("hello-computer")
}

and the output looked like this

hello-computer#

I thought that it would be

#hello-computer

It didn't put it before the prompt, your program executed and then
returned control to the OS which promptly (no pun intended)
redisplayed your prompt waiting for your instruction as to what it
shoudl do next. Also you should make it int main(void) just to avoid
getting yelled at by others ;) If you want the prompt to be on a new
line you need to include a newline \n at the end of your
"hello-computer" string.




Ryan Molden


-------------------------[ Student ]----------------------------------

University Of Washington

Dept. of Computer Science and Engineering

----------------------------------------------------------------------------
 
B

Burne C

Joona I Palaste said:
If, for some reason, "hello-computer" contained % signs, printf() would
not be messed up. For hardcoded strings there's not much benefit but if
the string comes from a variable this can save some debugging.

I would recommend "puts"

puts("hello-computer");

You don't have to care about the % and '\n', if you just want to output a line hard-coded.
 
R

Rich Grise

Victor Lamberty said:
....
Thank you for all of your ideas and I will try them all in time.
I wanted it to print the greeting then return control to the OS(FreeBSD)
like so:

hello-computer
# <- that's my shell prompt

and after using the \n swich it worked. In the future can anyone tell me if
either way has benifets

again Thank-you

The '\n' is not a switch - it's a newline character. Thinking of 'either
way' having 'benefits' is kind of misleading: you use a newline
if you want your output to go to the beginning of the next line.

If I say,
printf("H\ne\nl\nl\no\n.\n\n");

the output will look like this:

H
e
l
l
o
..

C:\>

I'm sure it's in some faq or some docs somewhere - I'm trying to
track down docs myself - I have a 1985 copy of MSC with no docs.

Good Luck!
Rich
 
H

Herbert Rosenau

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
 
H

Herbert Rosenau

What book I have not found a book that I like can you recomend one.

As you uses *ix man would be helpful.
Kerninghan & Ritchie, "Programming in C" is a MUST HAVE for each
beginner.

--
Tschau/Bye

Herbert Rosenau
http://www.pc-rosenau.de eComStation Reseller in Germany
eCS 1.1 GA englisch wird jetzt ausgeliefert
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,767
Messages
2,569,570
Members
45,045
Latest member
DRCM

Latest Threads

Top