cse 2541 – advanced c programming instructor: yang zhang [email protected]

11
CSE 2541 – Advanced C Programming Instructor: Yang Zhang [email protected]

Upload: winfred-shelton

Post on 30-Dec-2015

221 views

Category:

Documents


3 download

TRANSCRIPT

Page 1: CSE 2541 – Advanced C Programming Instructor: Yang Zhang Zhang.863@osu.edu

CSE 2541 – Advanced C Programming

Instructor: Yang [email protected]

Page 2: CSE 2541 – Advanced C Programming Instructor: Yang Zhang Zhang.863@osu.edu

Course info

•Prereq – CSE 2221 or CSE 222•Co-req – CSE 2231

Website – http://www.cse.ohio-state.edu/~zhangya/2451/

Page 3: CSE 2541 – Advanced C Programming Instructor: Yang Zhang Zhang.863@osu.edu

Brief history of C

•1970’s– Unix– C, from BCPL (Thompson and Ritchie)

•C programming Language– Widely used like the others: Fortran, Pascal– Main form of language for system programming– Available on any machine with C compiler and library

Page 4: CSE 2541 – Advanced C Programming Instructor: Yang Zhang Zhang.863@osu.edu

Why C?

•Popular language–Operating systems (Win, Linux, FreeBSD)–Web servers (Apache)–Web browsers (Fox)–Mail servers (sendmail, postfix)–DNS servers (bind)–Graphics card programming (OpenCL)•Programming language rankings•Why?–Performance–Portability–Familiar to programmers

Page 5: CSE 2541 – Advanced C Programming Instructor: Yang Zhang Zhang.863@osu.edu

Why C?

•Compared to assembly language–Abstracts the hardware view (registers, memory, call

stacks), making code portable and easier–Provides variables, functions, arrays, complex

arithmetic, Boolean expressions•Compared to other high-level languages–Maps almost directly into hardware instructions,

making optimization easier–Provides a minimal set of abstractions compared to

other HLLs–Like other HLLs, makes complex programming

simpler (at the expense of efficiency)

Page 6: CSE 2541 – Advanced C Programming Instructor: Yang Zhang Zhang.863@osu.edu

Hello World – code

/* Hello World! */#include <stdio.h>

int main(){ printf(“Hello World!\n”); return 0;}

Page 7: CSE 2541 – Advanced C Programming Instructor: Yang Zhang Zhang.863@osu.edu

C compilation model

Page 8: CSE 2541 – Advanced C Programming Instructor: Yang Zhang Zhang.863@osu.edu

Hello World – walkthrough

•C program: hello.c– emacs, vi, vim, pico, joe …– Plaintext only

•Preprocessing: hello.s, assembly code– cc -S hello.c

•Compilation: hello.o, a binary file– cc -c hello.s

•Linking: a.out or hello, an executable file– cc hello.o– cc -o hello hello.o

•Loading (dynamical linking) and execution: ./hello– ./a.out– ./hello

Page 9: CSE 2541 – Advanced C Programming Instructor: Yang Zhang Zhang.863@osu.edu

Hello World – content breakdown

•Comment•Preprocessor directive•Function definition•Output statement•Return clause

Page 10: CSE 2541 – Advanced C Programming Instructor: Yang Zhang Zhang.863@osu.edu

#include <stdio.h> int main(){ int first, second, add; float divide; printf("Enter two integers\n"); scanf("%d%d", &first, &second); add = first + second; divide = first / (float)second;

printf("Sum = %d\n",add); printf("Division = %.2f\n",divide); return 0;}

Second Example

Page 11: CSE 2541 – Advanced C Programming Instructor: Yang Zhang Zhang.863@osu.edu

Second example – content breakdown

• Variables• Function calls– Input– Output

• Operators• Typecasting