473,395 Members | 1,658 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

I got it to compile but it did somthing strange

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
Nov 13 '05 #1
8 2038
Victor Lamberty wrote:
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`.
--
Chris "electric hedgehog" Dollin
C FAQs at: http://www.faqs.org/faqs/by-newsgrou...mp.lang.c.html
C welcome: http://www.angelfire.com/ms3/bchambl...me_to_clc.html
Nov 13 '05 #2
In article <bf***********@biggoron.nerim.net>, no****@example.com says...
Try this:

#include <stdio.h>

int main()
{
printf("%s\n", "hello-computer");
return 0;
}


Why is the above better than:
printf("hello-computer\n");

??
Nov 13 '05 #3
On Thu, 24 Jul 2003 02:53:00 +0028, Victor Lamberty
<vi*************@sasktel.net> wrote:
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

----------------------------------------------------------------------------
Nov 13 '05 #4

"Joona I Palaste" <pa*****@cc.helsinki.fi> wrote in message
news:bf**********@oravannahka.helsinki.fi...
Randy Howard <ra**********@foomegapathdslbar.net> scribbled the following:
In article <bf***********@biggoron.nerim.net>, no****@example.com says...
Try this:

#include <stdio.h>

int main()
{
printf("%s\n", "hello-computer");
return 0;
}

Why is the above better than:
printf("hello-computer\n");

??


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.

--
BC
Nov 13 '05 #5
Burne C wrote:

"Victor Lamberty" <vi*************@sasktel.net> wrote in message
news:vh************@corp.supernews.com...
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( )


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

{
printf("hello-computer")


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.
}

and the output looked like this

hello-computer#

I thought that it would be

#hello-computer


HTH

--
BC

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
Nov 13 '05 #6
Victor Lamberty <vi*************@sasktel.net> wrote in message news:<vi***********@corp.supernews.com>...
Burne C wrote:
...
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.
.... 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
Nov 13 '05 #7
On Thu, 24 Jul 2003 02:25:00 UTC, Victor Lamberty
<vi*************@sasktel.net> wrote:
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
Nov 13 '05 #8
On Wed, 30 Jul 2003 11:37:10 UTC, Victor Lamberty
<vi*************@sasktel.net> wrote:

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.


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
Nov 13 '05 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Gianni Mariani | last post by:
Below is an example of how to do static type-checking of an erroneous set of input masks. The drawback is that it's a little too verbose and non-trivial to understand and to make it truly I'd...
0
by: Tony Johansson | last post by:
Hello! I get compile error when compiling using the command javac from the command terminal window(CMD). I have just two classes which are called HelloWorld.java and Slask.java. I have both...
2
by: Tony Johansson | last post by:
Hello! I get compile error when compiling using the command javac from the command terminal window(CMD). I have just two classes which are called HelloWorld.java and Slask.java. I have both...
5
by: Carmine Cairo | last post by:
Hi, I'm working on a project and today I've note a little problem during the compile fase. Here a little piece of code: // 1st version welldone = 0; size = p->getSize(); backbone = new...
10
by: Chris LaJoie | last post by:
Our company has been developing a program in C# for some time now, and we haven't had any problems with it, but just last night something cropped up that has me, and everyone else, stumped. I...
4
by: tony | last post by:
Hello! My question is about calling this method CollectData below but I get a compile error that I shouldn't have because the type parameter is correct. The compile error is the following:...
8
by: jean.daniel.michaud | last post by:
Hi all, Something I don't get. The code is: // snippet on #include <list> #include <iostream> int main()
18
by: sadegh | last post by:
I am using VC++6. I want to draw some simple shapes (e.g. line, circle, pixel) on a dialog box. How can I do this? Thanks a lot
1
by: sevak316 | last post by:
I have a project in Visual Studio 2008 which includes both .c and .cpp files. I have no problem compiling the .cpp file. However, there is a problem in the header of a .cpp file. In the header I...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.