Introduction to the C-Language and Programming Environment

Similar documents
Comp 524 Spring 2009 Exercise 1 Solutions Due in class (on paper) at 3:30 PM, January 29, 2009.

Revealing Internals of GNU Compiler Collection (GCC) Zhiqiang Lin

EL2310 Scientific Programming

Introduction to Supercomputing

EL2310 Scientific Programming

The Compiler as Attack Vector

Laboratory 1 Semester 1 11/12

CMPT 300. Operating Systems. Brief Intro to UNIX and C

C Compilation Model. Comp-206 : Introduction to Software Systems Lecture 9. Alexandre Denault Computer Science McGill University Fall 2006

Hand-on Labs for Chapter 1 and Appendix A CSCE 212 Introduction to Computer Architecture, Spring

Exercise 1: Basic Tools

ITC213: STRUCTURED PROGRAMMING. Bhaskar Shrestha National College of Computer Studies Tribhuvan University

Arm cross development tools


How to learn C? CSCI [4 6]730: A C Refresher or Introduction. Diving In: A Simple C Program 1-hello-word.c

Introduction to Cygwin Operating Environment

Operating System Labs. Yuanbin Wu

CS240: Programming in C

CSC111 Computer Science II

Problem Set 1: Unix Commands 1

Beyond this course. Machine code. Readings: CP:AMA 2.1, 15.4

Saint Louis University. Intro to Linux and C. CSCI 2400/ ECE 3217: Computer Architecture. Instructors: David Ferry

COMP1917: Computing 1 1. Introduction

Brief Linux Presentation. July 10th, 2006 Elan Borenstein

Arrays and Pointers. CSC209: Software Tools and Systems Programming (Winter 2019) Furkan Alaca & Paul Vrbik. University of Toronto Mississauga

CS356: Discussion #1 Development Environment. Marco Paolieri

Continue: How do I learn C? C Primer Continued (Makefiles, debugging, and more ) Last Time: A Simple(st) C Program 1-hello-world.c!

Red Hat Developer Tools

Follow us on Twitter for important news and Compiling Programs

1. Introduction. Course Web Site. COMP1917: Computing 1. Textbook. Occupational Health and Safety (OHS)

Programming in C. What is C?... What is C?

Programming in C UVic SEng 265

Introduction to C Programming

Systems Programming. Fatih Kesgin &Yusuf Yaslan Istanbul Technical University Computer Engineering Department 18/10/2005

Programming in C. What is C?... What is C?

PRINCIPLES OF OPERATING SYSTEMS

COMP1917: Computing 1 1. Introduction

PROGRAMMAZIONE I A.A. 2017/2018

CS 261 Fall C Introduction. Variables, Memory Model, Pointers, and Debugging. Mike Lam, Professor

Overview of Unix / Linux operating systems

C Introduction. Comparison w/ Java, Memory Model, and Pointers

CSE 303 Lecture 8. Intro to C programming

CS201 - Lecture 1 The C Programming Language

Simple C Program. Assembly Ouput. Using GCC to produce Assembly. Assembly produced by GCC is easy to recognize:

Programs. Function main. C Refresher. CSCI 4061 Introduction to Operating Systems

introduction week 1 Ritsumeikan University College of Information Science and Engineering Ian Piumarta 1 / 20 imperative programming about the course

EECS2031 Software Tools

CSC231 C Tutorial Fall 2018 Introduction to C

Operating System Labs. Yuanbin Wu

Chapter 2. Basics of Program Writing

Unix and C Program Development SEEM

Carnegie Mellon. Linux Boot Camp. Jack, Matthew, Nishad, Stanley 6 Sep 2016

Outline. Compiling process Linking libraries Common compiling op2ons Automa2ng the process

Computers and Computation. The Modern Computer. The Operating System. The Operating System

Intermediate Programming, Spring Misha Kazhdan

Getting started with UNIX/Linux for G51PRG and G51CSA

Spring 2018 NENG 202 Introduction to Computer Programming

1. The Mac Environment in Sierra Hall 1242

Draft. Chapter 1 Program Structure. 1.1 Introduction. 1.2 The 0s and the 1s. 1.3 Bits and Bytes. 1.4 Representation of Numbers in Memory

Program Design: Using the Debugger

Introduction in Unix. Linus Torvalds Ken Thompson & Dennis Ritchie

Oregon State University School of Electrical Engineering and Computer Science. CS 261 Recitation 1. Spring 2011

ECE 3210 Laboratory 1: Develop an Assembly Program

CMSC 104 Lecture 2 by S Lupoli adapted by C Grasso

CSE 351. Introduction & Course Tools

Course Information and Introduction

Introduction to Supercomputing

Introduction to Linux. Woo-Yeong Jeong Computer Systems Laboratory Sungkyunkwan University

mith College Computer Science CSC352 Week #7 Spring 2017 Introduction to C Dominique Thiébaut

Introduction to Linux

Development Environment & Linux Guide

UNIX Makefile. C Project Library Distribution and Installation.

Red Hat Developer Tools

Linux/Cygwin Practice Computer Architecture

LECTURE/ STUDY NOTES ON C

Topics. CS429: Computer Organization and Architecture. File Inclusion. A Simple C Program. Intro to C

Assignment 1. CSI Programming Practice, Fall 2011

1. The Mac Environment in SIE 1222

by Pearson Education, Inc. All Rights Reserved.

EE 209: Programming Structures for Electrical Engineering. (Many Slides Borrowed from Princeton COS 217)

CSCI 192 Engineering Programming 2. Subject Overview

EE 209: Programming Structures for Electrical Engineering

Debugging (Part 1) The material for this lecture is drawn, in part, from The Practice of Programming (Kernighan & Pike) Chapter 5

Software Project. Lecturers: Ran Caneti, Gideon Dror Teaching assistants: Nathan Manor, Ben Riva

Reviewing gcc, make, gdb, and Linux Editors 1

Included original file, but not rpt-hw.hpp

How Compiling and Compilers Work

EECS Software Tools. Lab 2 Tutorial: Introduction to UNIX/Linux. Tilemachos Pechlivanoglou

Fall, $ cat welcome.c #include <stdio.h>

CS429: Computer Organization and Architecture

CSC 112 Lab 1: Introduction to Unix and C++ Fall 2009

Computer Science 322 Operating Systems Mount Holyoke College Spring Topic Notes: C and Unix Overview

CS 211 Programming I for Engineers

CS11001/CS11002 Programming and Data Structures Autumn/Spring Semesters. Introduction

Department of Computer Science and Engineering Yonghong Yan

CS 253: Intro to Systems Programming 1/21

Room 3P16 Telephone: extension ~irjohnson/uqc146s1.html

Introduction to Operating Systems Part I

Introduction to Linux Basics

CS Prof J.P.Morrison

Transcription:

Introduction to the C-Language and Programming Environment Part 1: Basics to get started C Language Tutorial System Programming 251-0053 Winter Semester 2005/06 René Müller, IFW B45.2 SysProg WS05/06 1

Content Facts about C Writing and Compiling C Programs Tools Compiler, Linker, Assembler, Debugger Working Environment SysProg WS05/06 2

History of the C-Language C was developed by Dennis M. Ritchie at AT&T Bell Labs between 1969-1973 C is a descendant of Ken Thompson s language B Ritchie and Brian Kernighan published the standard book The C Programming Language in 1978. The C-dialect book described in the original publication is now known as K&R-Style. Due to the ease of portability the language began to spread. However there was no precise language standard (K&R insufficiently precise) A slightly modified version of the language from K&R was standardised by ANSI 1989 (ANSI-C) The latest revision of the Standard is C99 (ISO) Source: Ritchie D.M.: The Development of the C Language, ACM SIGPLAN, 1993 SysProg WS05/06 3

The early days the guys Dennis Ritchie (left) und Ken Thompson (right) in front In front of a PDP-11 system with two text-terminals (1972) source: Dennis Ritchie s home page, http://www.cs.bell-labs.com/who/dmr/ SysProg WS05/06 4

Their system PDP-11 Digital Equipment Corporation (DEC) shipped first model of PDP-11 series in 1970. CPU module of a PDP-11/20: 4kB memory, no floating-point unit, $12 000 (1972) Additional core-memory module: 8 kb memory, $10 000 source: http://www.psych.usyd.edu.au/pdp-11 SysProg WS05/06 5

C the way should should not use it This is a valid C program which happens to solve the n-queens Problem. v,i,j,k,l,s,a[99]; main() { for(scanf("%d",&s);*a-s;v=a[j*=v]-a[i],k=i<s,j+=(v=j<s&&(!k&&!!\ printf(2+"\n\n%c"-(!l<<!j)," #Q"[l^v?(l^j)&1:2])&&++l a[i]<s&&v\ &&v-i+j&&v+i-j))&&!(l%=s),v (i==j?a[i+=k]=0:++a[i])>=s*k&&++a[--i]); } Baruch Nissenbaum, Tel-Aviv University, Israel Winner of the 7th Int. Obfuscated C Code Contest 1990 (www.ioccc.org) SysProg WS05/06 6

Libraries 010101 010101 010101001001 001001 001001.a From C code to an executable binary 010101 010101 010101001001 001001 001001 printf(.) additional Object files.o.h C header files C-Compiler (GNU C, cc) main(.) gcc.c 010101 010101 010101001001 001001 001001.o C source files Compiler can generate assembly code (option S) Object files Assembly source files Assembler movl... as.s ld Archiver ar Linker Debugger gdb Inspecting the running program 010101 001001 Executable file a.out 010101 001001 Library.a SysProg WS05/06 7

Compiler Toolchain C-Compiler (gcc von GNU, or cc von DEC, SUN, etc.) reads C source file and Creates assembly file (.s) (gcc S) But can also create object file (.o) by calling Assembler (gcc c) create executable file by calling the Linker (default) afterwards Assembler (as) generates object files (.o) that contain machine code from assembly source files (.s) Linker (ld) collects object files and libraries and creates executable file Archiver (ar) creates libraries (.a) from object files Debugger (gdb from GNU) allows inspection of memory and register content of a running program SysProg WS05/06 8

Hello World -Example in C Save the code in a file hello.c : main() { printf( Hello World!\n ); } From the terminal the executable can be built and started: $ ls ls hello.c $ gcc gcc -o -o hello hello hello.c $ ls ls hello.c hello hello $./hello Hello Hello World! $ SysProg WS05/06 9

Hello World -Example in C (2) The execution of the program starts by calling the main-function. \n produces a new line on the terminal main() { printf( Hello World!\n ); } Writes string to standard output of the terminal. printf is a C-Function, which is part of the standard C-library. The main function can have different signatures: main() int main(int argc, char **argv) int main(int argc, char **argv, char **env) SysProg WS05/06 10

Compiling and Linking gcc -o hello hello.c directly creates an executable file hello (specified by option o) from the source file. Note: without specifying option -o, gcc chooses the default name a.out for the output file. The creation of the machine code and linking is done automatically. Calling gcc with option v (verbose) reveals what gcc does: $ gcc v o hello hello.c Reading specs from /usr/lib/gcc-lib/i386-redhat-linux/3.2.3/specs Configured with:../configure --prefix=/usr --mandir=/usr/share/man --infodir=/usr/share/info --enable-shared --enablethreads=posix --disable-checking --with-system-zlib --enable- cxa_atexit --host=i386-redhat-linux Thread model: posix gcc version 3.2.3 20030502 (Red Hat Linux 3.2.3-53) /usr/lib/gcc-lib/i386-redhat-linux/3.2.3/cc1 -lang-c -v -D GNUC =3 -D GNUC_MINOR =2 -D GNUC_PATCHLEVEL =3 - D GXX_ABI_VERSION=102 -D ELF -Dunix -D gnu_linux -Dlinux -D ELF -D unix -D gnu_linux -D linux -D unix - D linux -Asystem=posix -D NO_INLINE -D STDC_HOSTED =1 -Acpu=i386 -Amachine=i386 -Di386 -D i386 -D i386 - D tune_i386 hello.c -quiet -dumpbase hello.c -version -o /tmp/ccln1xyb.s GNU CPP version 3.2.3 20030502 (Red Hat Linux 3.2.3-53) (cpplib) (i386 Linux/ELF) GNU C version 3.2.3 20030502 (Red Hat Linux 3.2.3-53) (i386-redhat-linux) compiled by GNU C version 3.2.3 20030502 (Red Hat Linux 3.2.3-53). #include "..." search starts here: #include <...> search starts here: /usr/local/include /usr/lib/gcc-lib/i386-redhat-linux/3.2.3/include /usr/include End of search list. as -V -Qy -o /tmp/ccgjgvrg.o /tmp/ccln1xyb.s GNU assembler version 2.14.90.0.4 (i386-redhat-linux) using BFD version 2.14.90.0.4 20030523 /usr/lib/gcc-lib/i386-redhat-linux/3.2.3/collect2 --eh-frame-hdr -m elf_i386 -dynamic-linker /lib/ld-linux.so.2 -o hello /usr/lib/gcc-lib/i386-redhat-linux/3.2.3/../../../crt1.o /usr/lib/gcc-lib/i386-redhat-linux/3.2.3/../../../crti.o /usr/lib/gcc-lib/i386-redhat-linux/3.2.3/crtbegin.o -L/usr/lib/gcc-lib/i386-redhat-linux/3.2.3 -L/usr/lib/gcc-lib/i386- redhat-linux/3.2.3/../../.. /tmp/ccgjgvrg.o -lgcc -lgcc_eh -lc -lgcc -lgcc_eh /usr/lib/gcc-lib/i386-redhatlinux/3.2.3/crtend.o /usr/lib/gcc-lib/i386-redhat-linux/3.2.3/../../../crtn.o SysProg WS05/06 11

Debugging with GNU-Debugger When command line option -g is specified, gcc puts additional information (symbols, name and addresses of variables and functions) into the compiled file. These symbols can be used by a debugger to show meaningful names instead of numbers. Example with gdb (GNU debugger): $ gcc -g -o hello hello.c 1: /* hello.c */ 2: main() 3: { 4: int i=0; 5: while (i<3) { 6: printf( Hello World!\n ); 7: i = i+1; 8: } 9: } SysProg WS05/06 12

Debugging with GNU-Debugger (2) $ gdb hello (gdb) list 1 main() 2 { 3 int i; 4 while (i<3) { 5 printf("hello World!\n"); 6 i = i+1; 7 } 8 } (gdb) break 5 Breakpoint 1 at 0x804835c: file hello.c, line 5. (gdb) run Breakpoint 1, main () at hello.c:5 5 printf("hello World!\n"); (gdb) print i $1 = 0 (gdb) cont Continuing. Hello World! Breakpoint 1, main () at hello.c:5 5 printf("hello World!\n"); (gdb) print i $2 = 1 (gdb) quit SysProg WS05/06 13

Working Environment UNIX Command Line Since we are using Linux on command line (terminal) you have the following options: ETH Machines locally at ETH (IFW C31, IFW D31, IFW D35) make sure you choose linux when starting the system, login with your n.ethz account remote access to machines at ETH machines rifpc{1-30}.ethz.ch from Linux over ssh $ ssh yourlogin@rifpc{1-30}.ethz.ch from a Windows machine Get PuTTY a simple SSH client http://www.chiark.greenend.org.uk/~sgtatham/putty/download.html Note: there might be some firewall issues when accessing from outside ETH, in those cases use VPN. Personal notebook install Linux generally considered a good idea ;-) install GCC-environment from Cygwin under Windows http://www.cygwin.com SysProg WS05/06 14

Command Line Basics Change directory cd <dirname> cd.. change to parent directory cd ~ change to home directory cd foo/bar change to directory bar (which is subdirectory of foo) from current directory List directory content ls for short listing and ls la for long listing Create subdirectory mkdir <dirname> Delete files rm file Delete files and directories recursively: rm r <dirname> Delete empty directory rmdir dirname Concatenate 3 files to outfile cat file1 file2 file > outfile Tab-completion (<TAB> key auto completes command line) $ ls bar foo $ cd b<tab> by auto completion cd bar Very helpful, makes working on command line even more efficient SysProg WS05/06 15

Man Pages Man pages are the online help for various UNIX tools $ man gcc Shows man page for GCC Use <space> and/or <up>/<down> keys to move Key <q> returns back to command line Use man if you know the name of the tool you want to get the man page Apropos searches description $ apropos compiler g77 (1) GNU project Fortran 77 compiler gcc (1) GNU project C and C++ compiler gcj (1) Ahead-of-time compiler for Java less (8pm) perl pragma to request less of something $ man less shows less opposite of more (section 1) $ man 1 less shows also less opposite of more (section 1) $ man 8pm less shows perl pragma to (section 8pm) If man gcc yields an error No manual entry for gcc make sure that the environment variable is properly set. $ setenv MANPATH /usr/sepp/man:/usr/man:/usr/share/man:$manpath SysProg WS05/06 16