Introduction To C: Part 1: Overview C Language Tutorial System Programming 251 0053 Fall Semester 08
Introduction To C: Part 1: Overview C Language Tutorial System Programming 251 0053 Fall Semester 08
Part 1: Overview
C Language Tutorial
System Programming 2510053
Fall Semester 08
Mathias Payer, RZ H11
<mathias.payer@inf.ethz.ch>
C Introduction / Overview 1/21
Outline
C Introduction / Overview 2/21
Overview (this lecture)
● C features
● History
– Environment
– K&R vs. ANSI-C Syntax
● Indentation
● Compiler toolchain A language that combines all
the elegance and power of
● Hello World Example assembly language with all
the readability and
– Syntax maintainability of assembly
● Variable Types language.
New Hacker's Dictionary
● Loop Example
– Debugging
C Introduction / Overview 3/21
C Features
● Imperative
– Statements change program state
● Procedure based
– No objects
● Low level, but machine independent
– Designed to ease portability
● Direct (untyped) memory access
● Parameters passed by value
– Pass-by-reference by passing pointers
C Introduction / Overview 4/21
History
● Developed by Dennis Ritchie at AT&T Labs
– Between 1969 and 1972 (descendant from Thompson's “B”)
– Closely tied to the Unix OS
Dennies Ritchie and Ken Thompson are porting Unix to the PDP-11
http://www.cs.bell-labs.com/who/dmr/
C Introduction / Overview 5/21
History
C Introduction / Overview 6/21
History of languages
C Introduction / Overview 7/21
K&R vs. ANSI Syntax
C Introduction / Overview 8/21
Indentation
Indentation is important
for code readability
void function ()
{
int someval, j;
someval = 23;
for (j=0; j<10; j++) {
/* this is a comment */
printf(“Value: %d\n”, someval++);
}
}
I have stopped reading Stephen King
novels. Now I just read C code instead.
Richard O'Keefe (“The Craft of Prolog”)
C Introduction / Overview 9/21
Compiler Toolchain
.s.s
.h
.h
exe Linker Archiver
(ld) (ar)
.c C source file
.h C header file
.s Assembler source .o.o .a.a
.o Object .a
.a Library
C Introduction / Overview 10/21
Compiler Toolchain
C Introduction / Overview 11/21
Hello World
#include <stdio.h> file “hello.c”
int main()
{
/* print hello world to the terminal */
printf(“Hello World!\n”);
return 0; // clean exit
}
$ ls Name of the
hello.c executable
$ gcc o hello hello.c
$ ls
hello hello.c
$ ./hello
Hello World
$
C Introduction / Overview 12/21
Hello World
C Introduction / Overview 13/21
Hello World
● Let's check what gcc really does
$ gcc v o hello hello.c
Using builtin specs.
Target: i486linuxgnu
Configured with: ../src/configure v enablelanguages=c,c++,fortran,objc,objc++,treelang prefix=/
usr enableshared withsystemzlib libexecdir=/usr/lib withoutincludedgettext enable
threads=posix enablenls programsuffix=4.1 enable__cxa_atexit enableclocale=gnu enable
libstdcxxdebug enablempfr enablechecking=release i486linuxgnu
Thread model: posix
gcc version 4.1.2 (Ubuntu 4.1.20ubuntu4)
/usr/lib/gcc/i486linuxgnu/4.1.2/cc1 quiet v hello.c quiet dumpbase hello.c mtune=generic
auxbase hello version fstackprotector fstackprotector o /tmp/cccKSivl.s
#include "..." search starts here:
#include <...> search starts here:
/usr/local/include
/usr/lib/gcc/i486linuxgnu/4.1.2/include
/usr/include
End of search list.
GNU C version 4.1.2 (Ubuntu 4.1.20ubuntu4) (i486linuxgnu)
compiled by GNU C version 4.1.2 (Ubuntu 4.1.20ubuntu4).
GGC heuristics: param ggcminexpand=100 param ggcminheapsize=131072
Compiler executable checksum: c0d954aeefbb96d795ff3f6b3b72ef51
as V Qy o /tmp/cce0KhUv.o /tmp/cccKSivl.s
GNU assembler version 2.17.50 (i486linuxgnu) using BFD version 2.17.50 20070103 Ubuntu
/usr/lib/gcc/i486linuxgnu/4.1.2/collect2 ehframehdr m elf_i386 hashstyle=both dynamic
linker /lib/ldlinux.so.2 o hello /usr/lib/gcc/i486linuxgnu/4.1.2/../../../../lib/crt1.o
/usr/lib/gcc/i486linuxgnu/4.1.2/../../../../lib/crti.o /usr/lib/gcc/i486linuxgnu/4.1.2/crtbegin.o
L/usr/lib/gcc/i486linuxgnu/4.1.2 L/usr/lib/gcc/i486linuxgnu/4.1.2 L/usr/lib/gcc/i486linuxgnu/
4.1.2/../../../../lib L/lib/../lib L/usr/lib/../lib /tmp/cce0KhUv.o lgcc asneeded lgcc_s no
asneeded lc lgcc asneeded lgcc_s noasneeded /usr/lib/gcc/i486linuxgnu/4.1.2/crtend.o
/usr/lib/gcc/i486linuxgnu/4.1.2/../../../../lib/crtn.o
C Introduction / Overview 14/21
Variables – Types
C Introduction / Overview 15/21
Signed & Unsigned Integers
C Introduction / Overview 16/21
Another Example
file “loop.c”
#include <stdio.h>
#define MAX 10
int main()
{
int sum, i;
sum = 0;
for (i=0; i<=MAX; i++) {
sum += i;
}
printf("Sum of i (0%d): %d\n", MAX, sum);
}
$ gcc o loop loop.c
$ ./loop
Sum of i (010): 55
$
C Introduction / Overview 17/21
Add debug information
$ man gcc
...
g Produce debugging information in the operating system’s native for‐
mat (stabs, COFF, XCOFF, or DWARF 2). GDB can work with this
debugging information.
On most systems that use stabs format, g enables use of extra
debugging information that only GDB can use; this extra information
makes debugging work better in GDB but will probably make other
debuggers crash or refuse to read the program. If you want to con‐
trol for certain whether to generate the extra information, use
gstabs+, gstabs, gxcoff+, gxcoff, or gvms (see below).
...
$ gcc g o loop ./loop.c
Writing in C or C++ is like
running a chain saw with all the
safety guards removed.
Bob Gray
C Introduction / Overview 18/21
Debug Session
$ gdb ./loop
(gdb) list
1 #include <stdio.h>
2 #define MAX 10
3 int main()
4 {
5 int sum, i;
6 sum = 0;
7 for (i=0; i<=MAX; i++) {
8 sum += i;
9 }
10 printf("Sum of i (0%d): %d\n", MAX, sum);
11 return 0;
12 }
(gdb) break 8
Breakpoint 1 at 0x8048395: file loop.c, line 8.
(gdb) run
Starting program: /tmp/loop
Breakpoint 1, main () at loop.c:8
8 sum += i;
(gdb) cont
...
(gdb) print sum
$1 = 6
(gdb) quit
DEMO
C Introduction / Overview 19/21
Books / Tutorials
C Introduction / Overview 20/21
Questions
?
C Introduction / Overview 21/21