2
63

First steps of C

Install the programming environment for C/C++ for Linux or Windows then edit, compile, run and debug your first program.

Installation

Linux

The GNU C compiler gcc is normally already installed. Check by entering the command gcc -v. Also check that the command make and the debugger gdb are on your system. If necessary, install the programming environment for C with the following command:

$ sudo apt-get install gcc gdb make

To install the environment for programming in C++, type the following command:

$ sudo apt-get install g++

Install the manual pages for the developer:

$ sudo apt-get install manpages manpages-dev

Check:

$ man 3 printf

NOTE: The parameter 3 directly accesses section 3 of the manual which describes calls to the library. Try man man for more explanations.

Windows

Install MinGW (Minimalist GNU for Windows) in C:\MinGW. Follow the installation instructions of MinGW and MSYS with mingw-get. Download the w32API package and extract the folders called include and lib in C:\MinGW.

After MinGW has been installed, create a shortcut on the desk. Configure the target to run C:\MinGW\msys\1.0\msys.bat. Rename the shortcut Msys . Change the icon by selecting cmd.exe in the folder %windir%\system.

If you prefer the Windows command processor, open the Configuration panel, double-click on System then click on the tag Advanced and finally on the button Environment variables. Modify the variable PATH which gives the list of the folders containing commands. Add the folders C:\MinGW\bin and C:\MinGW\msys\1.0\bin carefully separating folder names by a ; (SEMICOLON). Start a command processor and check that the command gcc of the C compiler is found:

C:\Documents and Settings\frasq\Mes documents\C>gcc -v

You can also install the latest free version of the C/C++ development environment by Microsoft available here.

After the Toolkit has been installed, create a file called env.bat in your C folder with the following content:

  1. @ECHO off
  2.  
  3. ECHO Microsoft Visual C++ Toolkit (PATH INCLUDE LIB)
  4.  
  5. REM The paths to Microsoft Visual Studio and Microsoft SDKs\Windows depend on the version of the Toolkit.
  6.  
  7. SET PATH=%ProgramFiles%\Microsoft Visual Studio 10.0\VC\bin;%ProgramFiles%\Microsoft Visual Studio 10.0\Common7\IDE;%SystemRoot%\system32;%SystemRoot%
  8. SET INCLUDE=%ProgramFiles%\Microsoft Visual Studio 10.0\VC\include
  9. SET LIB=%ProgramFiles%\Microsoft Visual Studio 10.0\VC\lib;C:\Program Files\Microsoft SDKs\Windows\v7.0A\Lib

This file groups a series of commands which initialize the environment variables PATH, INCLUDE and LIB. PATH list the directories containing commands like cl, link or make from the Toolkit, and the commands from Windows. INCLUDE indicates to the compiler where the files included in a source code are. LIB gives the list of the directories containing libraries of compiled files necessary for building executables. Adapt env.bat to the version of the Toolkit installed on your system.

  1. ECHO Git (PATH HOME)
  2. SET PATH=%PATH%;%ProgramFiles%\Git\bin
  3. SET HOME=%HOMEPATH%

The rest of the file gives access to Git and defines the HOME variable useful to many programs ported from Unix.

Create a shortcut in the same folder. Configure the target so that it executes the order %comspec% /k env.bat. This line launches the command processor and tells it to start by running the content of the file env.bat. Rename the shortcut to Visual C++ Toolkit Command Prompt.

Double-click on the shortcut to launch the command processor. Check that the command cl from the C compiler is in the PATH:

C:\Documents and Settings\frasq\My documents\C>echo %PATH%
C:\Program Files\Microsoft Visual Studio 9.0\VC\bin;C:\Program Files\Microsoft Visual Stu
dio 9.0\Common7\IDE;C:\WINDOWS\system32;C:\WINDOWS
C:\Documents and Settings\frasq\My documents\C>cl
Microsoft (R) 32-bit C/C++ Optimizing Compiler Version 13.10.3077 for 80x86
Copyright (C) Microsoft Corporation 1984-2002. All rights reserved.

usage: cl [ option... ] filename... [ /link linkoption... ]
First program

Create a folder called C where you will save all your programs in C. Create another folder called Hello in C. Launch a text editor - gedit for Linux, textpad or notepad++ for Windows or better, use Eclipse - and create the file hello.c in the folder C/Hello with the following content:

  1. #include <stdio.h>
  2.  
  3. main() {
  4.     printf("Hello from C!\n");
  5.     return 0;
  6. }

NOTE: Don't type the line numbers.

Linux

Compile the program:

$ cd
$ cd C/Hello
$ ls
hello.c
$ gcc -o hello hello.c
$ ls
hello hello.c

The -o hello option instructs gcc to name the executable hello. Without this option, the executable is named a.out.

$ gcc hello.c
$ ls
a.out hello hello.c
$ rm a.out

Run the executable:

$ ./hello
Hello from C!

Windows

With MinGW, you work exactly like under Linux and you compile the program with gcc. If you have decided for the Toolkit by Microsoft, compile the program with cl :

C:\Documents and Settings\frasq\My documents\C\Hello>cl hello.c
Microsoft (R) 32-bit C/C++ Optimizing Compiler Version 13.10.3077 for 80x86
Copyright (C) Microsoft Corporation 1984-2002. All rights reserved.

hello.c
Microsoft (R) Incremental Linker Version 7.10.3077
Copyright (C) Microsoft Corporation.  All rights reserved.

/out:hello.exe
hello.obj
C:\Documents and Settings\frasq\Mes documents\C\Hello>hello
Hello from C!

Try the same program in C++:

  1. #include <iostream>
  2.  
  3. int main() {
  4.     std::cout << "Hello" << " " << "from C++!" << std::endl;
  5. }

Compile and run the program:

$ cd
$ cd C/Hello
$ ls
hello.cpp
$ g++ -o hello hello.cpp
$ ./hello
Hello from C++!
Building a program

Create a file called makefile in C/Hello with the following content:

  1. ALL=hello
  2.  
  3. SRCS=hello.c
  4. OBJS=$(SRCS:.c=.o)
  5.  
  6. CC=gcc
  7. CFLAGS=-DDEBUG -g
  8. LDFLAGS=
  9.  
  10. all:    $(ALL)
  11.  
  12. $(ALL): $(OBJS)
  13.     $(CC) $^ -o $@ $(LDFLAGS)
  14.  
  15. .PHONY: clean wipe
  16.  
  17. clean:
  18.     rm -f $(OBJS)
  19.  
  20. wipe:   clean
  21.     rm -f $(ALL)
$ ls
hello hello.c makefile
$ make clean
rm -f hello.o
$ make wipe
rm -f hello.o
rm -f hello
$ ls
hello.c
$ make
gcc -DDEBUG -g -c -o hello.o hello.c
gcc hello.o -o hello$ rm hello
$ make
gcc hello.o -o hello
$ touch hello.c
$ make
gcc -DDEBUG -g -c -o hello.o hello.c
gcc hello.o -o hello

The output of the makefile shows how gcc is run a first time with the -c option to just compile the source file hello.c and build the object code in hello.o then a second time to link the object file hello.o with the C library and produce the executable hello. If hello is deleted, make doesn't recompile hello.c to rebuild the executable. If hello.c is changed, it's properly recompiled. The -g option asks the compiler to add the code necessary to run the program in debug mode with gdb.

Under Windows, if you are using the Toolkit by Microsoft, the command make is called nmake and the input file is slightly different.

  1. APP=hello
  2.  
  3. CC=cl
  4. LINK=link
  5.  
  6. all:    $(APP).exe
  7.  
  8. $(APP).exe: $(APP).obj
  9.     $(LINK) $(APP).obj /out:$(APP).exe
  10.  
  11. clean:
  12.     del *.obj
  13.    
  14. wipe:   clean
  15.     del $(APP).exe
C:\Documents and Settings\frasq\My documents\C\Hello>nmake /f makefile.nmk

The variations for a program in C++:

  1. APP=hello
  2.  
  3. CPP=g++
  4. CPPFLAGS=-DDEBUG -g
  5.  
  6. $(APP): $(APP).o
  7.     $(CPP) $(APP).o -o $(APP)
  8.  
  9. .PHONY: clean wipe
  10.  
  11. clean:
  12.     rm -f *.o
  13.    
  14. wipe:   clean
  15.     rm -f $(APP)
  1. APP=hello
  2.  
  3. CPP=cl
  4. CPPFLAGS=/EHsc
  5. LINK=link
  6.  
  7. $(APP).exe: $(APP).obj
  8.     $(LINK) $(APP).obj /out:$(APP).exe
  9.  
  10. clean:
  11.     del *.obj
  12.    
  13. wipe:   clean
  14.     del $(APP).exe
Debugging a program

To analize the execution of a program step by step, compile it in debugging mode by passing the option -g to gcc:

$ gcc -g -o hello hello.c

Start the debugger gdb with the executable in argument:

$ gdb hello
GNU gdb (GDB) 7.1-ubuntu
Copyright (C) 2010 Free Software Foundation, Inc.
...
Reading symbols from /home/frasq/C/Hello/hello...done.
(gdb) 

NOTE: If gcc doesn't find symbols in hello, make sure that you've passed the option -g to gcc without the option -s.

Place a breakpoint at the beginning of the program:

(gdb) break main
Breakpoint 1 at 0x80483ed: file hello.c, line 4.

Start the program:

(gdb) run
Starting program: /home/frasq/C/Hello/hello 

Breakpoint 1, main () at hello.c:4
4		printf("Hello from C!\n");

List the source code of the program around the breakpoint:

(gdb) list 4
1	#include <stdio.h>
2	
3	main() {
4		printf("Hello from C!\n");
5		return 0;
6	}

Run the next line of code:

(gdb) step
Hello from C!
5		return 0;

Continue running the program until the end:

(gdb) cont
Continuing.

Program exited normally.

Quit gdb:

(gdb) quit
$ 

Comments

To add a comment, click here.