PDA

View Full Version : C tutorials for a beginner


Tyaedalis
2005.09.11, 08:49 PM
does anyone know of *GOOD* beginner tutorials for C? post them, please.

Andrew
2005.09.11, 08:58 PM
Try this one:

http://www.eskimo.com/~scs/cclass/notes/top.html

or, if you want something simpler & less detailed:

http://www.cocoadevcentral.com/articles/000081.php

Tyaedalis
2005.09.11, 09:16 PM
thanks. i have a question. is there a link between cocoa and c?

Andrew
2005.09.11, 09:24 PM
thanks. i have a question. is there a link between cocoa and c?

The language of choice for Cocoa programming is objective-C. Objective-C is a true superset of C. That means that objective-C is C with stuff added to it. If you want to start writing plain C code in the middle of an objective-C method, you can do so.

Tyaedalis
2005.09.11, 09:30 PM
am having trouble with the

#include <stdio.h>


i dont know what the stdio.h means

i kow it means standard something, but what does it do?
does the program need it?

also, how do i set up Xcode for C compiling?

Andrew
2005.09.11, 09:32 PM
BTW, have you ever programmed before (in any language)?
If you haven't, you should consider learning Python. It's simple and makes a great first language. Here's a great (free) book to learn from: How to Think Like a Computer Scientist: Learning with Python (http://greenteapress.com/thinkpython/thinkCSpy.pdf) (PDF)

Tyaedalis
2005.09.11, 09:34 PM
i have learned the basics of BASIC :P but i cant remember anything from it. i tried learning C before recntly, but got distracted... pythons seems boring. what can you do with it and how do you compile (or interperet) it?

Andrew
2005.09.11, 09:46 PM
am having trouble with the

#include <stdio.h>


i dont know what the stdio.h means

i kow it means standard something, but what does it do?
does the program need it?

also, how do i set up Xcode for C compiling?

#include someFile means "paste the contents of someFile here". You'll find stdio.h in /usr/includes/stdio.h. It defines a bunch of stuff (like the printf function) which allows your program to get input an produce output (hence, the io part).

You don't need Xcode for plain C (although it helps once you start making more complex programs). Use GCC for now. Here's how:

1) open Terminal

2) cd to the directory of your source code (if you don't know how to use a command line, google for "basic unix tutorial")

3) type gcc myFile.c -o whateverYouWantToCallYourProgram

4) run it by typing ./nameOfYourProgram

Andrew
2005.09.11, 09:51 PM
pythons seems boring. what can you do with it and how do you compile (or interperet) it?

Ever heard of BitTorrent? That was written in Python.

To run a Python program, just type python nameOfMyPythonProgram.py

There's no need to compile your code before running it.

Tyaedalis
2005.09.11, 09:55 PM
type that in the terminal?

and i write the code in textedit?

Andrew
2005.09.11, 10:03 PM
type that in the terminal?

and i write the code in textedit?

Yes. Although, you can write the source code in any text editor you like (including Xcode). If you want to write it in TextEdit, make sure you save it as plain text (not RTF).

Tyaedalis
2005.09.11, 10:07 PM
ok, i got the normal

print "Hello, World!"


done. it's exactly the same as BASIC.

i think i will just jump into C, caus ei started on c and i like it.

i wrote this a while back in C:

#include <stdio.h>

int main()
{
char name[20], age[9];
printf("\nWhat is your name? ");
scanf("%s", name);
printf("How old are you? ");
scanf("%s", age);
printf("Your name is %s, and you are %s years old.\n", name,age);
return 0;
}

Andrew
2005.09.11, 10:33 PM
Be careful. There is a SERIOUS buffer-overflow problem with that program. Try entering a really long name and age.

One solution is to limit the number of character that scanf will read:

scanf("%8s", name);
fflush(stdin);

Note that I used 8 instead 9. That's because a C string is an array of characters which end in the NUL character ('\0').

Python hides these kinds of details for you.

Tyaedalis
2005.09.11, 10:45 PM
explain the

fflush(stdin);

please

and in the scanf("%8s", name);
the 8 means only take 8 letters?

Andrew
2005.09.11, 10:54 PM
yes, "%8s" means read up to 8 characters.

fflush(stdin) means empty the standard input buffer so that the next time I call scanf(), there won't be junk in the buffer.

Tyaedalis
2005.09.11, 11:42 PM
I have a problem with thsi code.

this is the result:

[Session started at 2005-09-11 19:40:41 -0700.]

What is your name? Matthew Liebrich
How old are you? Your name is Matthew, and you are Lie years old.

Executable “Testing codes” has exited with status 0.

bold means my input. when there's a space in the name, it takes it as the age...

Tyaedalis
2005.09.12, 01:57 AM
couls someone post a small program that would just show images? if you do, please include the image files! i just want to see which commands and such are needed. maybe a keyboard input code too?

Andrew
2005.09.12, 03:31 AM
when there's a space in the name, it takes it as the age...

That's the way scanf works. Read the man pages on scanf for all the gory details:
type man scanf in a terminal

Andrew
2005.09.12, 03:42 AM
couls someone post a small program that would just show images? if you do, please include the image files! i just want to see which commands and such are needed. maybe a keyboard input code too?

This kind of stuff is not for beginner C programmers. Stick to text only programs for the moment if you want to learn C. If you want to jump right into high-level stuff, start with a higher level language like objective-C, Ruby, Python, or Lua. That said, you may want to check out SDL (http://www.libsdl.org). It's a C library which simplifies these tasks.

I'd strongly suggest learning objective-C + Cocoa if you're in a hurry to do such high level things (on the Mac, anyway).

Tyaedalis
2005.09.12, 09:55 AM
yeah, i want to learn objective-c+cocoa. and SDL can work with objective-c, too right?

Andrew
2005.09.12, 01:04 PM
yeah, i want to learn objective-c+cocoa. and SDL can work with objective-c, too right?

Yes, you can get SDL to work with objective-C... but why? One of the main advantages of SDL is that it's cross-platform (as opposed to Cocoa, which is pretty much Mac-only). About the only thing Cocoa doesn't give you is a simplified HID input (joystick/gamepad) API. Cocoa by itself should have everything you need at the moment.

THE best book on Cocoa is Aaron Hillegass' "Cocoa Programming for Mac OS X" (http://www.bignerdranch.com/products/cocoa1.shtml).

Also be sure to read some of the tutorials on Cocoa Dev Central (http://www.cocoadevcentral.com/).

Tyaedalis
2005.09.12, 07:52 PM
ok. i will. but what do you think i could make better on my current program?

Andrew
2005.09.13, 03:37 AM
do you mean "how can I fix the spacing bug?", or do you mean "what other features could I add to my program to get me practising C?"?

akb825
2005.09.13, 03:42 AM
You could always use fgetc to get the input 1 character at a time until you reach a new line for the name.