introduction to “programming in c” lone leth thomsen

59
Introduction to “Programming in C” Lone Leth Thomsen

Upload: george-grant

Post on 11-Jan-2016

222 views

Category:

Documents


1 download

TRANSCRIPT

Page 1: Introduction to “Programming in C” Lone Leth Thomsen

Introduction to “Programming in C”

Lone Leth Thomsen

Page 2: Introduction to “Programming in C” Lone Leth Thomsen

February 2006 Basis-C-1/LL 2

Contents

• Introduction to the course– Purpose, literature

• Mini project/evaluation

• Today’s topics (easy start)

Page 3: Introduction to “Programming in C” Lone Leth Thomsen

February 2006 Basis-C-1/LL 3

Introduction to the course

• Purpose– “a give jer en grundlæggende

programmeringsfærdighed OG en forståelse af datalogiske grundbegreber knyttet til data, kontrol, algoritmer, abstraktion, struktur og korrekthed”

• Notice ”og-delen”, here the focus is on understanding and not just the ability to write programs

Page 4: Introduction to “Programming in C” Lone Leth Thomsen

February 2006 Basis-C-1/LL 4

Practical info

• Lectures Thursday 8.15 - 10.00

• Exercises Thursday 10.15 - 12.00

• 10 modules, SE course– 8 lectures with exercises– 2 modules used on larger exam exercise

Page 5: Introduction to “Programming in C” Lone Leth Thomsen

February 2006 Basis-C-1/LL 5

Mini project/evaluation

• Larger programming exercise as starting point• 2 modules ”reserved” for this• Solved in half-groups• The solution has to be design considerations as

well as an executable program• CD, description and print-out• Questions to program as well as course curriculum• Examination in half-groups, individual evaluation

Page 6: Introduction to “Programming in C” Lone Leth Thomsen

February 2006 Basis-C-1/LL 6

Why do we write programs?

• To implement algorithms that are designed to solve problems

• Programs are important in system design

• Lots of examples and applications, some concrete, others more abstract

Page 7: Introduction to “Programming in C” Lone Leth Thomsen

February 2006 Basis-C-1/LL 7

Analysis

• Analysis of a problem involves identification of input and output as well as requirements and limitations

• This requires that you can specify the problem in a clear and unambiguous way

Page 8: Introduction to “Programming in C” Lone Leth Thomsen

February 2006 Basis-C-1/LL 8

Algorithm design

• Try not to solve all the problem details from the beginning – you’ll never get any further

• Use top-down design– Repeatedly split up the problem into smaller

sub problems– Solve the main task by solving each of the sub

problems

Page 9: Introduction to “Programming in C” Lone Leth Thomsen

February 2006 Basis-C-1/LL 9

Algorithm design 2

• Structured programming is a strategy for solving problems

• A structured program is written using simple control structures. These define an algorithm

• A simple control structure is often a sequence, a selection or a repetition

Page 10: Introduction to “Programming in C” Lone Leth Thomsen

February 2006 Basis-C-1/LL 10

A sequence

• A sequence consists of steps, performed one by one

Page 11: Introduction to “Programming in C” Lone Leth Thomsen

February 2006 Basis-C-1/LL 11

A selection

• A selection contains a condition that can be evaluated to true or false

false true

Page 12: Introduction to “Programming in C” Lone Leth Thomsen

February 2006 Basis-C-1/LL 12

A repetition

• A repetition repeats steps, as long as a condition is true

false

true

Page 13: Introduction to “Programming in C” Lone Leth Thomsen

February 2006 Basis-C-1/LL 13

What is a programming language?

• A set of rules used to communicate an algorithm

• C is a procedural programming language– The traditional model– Specifies an explicit sequence of steps to follow

to obtain a result

Page 14: Introduction to “Programming in C” Lone Leth Thomsen

February 2006 Basis-C-1/LL 14 Michael E. Caspersen, 2000 Gymnasieorientering 1.12

Milepæle i sprogudviklingen

Fortran

Algol

PascalSimula

C

C++

Smalltalk

Java

1960

1957

1967

1971

1980

1985

1995

Lisp

1958

FP

MirandaHaskell

Standard ML PrologBETA

Imperativesprog

Funktions-sprog

Logik-sprog

Page 15: Introduction to “Programming in C” Lone Leth Thomsen

February 2006 Basis-C-1/LL 15

What is an imperative language?

• A program written in an imperative programming language is a collection of commands to the computer to perform certain operations– x:= x + 1

– write(”Restart Windows”)

– computeAverageSalary(totalSalary, nrOfPeople)

– rotate(something)

Page 16: Introduction to “Programming in C” Lone Leth Thomsen

February 2006 Basis-C-1/LL 16

Procedural programmering

• Procedures are abstractions over commands• A programming style that uses commands

in certain control patterns• Models systems whose state changes as a

function over time • Every command has a measurable effect on

its environment• Assignment!!

Page 17: Introduction to “Programming in C” Lone Leth Thomsen

February 2006 Basis-C-1/LL 17

From then till now

• Imperative programming languages are based on the von Neumann architecture (one type of architecture)

• Modern programming languages are based on an abstract, technology independent computation model

Page 18: Introduction to “Programming in C” Lone Leth Thomsen

February 2006 Basis-C-1/LL 18

ANSI C

• American National Standards Institute ”C”– Very common C standard– Developed in 1989

Page 19: Introduction to “Programming in C” Lone Leth Thomsen

February 2006 Basis-C-1/LL 19

C

• C is the result of the further development of earlier languages

• C is the basis for many newer languages (Concurrent C, C++, C#)

• Often called a middle level language – contains the building blocks that you need

Page 20: Introduction to “Programming in C” Lone Leth Thomsen

February 2006 Basis-C-1/LL 20

Programming

• Define the problem

• Write algorithm that solves the problem

• Program the algorithm in C

• Test the program

Page 21: Introduction to “Programming in C” Lone Leth Thomsen

February 2006 Basis-C-1/LL 21

• Programs have to be translated to the target computers machine language

– Compiler: the program that translates

– Source file: input to the compiler

– If the program is syntactically correct, the compiler will save the machine language instructions in an object file

• The linker combines an object file with already existing libraries of functions and procedures in an executable file

Programming 2

Page 22: Introduction to “Programming in C” Lone Leth Thomsen

February 2006 Basis-C-1/LL 22

Programming 3

• Several steps:– Editor: used to make source code– Pre-processor: enhances with include files– Compiler: used to make object code– Linker: used to link object code and libraries– Run: used to load and execute a program

Page 23: Introduction to “Programming in C” Lone Leth Thomsen

February 2006 Basis-C-1/LL 23

Programming 4

edit compile execute

Page 24: Introduction to “Programming in C” Lone Leth Thomsen

February 2006 Basis-C-1/LL 24

Programming in C

• A C program consists of one or more source files, each containing parts of the program– Joint/common declarations are often collected

in header files

Page 25: Introduction to “Programming in C” Lone Leth Thomsen

February 2006 Basis-C-1/LL 25

Hello World, once again/* The traditional first program in honour of Dennis Ritchie, who invented C while at Bell Labs in 1972.*/

#include <stdio.h>

int main(void)

{

printf("Hello, world!\n");

return 0;

}

Page 26: Introduction to “Programming in C” Lone Leth Thomsen

February 2006 Basis-C-1/LL 26

#include <stdio.h> message to the preprocessor that the standard i/o library is to be included

int main(void)

{

printf("Hello, world!\n");

return 0;

}

Page 27: Introduction to “Programming in C” Lone Leth Thomsen

February 2006 Basis-C-1/LL 27

#include <stdio.h>

int main(void) the program starts at main

{

printf("Hello, world!\n");

return 0;

}

Page 28: Introduction to “Programming in C” Lone Leth Thomsen

February 2006 Basis-C-1/LL 28

#include <stdio.h>

int main(void)

{ a new function, identified by {

printf("Hello, world!\n");

return 0;

}

Page 29: Introduction to “Programming in C” Lone Leth Thomsen

February 2006 Basis-C-1/LL 29

#include <stdio.h>

int main(void)

{

printf("Hello, world!\n"); printf prints the text on the screen

return 0;

}

Page 30: Introduction to “Programming in C” Lone Leth Thomsen

February 2006 Basis-C-1/LL 30

#include <stdio.h>

int main(void)

{

printf("Hello, world!\n");

return 0; return 0 indicates correct termination

}

Page 31: Introduction to “Programming in C” Lone Leth Thomsen

February 2006 Basis-C-1/LL 31

#include <stdio.h>

int main(void)

{

printf("Hello, world!\n");

return 0;

} function complete, indicated by }

Page 32: Introduction to “Programming in C” Lone Leth Thomsen

February 2006 Basis-C-1/LL 32

Example#include <stdio.h>

int main(void)

{

char c;

c = 'A';

printf("%c\n", c); /* the letter A is printed */

return 0;

}

Page 33: Introduction to “Programming in C” Lone Leth Thomsen

February 2006 Basis-C-1/LL 33

Example 2

#include <stdio.h>

int main(void)

{

float x, y;

x = 1.0;

y = 2.0;

printf("The sum of x and y is %f.\n", x + y);

return 0;

}

Page 34: Introduction to “Programming in C” Lone Leth Thomsen

February 2006 Basis-C-1/LL 34

#include <stdio.h>

int main(void)

{

char first, middle, last;

int age;

printf("Input your three initials and your age: ");

scanf("%c%c%c%d", &first, &middle, &last, &age);

printf("\nGreetings %c.%c.%c. %s %d.\n", first,

middle, last, "Next year your age will be", age + 1);

return 0;

}

Page 35: Introduction to “Programming in C” Lone Leth Thomsen

February 2006 Basis-C-1/LL 35

/*This version allows white space to be typed between the initials on input.*/

#include <stdio.h>

int main(void)

{

char first, middle, last;

int age;

printf("Input your three initials and your age: ");

scanf("%c %c %c%d", &first, &middle, &last, &age);

printf("\nGreetings %c.%c.%c. %s %d.\n", first,

middle, last, "Next year your age will be", age + 1);

return 0;

}

Page 36: Introduction to “Programming in C” Lone Leth Thomsen

February 2006 Basis-C-1/LL 36

#include <stdio.h>

#define PI 3.141592653589793

int main(void)

{

double radius;

printf("\n%s\n\n%s", "This program computes the area of a circle.", "Input the radius: ");

scanf("%lf", &radius);

printf("\n%s\n%s%.2f%s%.2f%s%.2f\n%s%.5f\n\n",

"Area = PI * radius * radius",

" = ", PI, " * ", radius, " * ", radius,

" = ", PI * radius * radius);

return 0;

}

Page 37: Introduction to “Programming in C” Lone Leth Thomsen

February 2006 Basis-C-1/LL 37

/* Read in two integers and print their sum. */

#include <stdio.h>

int main(void)

{

int a, b, sum;

printf("Input two integers: ");

scanf("%d%d", &a, &b);

sum = a + b;

printf("%d + %d = %d\n", a, b, sum);

return 0;

}

Page 38: Introduction to “Programming in C” Lone Leth Thomsen

February 2006 Basis-C-1/LL 38

How not to do it …int main(

){float qx,

zz,

tt;printf("gimme 3"

);scanf

( "%f%f %f",&qx,&zz

,&tt);printf("averageis=%f",(qx+tt+zz)/3.0);

return 0;}

Page 39: Introduction to “Programming in C” Lone Leth Thomsen

February 2006 Basis-C-1/LL 39

#include <stdio.h>

int main(void)

{

int x, y, z, min;

printf("Input three integers: ");

scanf("%d%d%d", &x, &y, &z);

if (x < y)

min = x;

else

min = y;

if (z < min)

min = z;

printf("The minimum value is %d\n", min);

return 0;

}

Page 40: Introduction to “Programming in C” Lone Leth Thomsen

February 2006 Basis-C-1/LL 40

Editing

• Create a text file using an editor – e.g. vi – containing a C program

• The file name has to end with .c, indicating that the file contains C source code

• E.g. test.c

Page 41: Introduction to “Programming in C” Lone Leth Thomsen

February 2006 Basis-C-1/LL 41

Compiling

• gcc is the GNU C Compiler

• The gcc compiler is a tool used for converting text C source files to executable binary files

• gcc alternately calls the pre-processor, the compiler and the linker

• Errors at this point are called syntax errors or compile-time errors

.

Page 42: Introduction to “Programming in C” Lone Leth Thomsen

February 2006 Basis-C-1/LL 42

Compiling 2

• Using compiler flags can help finding potential fatal errors in source code

• gcc test.c

– Gives an executable file called a.out

• gcc test.c -o test

– Gives an executable file called a.out test (instead of a.out)

• This allows for more readable file names ….

Page 43: Introduction to “Programming in C” Lone Leth Thomsen

February 2006 Basis-C-1/LL 43

C program structure

Pre_processor instructions

main function heading{ declarations statements}

Page 44: Introduction to “Programming in C” Lone Leth Thomsen

February 2006 Basis-C-1/LL 44

C standard libraries

• Libraries contain standard program modules (called functions)– E.g. math, standard input/output, string

manipulation– ANSI needs access to certain standard libraries– Each library has a standard file header, name

ending with .h

Page 45: Introduction to “Programming in C” Lone Leth Thomsen

February 2006 Basis-C-1/LL 45

• As an example:– stdio.h (contains e.g. printf)– Can be used by writing– #include <stdio.h>

C standard libraries 2

Page 46: Introduction to “Programming in C” Lone Leth Thomsen

February 2006 Basis-C-1/LL 46

Symbolic constants

• A symbolic constant is a name that is substituted for a sequence of characters

• #define is used to define constants and macros,– E.g. #define PI = 3.14

Page 47: Introduction to “Programming in C” Lone Leth Thomsen

February 2006 Basis-C-1/LL 47

Comments

• /* this is a comment */

• Comments can span several lines

• Everything between /* and */ is ignored by the compiler

Page 48: Introduction to “Programming in C” Lone Leth Thomsen

February 2006 Basis-C-1/LL 48

main

• main is the function that is called first• All other functions are called from main• Every C program must have a main function• int main(void)

– () after main means that it is a function– int describes the return type– void indicates that the function does not take

arguments

Page 49: Introduction to “Programming in C” Lone Leth Thomsen

February 2006 Basis-C-1/LL 49

Statements

• A statement implies that a certain action is performed

• 3 different kinds in C– Expression statement– Compound statement– Control statement

Page 50: Introduction to “Programming in C” Lone Leth Thomsen

February 2006 Basis-C-1/LL 50

Expression statements

• An expression statement consists of an expression followed by a semi colon

• The execution of such an expression implies the evaluation of the related expression

• E.g. a = 6; c = a + b; ; (empty statement)

Page 51: Introduction to “Programming in C” Lone Leth Thomsen

February 2006 Basis-C-1/LL 51

Compound statements

• Consists of several individual statements enclosed by { }

• Whatever lies inside { } is to be interpreted as a single statement {

statement1;

statement2;

} (NOTE – no ;)

Page 52: Introduction to “Programming in C” Lone Leth Thomsen

February 2006 Basis-C-1/LL 52

Control statements

• These control the flow of execution in a program or a function

• 2 kinds– Selection

• if, if-else, switch

– Repetition• for, while, do-while

Page 53: Introduction to “Programming in C” Lone Leth Thomsen

February 2006 Basis-C-1/LL 53

Return statements

• Used to inform the operating system about program status

• return(b); is the general form

• return(0); indicates that the program has terminated correctly

• All other return values mean error!

Page 54: Introduction to “Programming in C” Lone Leth Thomsen

February 2006 Basis-C-1/LL 54

printf

• Used to write data to standard output• printf(format_string, arg1, arg2 …)• format_string is a character string containing

formatting information• Each group consists of the symbol % and a series of

conversion characters indicating the type• E.g. d for int, c for char, f for float or double• (Note int is d, NOT i)

Page 55: Introduction to “Programming in C” Lone Leth Thomsen

February 2006 Basis-C-1/LL 55

scanf

• Used to read data from standard input• scanf(format_string, &arg1, &arg2 …)• format_string is a character string containing

formatting information• Each group consists of the symbol % and a series

of conversion characters indicating the type• E.g. d for int, c for char, f for float or double• (Same as for printf)

Page 56: Introduction to “Programming in C” Lone Leth Thomsen

February 2006 Basis-C-1/LL 56

scanf 2

• The & symbol indicates that the next variable has to be associated with a value

• & means address-of

• & tells scanf where to find the variables that need a value

• E.g. scanf(”%d%d”, &a, &b)

Page 57: Introduction to “Programming in C” Lone Leth Thomsen

February 2006 Basis-C-1/LL 57

Example

#include <stdio.h>

int main(void){ int a, b, sum; printf(”Enter two integers: \n”); scanf(”%d%d”, &a, &b); sum = a + b; printf(%d + %d = %d\n”, a, b, sum); return(0);}

Page 58: Introduction to “Programming in C” Lone Leth Thomsen

February 2006 Basis-C-1/LL 58

Example 2#include <stdio.h>void main(){ int i=1, sum=0; while (i<=10) { sum = sum + i; i = i + 1; } printf(”The sum is %d\n”,sum); return(0);}

Page 59: Introduction to “Programming in C” Lone Leth Thomsen

February 2006 Basis-C-1/LL 59

Example 2#include <stdio.h> instruction to the pre-processorvoid main() main program{ int i=1, sum=0; a declaration while (i<=10) { sum = sum + i; a compound statement i = i + 1; } printf(”The sum is %d\n”,sum); return(0); return, indicates everything OK }