Posted by TheNymph on 13:17:00 03-07-2002
*waves* My name is Kris, I'm 24 years old (gosh but I feel old compared to some of you! ), I've got a semester of experience in Ada from college. I would have gone further, but the teacher scared me. It happens. So, I've decided to get back into the programming languages after a good....oh...6 years, with teaching myself C.
From Ada there are differences, and there aren't. My head goes through the same logic, so it feels familiar. Anyways, I was doing that silly little Hello World program (doesn't everyone start with that in every language? I distinctly remember writing something similar in Ada) and I had a few questions. I went through another C Programming tutorial Intro, and it confused me even more.
So, let's start from the beginning, shall we?
#include (got the fact that stdio.h is a header file and it's included in the very beginning for the program to run, but what does stdio.h actually do?)
main()(got that down....that with the open brace starts the block, and the closing brace ends it)
printf("hello, World!\n"); (what does the \n mean? what exactly does it do and why's it there?)
return 0; (why are we returning 0? I've noticed this line is at the end of alot of small programs....is this a sign for the program to end?)
*curtsies and steps off the newbie box with an embarrassed grin*
~Nymph
Posted by KaGez on 13:50:00 03-07-2002
First:
Hello and welcome to the YPN!
hope to see ya around in the channel
anyways:
1.) in stdio.h you will find the prototypes for example for printf, and almostt all other C's IO functions. Other functions for example are
fopen, fclose, fprintf, sprintf and so on, just everything you need for doing IO. If just includes the prototypes of the functions, which are needed for compiling the stuff.
2.) I think main() is clear even tho, you could make it
int main(int argc, char **argv[])
but I think you still won7t need it. You should only use it if you want to get command line parameters
3.) printf is simple. It just outputs the text inside the "" inside the brackets. Another example would be
printf("Integer value of string is: %d", character);
where "character" is a variable of the type "char", which contains 1 character (for example "d"). It will print out the ASCII value for the character that the variable "character" contains. %d is used to output decimal values. makes sence, eh? you can also use %f for floats, %s for strings and so on. You can look that up in a C reference for example
printf("String contains: %s", variable);
will print the contents of "variable", but only if "variable" is of the type char.
Anyways, the "\n" at the end does a line break (do I need to explain further? ) It just gets the cursor to the next line.
4.) return 0;
This is fact is confusing, since functions return 1 on success, and not 0... It will also work with "return 1;" , or even with "return;" only. To be honest, "I have _no_ idea why we return 0!" *grin*
I hope it helped you a little bit
[addsig]
Posted by TheNymph on 14:21:00 03-07-2002
Thanks KaGez. That helped alot. Any ideas as to why we use % for the %d, %s and %f?
I'm sure I'll drop into IRC soon. I spend most of my days in IRC on darkmyst with my online RPG group in our OOC chat rooms.
Posted by fsvara on 14:24:00 03-07-2002
Characters that have a "\" before them in printf usually mean something special. \n is the newline, as kagez said. If you are on a unix system, do "man ascii" for a list of all ASCII characters and their "escape character" (that's what they're called) representation.
Some escape characters you might need are \t for tab, \0 for NULL, the character that marks the end of a string in C, or \a for a beep.
As for the "return 0" part, I do not know why unix thinks programs shuld return 0 on success, which is usually defined as FALSE. However, doing "return 1" instead is not a very good idea. If you run a unix-like system, you can do the following:
blah.c:
#include
int main()
{
printf("hi\n");
return 1;
}
Now, the unix shells usually use the operator "&&" for saying "if it worked, run xxx".
So, we compile the above program, call it "blah" and do "./blah && echo worked".
What will this output? Only "hi"!
Now, change the program to return 0, compile it, and do the same again. It will now print.
hi
worked
So, esentially you just return 0 when your program was successful and did what it was supposed to. If it failed, you return 1. That is useful for scripts for example, so they can stop executing when one program they are calling reports it failed, by returning 1.
Btw, for getting parameters from the commandline, that's *argv[] and not **argv[].
Posted by KaGez on 08:29:00 03-08-2002
those things (\) are called "escape characters" as svara said, and they are called "escape characters" because you usually ascape something with it. For example if you'd do something like
printf("He said: "noooo!"\n");
you'll get a error, because C "thinks" that the string was ended after the :, where the " is placed. To avod this for example you use the "escape character" \ . The it'd look like:
printf("He said: \"noooo!\"\n");
This is valid
To print a "/", what shold you do? easy:
printf("\\");
This should print you a single \ on the screen (I'm not sure if it was \\\ or \\, just try it)
[addsig]
Posted by robost86 on 11:17:00 03-08-2002
Just wanted to congratulate you to your choice of programming language. Maybe that's because you're older and wiser
Have fun, and welcome to the YPN!
PS: I hope to see you learning assembly language after you're done with C
Posted by KaGez on 11:31:00 03-08-2002
bah! gah!
I've learned a bit of asm in the last days... wanna know what I have to say about it?
It sux!
No, seriously. Maybe the binary size is smaller, but on my PC, with my 1GHz I don't have _any_ speed boost... I'll stick with C, and I think I'll try to make C++ the language of my choice, since it seems to be _great_ for game dev
[addsig]
Posted by fsvara on 12:27:00 03-08-2002
/me doesn't think you'd experience any significant speed boost with any language when the program is small (and i suppose you didn't code something big in asm if you tried it the last few days only), unless you really benchmark it...
what sense does a 500% speed boost on 10microseconds do for the user? in most cases nothing, as you won't (or rather: can't) notice it.
Posted by robost86 on 12:42:00 03-08-2002
I guess KaGez doesn't want to realize it's not magic that makes the PC work... _That's_ there most important reason to learn asm. It's faster, it's smaller - but like he said, that doesn't matter in most cases (in some cases it does, though). If you're happy with slow and big program, that you don't understand in detail how they work - then asm is nothing for you
Posted by robost86 on 12:47:00 03-08-2002
A little note for svara: If that 10ms code piece is run 100'000 times per second, you WILL notice the difference. This situation appears in many games. You have to know _where_ to use asm, and where you should not use it. C is enough for most tasks, but in some situations, assembly language is needed (or at least speeds the thing up a bit).
Posted by KaGez on 14:21:00 03-08-2002
tell me something that can be done in asm, and not in C... no, I mean it serious, not to flame you or something.
[addsig]
Posted by fsvara on 16:02:00 03-08-2002
lilo
Posted by Govtcheez on 16:06:00 03-08-2002
Not familiar with lilo, but what does it do that couldn't possibly be done in C?
Posted by fsvara on 19:33:00 03-08-2002
i'm not familiar with bootloaders, ask robert about that
you can't write bootloaders with using assembly tho.
Just check out lilo's source - it's like 50% asm.
Posted by Govtcheez on 19:39:00 03-08-2002
Yeah, but I'm pretty sure that it could be written in C...
Posted by KaGez on 00:01:00 03-09-2002
fsvara:
apt-get source lilo
now look at the source. It's completely in C!
[addsig]
Posted by fsvara on 10:23:00 03-09-2002
no it isn't. the .c files are, yes.
But it's the .S files that contain the asm.
All in all, lilo is 6795 lines of C code and 9882 lines of assembler
Posted by robost86 on 12:37:00 03-09-2002
Boot loaders don't HAVE to be coded in asm, but then you have to add some extentions to the language.
We don't only have the question if it's possible to do something, but if it's a good idea. You could rewrite gcc in VB, but that's certainly a BAD idea. Some things just shouldn't be written in high-level languages, even though it's possible.
Posted by KaGez on 13:21:00 03-09-2002
isn't gcc written entirely in C ?
[addsig]