Você está na página 1de 21

Introduction to C

Part 1: Overview
C Language Tutorial
System Programming 251­0053
Fall Semester 08 

Mathias Payer, RZ H11
<mathias.payer@inf.ethz.ch>

C Introduction / Overview 1/21
Outline

● Tutorial teaches basics of C


● 4 Lectures
– 09/25: Overview
– 10/02: Basic programming
– 10/10: Advanced programming
– 10/17: Environment

● Hands-on work handled in exercise classes


– Debugger examples
– Inline assembler instructions

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

● First standard written by Ritchie and Brian Kernighan


– “The C Programming Language” (1978)
– Known as K&R-Syntax
● C made it possible to write fast,
“portable” code
● First standardization imprecise
● Second standard ANSI-C (in '89)
● Same standard in '90 by ISO
● Latest revision is ANSI-C99

C Introduction / Overview 6/21
History of languages

Source: CSC 221 Computer Programming I/History / http://www.comphist.org

C Introduction / Overview 7/21
K&R vs. ANSI Syntax

K&R-C Style ANSI-C Style


main(argc, argv) int main(int argc, char 
/* int argc; */ **argv)
char **argv; {
{     ...
    ...     while (x == y) {
    while (x == y) {         something();
        something();     }
    }     finalthing();
    finalthing();     ...
    ... return 0;
return (0); }
}

Use ANSI-C style, although K&R is still valid.

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

.c.c C Compiler .s Assembler .o


(gcc) (as)

.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 Compiler (gcc) reads source and header files


– can output assembly (gcc -S)
– can output object files (gcc -c)
– create executables by invoking the linker (default)
● Assember (as) generates object files with machine
code
● Linker (ld) resolved dependencies and collects
different object files into an executable
● Archiver (ar) generates libraries from object files
● Debugger (gdb) can debug a running program and
allows inspection of memory addresses and registers

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

● Program entry point is the “main” function (which


can have different signatures:
– int main()
– int main(int argc, char *argv[])
● #include <stdio.h>
– Includes function signatures for common I/O operations
● printf("Hello World/n");
– Calls I/O function and prints the given string to the console
● /* this is a multi­line comment */
● // this is a single line comment
C is not a high­level language.
­­ Brian Kernighan

C Introduction / Overview 13/21
Hello World
● Let's check what gcc really does
$ gcc ­v ­o hello hello.c
Using built­in specs.
Target: i486­linux­gnu
Configured with: ../src/configure ­v ­­enable­languages=c,c++,fortran,objc,obj­c++,treelang ­­prefix=/
usr ­­enable­shared ­­with­system­zlib ­­libexecdir=/usr/lib ­­without­included­gettext ­­enable­
threads=posix ­­enable­nls ­­program­suffix=­4.1 ­­enable­__cxa_atexit ­­enable­clocale=gnu ­­enable­
libstdcxx­debug ­­enable­mpfr ­­enable­checking=release i486­linux­gnu
Thread model: posix
gcc version 4.1.2 (Ubuntu 4.1.2­0ubuntu4)
 /usr/lib/gcc/i486­linux­gnu/4.1.2/cc1 ­quiet ­v hello.c ­quiet ­dumpbase hello.c ­mtune=generic 
­auxbase hello ­version ­fstack­protector ­fstack­protector ­o /tmp/cccKSivl.s
#include "..." search starts here:
#include <...> search starts here:
 /usr/local/include
 /usr/lib/gcc/i486­linux­gnu/4.1.2/include
 /usr/include
End of search list.
GNU C version 4.1.2 (Ubuntu 4.1.2­0ubuntu4) (i486­linux­gnu)
        compiled by GNU C version 4.1.2 (Ubuntu 4.1.2­0ubuntu4).
GGC heuristics: ­­param ggc­min­expand=100 ­­param ggc­min­heapsize=131072
Compiler executable checksum: c0d954aeefbb96d795ff3f6b3b72ef51
 as ­V ­Qy ­o /tmp/cce0KhUv.o /tmp/cccKSivl.s
GNU assembler version 2.17.50 (i486­linux­gnu) using BFD version 2.17.50 20070103 Ubuntu
 /usr/lib/gcc/i486­linux­gnu/4.1.2/collect2 ­­eh­frame­hdr ­m elf_i386 ­­hash­style=both ­dynamic­
linker /lib/ld­linux.so.2 ­o hello /usr/lib/gcc/i486­linux­gnu/4.1.2/../../../../lib/crt1.o 
/usr/lib/gcc/i486­linux­gnu/4.1.2/../../../../lib/crti.o /usr/lib/gcc/i486­linux­gnu/4.1.2/crtbegin.o 
­L/usr/lib/gcc/i486­linux­gnu/4.1.2 ­L/usr/lib/gcc/i486­linux­gnu/4.1.2 ­L/usr/lib/gcc/i486­linux­gnu/
4.1.2/../../../../lib ­L/lib/../lib ­L/usr/lib/../lib /tmp/cce0KhUv.o ­lgcc ­­as­needed ­lgcc_s ­­no­
as­needed ­lc ­lgcc ­­as­needed ­lgcc_s ­­no­as­needed /usr/lib/gcc/i486­linux­gnu/4.1.2/crtend.o 
/usr/lib/gcc/i486­linux­gnu/4.1.2/../../../../lib/crtn.o

C Introduction / Overview 14/21
Variables – Types

Typname Description Size Format


short short integer 2 bytes (IA32)
int integer 4B (IA32), 2B (16bit cpus)
long int / long long integer 4B (IA32), 8B (sparcv9)
long long int very long integer 8B (IA32), 4B (16bit cpus)
float single precision 32 bit, IEEE 754
floating-point number
double double precision 64 bit, IEEE 754
floating-point number
char single character 1 byte

Warning: The size of an Integer type is platform/compiler


dependent. Runtime check with sizeof operator is possible:
e.g.: sizeof(long long int) = 8 (bytes)

C Introduction / Overview 15/21
Signed & Unsigned Integers

Typname Size (IA32) Range


signed char 1B -128 .. +127
unsigned char 1B 0 .. 255
signed short 2B -32'768 .. +32'767
unsigned short 2B 0 .. 65'535
signed int 4B -2'147'483'648 .. +2'147'483'647
unsigned int 4B 0 .. 4'294'967'295
s long long int 8B -9'223'372'036'854'775'808 .. +xxx'807
u long long int 8B 0 .. 18'7446'744'073'709'551'615

● Integer types in C can be interpreted as signed (2's


complement) or unsigned (straight representation)
● Unless unsigned is specified the compiler implicitly
assumes a signed type.

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 (0­10): 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

● Many good tutorials available online (ask Google)


● O'Reilly “C Pocket Reference” (0-596-00436-2)
– http://proquest.safaribooksonline.com/0596004362 (my tip)
● Computer Systems: A Programmer's Perspective
(0-13-034074-X)
● The C Programming Language (0-13-110362-8)

C Introduction / Overview 20/21
Questions

?
C Introduction / Overview 21/21

Você também pode gostar