Red Antigua Logo
Yet another piece of web.
Search this site (by Google)
Tools    (top)
Your IP
Check a site for broken links
(W3C)

Perl modules    (top)
Tree::Numbered::Tools
(CPAN)
Perl tutorials    (top)
Perl modules
HTML::Template
CGI::Application
Cookies with CGI::Application
Upload files with CGI::Application
Download files with CGI::Application
Redirect with CGI::Application
CPAN shell
Install DBD::mysql from the CPAN shell
Perl trim function
Validate an IP with Perl
Run suid Perl scripts under Apache
Perl taint mode
Perl date functions with Date::Calc

In Spanish
Curso de Perl

C tutorials    (top)
C - Introduction
C - Absolute beginner's Emacs
C - Examples for beginners
C - Makefile examples
C - Autotools examples
Server configurations    (top)
DNS
Apache
Apache Authentication and Access Control
mod_perl on FreeBSD
MySQL
MySQL add account
phpMyAdmin
Squid
DHCP

UNIX on Windows    (top)
MSYS2 - UNIX environment for MS Windows 32/64 bits
Apache setup on Windows
MySQL setup on Windows
PHP setup on Windows
Perl setup on Windows
Emacs setup on Windows
PuTTY
WinSCP
GIMP on Windows
MinGW - gcc on Windows
MSYS - UNIX-styled shell on Windows
msysDTK - autotools on Windows
GDB for MinGW on Windows

Misc. FreeBSD/UNIX    (top)
'portupgrade' on FreeBSD
'ipf' on FreeBSD
'pf' on FreeBSD
'su' on FreeBSD
Mount an ISO image under FreeBSD
Load the correct sound driver under FreeBSD without knowing what sound card you are using
Simultaneous sound channels on FreeBSD
FreeBSD network stuff
DOS-to-UNIX file conversion
favicon.ico on UNIX
Emacs tips
Command Line Calculator
Save multimedia streams with 'mplayer'
xargs - solution to 'Argument list too long'
Process multiple images from the command line using 'ImageMagick'
Turn the system bell off under X Windows
Process each line in an input file from the command line (or in a shell script)
How to keep a program running in the background using 'nohup'
How to remove symbolic links in the current directory using 'find' and 'rm'
How to remove Emacs backup files in the current directory and all subdirectories using 'find' and 'rm'
How to execute .profile without logging in
Configure X to handle non-English characters
How to move /var to /usr/var

Redirect a web page    (top)
Redirect to another web page
Apache redirect
C redirect
Perl redirect
PHP redirect
HTML redirect
JavaScript redirect

Javascript    (top)
Trim function
Login form
Register form
Popup window

C tutorials - Introduction
C tutorials - Introduction
Introduction
The tutorials
Links
Introduction top
This is an introduction to my C tutorials (and related programming tools).
Before continuing, I would recommend you to read the excellent article Teach Yourself Programming in Ten Years.
Then you should ask yourself if you really want to learn C (or any programming language). ;-)
Theoretically, you could read through this tutorial quickly, but do not expect yourself to be a C programmer without practice, practice, and more practice.
The goal is to learn about how to use a complete development environment, including the C language, the compiler, and related tools (Emacs, make, gdb, SVN, autotools, etc.).

Why yet another tutorial:
When I learn, I prefer to learn by example.
So I created these tutorials full of examples, as much for myself as for anyone else who might have interests learning C by example.

How to learn:
Everyone has her/his preferred way to learn, of course, but my simple tip is to write a C program or a Makefile completely from scratch instead of using copy/paste - then you will see if you remember the syntax of the document you are writing, and you will learn much quicker.

Tutorials, examples, or guides?:
You should see the tutorials as a collection of examples rather than complete guides.
I have added links to manuals, guides, and other tutorials, but have avoided as much text as possible.
That is because I don't think there is a lack of information on the Net - there is just too much of it... ;-)

A word about platforms:

UNIX/BSD/Linux:
If you found my website, I guess you at least have some interest in UNIX/Linux-stylish platforms.
Most of the software tools described in this set of tutorials were originally developed on UNIX/Linux.
If you are on such a platform, you probably already have everything you need, already installed.
If not, you can easily install any missing software using ports, packages and/or source code.

MAC OSX:
Open Source development is available for Mac OSX using Xcode. Most the UNIX/Linux tools are available for Mac OSX as well. Check the Mac OSX developer links for details.

MS Windows:
MS Visual Studio is not treated here.
MSYS2 is the environment of choice, with package management included.
The advantage of MSYS2 (and MinGW/MSYS) is that it isn't needed to be installed on the target computer to run your developed programs, which is the case with CygWin.
There is also info about GDB on Windows, or checkout the CygWin site.
Information about how to setup (older, more obsolete, less recommended) systems are: MinGW, MSYS, msysDTK (autotools etc.).
The advantage of CygWin is that it is a more complete UNIX-like environment than MinGW/MSYS.
Finally, it is up to your taste to choose between the two.
You can also "mix" the development environment: If you have remote access to a UNIX/Linux box, but are working on a Windows box locally, use the (probably already installed) development tools on the Linux box through for example PuTTY, and use Emacs on Windows to edit the files remotely.
Check the MS Windows developer links for more details.

A word about editors:
If you already has a favorite editor and is decided to keep using it, that's fine, skip reading the rest of this section.
If you don't have a favorite editor, I recommend Emacs, just because I like it.
Emacs might have a rather high learning curve, but has been developed by developers for developers, so it is really good when it comes to programming (among other things).
To avoid this high learning curve (or at least make it a little bit smoother), I wrote An absolute beginner's tutorial about editing C files with Emacs.
Emacs is highly adapdted to software development, and has a mode before almost anything software related you may think of: cc-mode which highlights and indents your C source code automatically, makefile-mode which highlights and syntax checks your Makefile, you can debug your programs by running gdb inside Emacs, you can control your program versions with CVS or SVN inside Emacs, etc. etc.
Others might recommend vi, because they like it.
You can use even Notepad or Wordpad (though I would not recommend it, as both editors lack any features but basic editing), as long as you save the file in plain text format.
Check the links to lists of other text editors if you want to try something else.
If you wonder why Emacs but no other editors are mentioned in the tutorials, then you know why:
I like Emacs. :-)

The tutorials: top
  1. Emacs - An absolute beginner's tutorial about editing C files Related links
    (Skip this section if you already is familiar to Emacs, or prefer another editor.)
    Emacs is a very powerful editor, but the learning curve can be rather high.
    This tutorial's intention is not to scare you, but to get you familiar with the editor, at a slow pace and with lots of screenshots.

  2. C examples - for beginners Related links
    A C tutorial for beginners.
    If you aren't familiar with
    cc myprog.c -o myprog
    then this is for you.
    Get familiar with C source code and how to compile a program.
    As an absolute C beginner, you may want to see some very simple C source code.
    Copy the source code, paste it into your favorite editor, modify it, recompile it, run it, get your feet wet.

  3. Programming in C (external tutorial/exercises by Dave Marshall) Related links
    When you're ready to move on (or if you think the previous tutorials are just way too simple), you may continue with this very complete tutorial.
    (skip the CDE section and start at C/C++ Program Compilation).
    There are lots of exercises in Dave's tutorial.
    Take your time, and do as many of them as possible.
    Practice, practice, practice...
    (I learned a lot from these exercises.)

  4. Makefile examples Related links
    Learn how to make the compiling of your C programs easier.
    When you get tired of typing
    cc -a -whole -lot -of -flags -and -arguments myprog.c -o myprog
    from the command line everytime you want to recompile the C source code, it is time to have a look at make.
    To compile (and doing other stuff as well), you just type make myprog.
    Or simply make.

  5. C examples - pointers Related links
    You cannot really use C without using pointers.

  6. Version control with SVN and/or CVS Related links
    You can make backup copies of your source code using a simple cp, but there are some problems with this approach:
    How will you name the backup file?
    How will you name a second and a third backup file?
    What did you change/add/remove since the backup from last Tuesday?
    What about if you work in a group of programmers, sharing the same code, perhaps over a network?
    SVN (Subversion) or CVS (Concurrent Versions System) are two similar and commonly used tools to help you.

  7. Debugging, coding mistakes, memory leaks, performance problems (gdb, splint, Valgrind, gprof) Related links
    Learn how to debug your programs with gdb.
    It's ok to debug your program myprog.c once in a while by adding lines such as
    printf("DEBUG: The variable i=d%\n", i);
    but you should get used to something more sophisticated (but not necessarily more complicated).
    This tutorial shows some examples how to use gdb from inside Emacs, but it also applies to the debugger in general.
    For example, you can start debugging with gdb myprog.c., step to the line you want to debug, then inside gdb type exactly the same as you would in your source code:
    printf "DEBUG: The variable i=d%\n", i
    No more reasons for not using gdb!
    gdb may be run inside Emacs.
    It is very convenient with a split window view, one for the debugger commands, one for the source code.
    Other important tools to help you produce better code are splint (security check), Valgrind (memory leak check etc.), and gprof (performance analysis, a.k.a. profiling).

  8. C examples - glibc Related links
    Practice. Play around with The GNU C Library.

  9. C examples - sockets Related links
    Practice. Network programming.

  10. C examples - pthreads Related links
    Practice. Multithreaded programming.

  11. C examples - regular expressions Related links
    Practice. Find patterns with the POSIX regexp and the pcre libraries.

  12. C examples - databases Related links
    Practice. Connect to a database using MySQL's mysqlclient or PostgreSQL's libpq library.

  13. The C compiler, behind the scenes Related links
    Learn about what happens behind the scenes in the different C compilation steps:
    • The preprocessor
    • The compiler
    • The assembler
    • The linker

  14. Portable C libraries Related links
    Create portable code and libraries.

    1. Learn the basics about how to create a static and/or shared library:
      Create static:
      cc -g -ansi -pedantic -Wall -O2 -c myfunctions1.c myfunctions2.c
      ar rv libmystatic.a myfunctions1.o myfunctions2.o
      cp libmystatic.a /path/to/my/lib/ (optional)
      Use static:
      cc -o executable-name prog.c -L/path/to/my/lib -lmystatic
      Create shared (may vary a lot among platforms):
      cc -fpic -g -ansi -pedantic -Wall -O2 -c myfunctions1.c myfunctions2.c
      cc -shared -Wl,-soname,libmyshared.so.1 -o libmyshared.so.1.0.0 myfunctions1.o myfunctions2.o
      ln -sf libmyshared.so.1.0.0 libmyshared.so
      ln -sf libmyshared.so.1.0.0 libmyshared.so.1
      TODO: ldconfig() etc...
      Use shared:
      TODO
    2. Make the C code of the library portable between platforms:
      #ifdef WIN32
      ...
      #ifdef BSD
      ...
      #ifdef LINUX
      ...
      etc.
    3. Create a library for various target platforms:
      Use one of the gcc flags -shared, -G, link /DLL, -bundle etc., depending on your platform.
      Optionally, archive various libraries into a single file with ar.
    4. Don't dive to deep into the differences between platforms - a libtool tutorial is waiting for you (in the Autotools tutorial)...

  15. Autotools, Cmake, and Qmake Related links
    Autotools (that is, autoconf, automake, and libtools) helps you with the automizing of the creation of Makefiles and the sometimes tricky task to make your code portable.
    Cmake and Qmake are two alternatives to autotools.

  16. Distribute your code Related links
    Distribute your applications - use 'make dist' from Autotools (or Cmake, or Qmake, the choice is yours) to create tarballs, FreeBSD ports & packages, Debian ports? & packages?, Red Hat RPMs, MS Windows installers.

  17. C examples - graphics Related links
    Xlib: To understand how a graphical environment (windows, events, etc.) really works.
    Xt (X toolkit): The "glue" between Xlib and widgets such as Xaw and Motif.
    Xaw (Athena Widgets): GUI widgets (controls), included in the X distribution.
    Motif: GUI widgets as an alternative to Xaw.
    GTK+: The GIMP Toolkit, widgets originally created for GIMP, accessing Xlib directly, not using Xt. Also for MS Windows.
    Glade: GUI for GTK+.
    wxWidgets: Cross-platform (Windows, Mac OSX, Linux, FreeBSD, etc.) GUI Toolkit. Used in aMule, Audacity, Code::Blocks, VLC, among other software.
    Imlib2: Graphics library for fast execution (X Windows only)
    SDL: Multimedia library (only graphics treated here) (multiple platforms)
    MagickWand/MagickCore: APIs for ImageMagick.
    GD (Graphics Draw): Dynamic creation of PNG, JPEG and GIF images, mostly for web site development.
    Qt (C++): Cross-platform (Windows, Mac OSX, Linux, FreeBSD, etc.) GUI Toolkit. Used in KDE, Opera, and Google Earth, among other software.
    Qt+KDE (C++): How to create a KDE application using Qt.
    Qt+Qdevelop (C++): Using Qt with MinGW and the QDevelop IDE.
    OpenGL: Cross-platform API for 2D/3D graphics.

  18. C examples - audio, multimedia Related links
    How to generate/parse WAV, SDL audio/video, mplayer/ffmpeg.

  19. C examples - kernel modules and drivers Related links
    How to create kernel modules and drivers.

  20. IDEs
    As a reference list, if you prefer an Integrated Development Environment.
    • Code::Blocks - Cross-platform (Windows/Linux/FreeBSD) C/C++ IDE
    • KDevelop - IDE for KDE
    • Eclipse - Generic IDE with support for many languages, originally by IBM
    • MinGW - Not a graphical IDE, but a minimal UNIX development environment for MS Windows
    • CygWin - A non-graphical, not-so-minimal UNIX development environment for MS Windows

I chose this order for the tutorials, as I believe you should first should get acquaintanced with small, simple examples of C code and compiling/linking them from the command line, before writing learning how to write Makefiles to simplify the compiling/linking procedure.
Before learning how to use Autotools to create Makefiles and portable libraries automatically, I believe that one should understand how to write a Makefile manually and create a portable library by hand.
When you also have learned how to create your own distributions, I believe you have gone through all the necessary steps to understand how an application is created from scratch.
Then, not before, I would suggest you to try out a graphical IDE, which will do some or all things for you, sometimes without letting you know what happens behind the scenes (which is the disadvantage of using a graphical IDE). That is, if you still are interested in learning about graphical IDEs...


Links: top

Article:
  • Teach Yourself Programming in Ten Years by Peter Norvig.
Emacs related: Up
  • Emacs Beginner's HOWTO
  • GNU Emacs Tutorial (from 1997, but still with valuable info)
  • Emacs tips
  • Emacs on Windows
  • Emacs refcard (PDF)
  • Emacs tour
  • Emacs manual (extensive reading)
  • Using Emacs as a Development Environment
Links to other text editors: Up
  • Text editors on Windows
Links to other C examples: Up
  • Wikipedia - Category:Articles with example C code
  • How compiling works under Linux (and Unices in general)
  • Compiling with cc (from FreeBSD Developers' Handbook)
List of links to other C tutorials: Up
  • dmoz.org - Computers/Programming/Languages/C/Tutorials
Makefile tutorials: Up
  • make on Wikipedia
  • The GNU make manual: An Introduction to Makefiles
  • The GNU make manual
  • Makefile Debugging: Tracing Macro Values
Pointers: Up
  • C pointers on Wikipedia
  • C Programming/Pointers and arrays on Wikibooks
  • C Programming Tutorial: Pointers
  • Everything you need to know about pointers in C
  • The Function Pointer Tutorials
  • Callback function example on Wikipedia
  • C Programming/Memory management on Wikibooks
  • Memory Layout And The Stack
SVN & CVS: Up
  • Introduction to Version Control
  • SVN (Subversion) home page
  • SVN High-speed Tutorial
  • SVN manual
  • CVS to SVN repository conversion tool
  • TortoiseSVN - A Subversion client for Windows
Debugging, security check, memory leak check, performance analysis: Up
  • gdb:
    Debugging on Wikipedia
    GDB manual
    'Debugging' from FreeBSD Developer's Handbook
    Guide to Faster, Less Frustrating Debugging
    The Student's Guide to the Secret Art of Debugging (pdf)
    An Introduction To Using GDB Under Emacs
    EmacsWiki: DebuggingWithEmacs
    LinuxJournal article: Emacs Mode for GDB
  • splint:
    Splint on Wikipedia
    splint home page
  • Valgrind:
    Valgrind on Wikipedia
    Valgrind home page
    Article: Valgrind 2.2.0: Memory Debugging and Profiling
  • gprof:
    Performance analysis (profiling) on Wikipedia
    gprof manual
  • Secure programming:
    Secure Programming for Linux and Unix HOWTO (extensive reading)
glibc: Up
  • The GNU C Library manual
Sockets: Up
  • Introduction to network functions in C
  • Beej's Guide to Network Programming (Using Internet Sockets)
pthreads: Up
  • Introduction to Programming Threads
  • Multi-Threaded Programming With POSIX Threads
  • POSIX Threads Programming
Regular Expressions: Up
  • Regular Expression Matching - from the glibc manual
  • PCRE - Perl Compatible Regular Expressions
Databases: Up
  • MySQL C API
C behind the scenes (preprocessor/compiler/assembler/linker): Up
  • General:
    Components of the C compilation system
    Invoking GCC: Overall options
    FreeBSD Developers' Handbook: Compiling with cc
    Using the GNU Compiler Collection
    Using the GNU Compiler Collection (Part2)
  • cpp:
    C Preprocessor on Wikipedia
    C Programming/Preprocessor (Wikibook)
    C Preprocessor Trick For Implementing Similar Data Types
    GNU cpp manual
    cpp(1) man page
  • gcc:
    GCC on Wikipedia
    GNU C Compiler Internals (Wikibook)
    GCC manual
    gcc(1) man page
  • as:
    Assembly language on Wikipedia
    Linux Assembly
    Assembly Language Programming from FreeBSD Developers' Handbook
    How do I write "Hello, world" in FreeBSD assembler?
    GNU as manual
    ld(1) man page
  • ld:
    Linker on Wikipedia
    Components of the C compilation system: Link editing
    GCC ld manual
    ld(1) man page
Portable C libraries - links: Up
  • Static, Shared Dynamic and Loadable Linux Libraries
  • Compiling and Linking Dynamically-Loaded Functions
  • Using static and shared libraries across platforms
  • Manual for the The GNU C Library
  • LinuxFocus Tip: #defines for writing portable C-code
  • DLL Creation in MingW
Autotools, Cmake, and Qmake: Up
  • autotools on Wikipedia
  • autotut: Using GNU auto{conf,make,header}
  • Online version of the book 'GNU Autoconf, Automake and Libtool'
  • autoconf manual
  • automake manual
  • libtool manual
  • gettext manual
  • Cmake - Examples
  • Cross-Platform Software Development Using CMake
  • Cmake tutorial links
  • Qt: Qmake
How to distribute software: Up
TBD

Graphics programming: Up
  • Xlib:
    Wikipedia: Figure showing relation between Xlib, Xt, Xaw, Motif, GTK+, and Qt
    Wikipedia: X Window System protocols and architecture
    Wikipedia: Xlib
    Basic Graphics Programming With The Xlib Library
    Basic Graphics Programming With The Xlib Library - Part II
    How to Write a Screensaver Module
    Basic Graphics Programming With The XCB Library
  • Xt:
    Example C program that creates a Menu Bar using just Xt/Xaw
  • Xaw:
    Wikipedia: Xaw
    Xaw man page
  • Motif:
    Wikipedia: Motif
    A First MOTIF Program
    Motif Tutorials
  • GTK+:
    GTK+ on Wikipedia
    GTK+ Documentation
    GTK+ 2.x Hello, World
    GTK+ 3.0 Tutorial
  • Glade:
    Glade home page
    Starting off in Glade/GTK+
    GTK+ programming with Glade
    Creating your own GTK+ widget & adding it to Glade Palette
  • wxWidgets:
    Wikipedia: wxWidgets
    wxWidgets: First steps (includes how to build wxWidgets programs with MinGW)
  • Imlib2:
    Imlib2 Library Documentation
  • SDL:
    SDL
    SDL tutorials
    GFX with SDL
  • ImageMagick:
    ImageMagick home page
    MagickWand API
    MagickCore API
  • GD:
    libGD home page
    libGD docs
  • Qt+KDE (C++):
    KDE TechBase: Introduction To KDE 4 Programming
    KDE TechBase: First program
  • OpenGL:
    OpenGL on Wikipedia
    OpenGL home page
    OpenGL: Getting Started
Multimedia: Up
  • SDL Introduction
Kernel & drivers: Up
  • Writing a kernel module for FreeBSD
  • Writing device drivers in Linux: A brief tutorial
  • PCI Vendor and Device Lists
Mac OSX developer links: Up
  • http://www.apple.com/opensource/
MS Windows developer links: Up
  • General:
    MinGW home page
    Cygwin home page
    MinGW setup
    MSYS setup
    msysDTK (autotools etc.) setup
    GDB on Windows setup
    Emacs on Windows
    Win32 Library by Function Name
    theForger's Win32 API Tutorial
    Win32 Tips, Tricks and Tutorials
    AL's Programming Resource- Win32 Tutorials
    FoosYerDoos WinAPI C++ programming (C++, outdated, but very complete tutorial)
  • Graphics:
    Qt for Windows (C++)
    GTK+, Glade, LibGlade etc. for Windows
    MinGW and SDL: Windows Development
Miscellaneous links:
  • Escape sequences and escape characters
Last modified: Thu Sep 8 11:18:32 EDT 2016