+ All Categories
Home > Documents > Introduction to C

Introduction to C

Date post: 20-Jan-2016
Category:
Upload: keefer
View: 30 times
Download: 0 times
Share this document with a friend
Description:
Introduction to C. CMSC 104, Section 4 Richard Chang. History of Programming Languages & C. Machine code (aka “binary”) Somehow enter raw sequence of binary patterns 1011010111001011 1011010110101010 Assembly “language” Gave human-friendly syntax to machine code: MOV1200, R0 - PowerPoint PPT Presentation
15
Introduction to C CMSC 104, Section 4 Richard Chang 1
Transcript
Page 1: Introduction to C

Introduction to C

CMSC 104, Section 4

Richard Chang

1

Page 2: Introduction to C

History of Programming Languages & C

Machine code (aka “binary”) Somehow enter raw sequence of binary patterns

1011010111001011

1011010110101010

Assembly “language” Gave human-friendly syntax to machine code:

MOV 1200, R0

SUB 1202, R0

MOV R0, 12002

Page 3: Introduction to C

History of Programming Languages & C

Early high-level languages COBOL

SUBTRACT B FROM A GIVING C MULTIPLY C BY 2 GIVING D

FORTRANS1 = 3.0

S2 = 4.0

H = SQRT((S1 * S1) + (S2 * S2))

3

Page 4: Introduction to C

History of C

Derived from… (wait for it…) “B”! (“B” itself was derived from the BCPL language)

Design goals were for C to be: Efficient Close to the machine Structured: A true high-level language with

sophisticated control flow, data structures

4

Page 5: Introduction to C

History of C

UNIX was recoded in C

PDP-11 was a machine with 64 Kilobytes of addressable memory

C is written in C!

Of course, first versions were written in assembly language

5

Page 6: Introduction to C

Hello World ...

C: main( ) {

printf("hello, world");}

COBOL: MAIN SECTION

DISPLAY “hello, world“STOP RUN.

Fortran77: PROGRAM HELLO

PRINT*, ‘hello, world‘END

Lisp: (defun helloworld ()

(print “hello, world") )

English: Hello, world.

Spanish: Hola mundo

French: Salut le Monde

Greek: Γεια σου κόσμε

6

Page 7: Introduction to C

7

Writing C Programs

A programmer uses a text editor (not the same as a word processor!) to create or modify files containing C code.

Code is also known as source code.

A file containing source code is called a source file.

Page 8: Introduction to C

8

A Simple C Program

One of the first C program examples

1. main ( ) {2. printf (“hello, world”) ;3. }

Page 9: Introduction to C

A Simple C Program…to a Computer

m a i n ( ) { \n \t p r i n f ( “

h e l l o , w o r l d “ ) ; \n

} - - - - - - - - - - - - - - -

9

• So, after a C source file has been created, the programmer must invoke the C compiler before the program can be executed (run).

Page 10: Introduction to C

10

3 Stages of Compilation

Stage 1: Preprocessing combines several text files for the compiler

Stage 2: Compiling translates high-level source code into machine

language (object code)

Stage 2: Linking combines object code from Stage 2 with object

code in libraries to create an executable program

Page 11: Introduction to C

Program Development Using gcc

11

Source File pgm.c

Program Object Code File pgm.o

Executable File a.out

Preprocessor

Modified Source Code in RAM

Compiler

Linker

Other Object Code Files (if any)

Editor

Page 12: Introduction to C

12

Anatomy of a C Program

program header comment

preprocessor directives (if any)

main ( ) { statement(s) }

Page 13: Introduction to C

13

Program Header Comment

Comments help a human being understand what the program does.

It is customary to include comments at the top of the file that include: the name of the file author purpose of the source code in the file

Comments are ignored by the compiler. Comments also appear interspersed in the source

code.

Page 14: Introduction to C

14

Preprocessor Directives

Lines that begin with a # in column 1 are called preprocessor directives (commands).

Example: #include <stdio.h>

tells the compiler that you want to use functions like printf().

Page 15: Introduction to C

15

main ( )

Source code for the program go between the curly braces { } after main()

main() {

/* source code goes here... */

// and comments too!

}


Recommended