Presentation is loading. Please wait.

Presentation is loading. Please wait.

Introduction to “Programming in C”

Similar presentations


Presentation on theme: "Introduction to “Programming in C”"— Presentation transcript:

1 Introduction to “Programming in C”
Lone Leth Thomsen

2 Contents Introduction to the course Mini project/evaluation
Purpose, literature Mini project/evaluation Today’s topics (easy start) 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 February 2006 Basis-C-1/LL

4 Practical info Lectures Thursday 8.15 - 10.00
Exercises Thursday 10 modules, SE course 8 lectures with exercises 2 modules used on larger exam exercise 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 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 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 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 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 February 2006 Basis-C-1/LL

10 A sequence A sequence consists of steps, performed one by one
February 2006 Basis-C-1/LL

11 A selection A selection contains a condition that can be evaluated to true or false false true February 2006 Basis-C-1/LL

12 A repetition A repetition repeats steps, as long as a condition is true false true 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 February 2006 Basis-C-1/LL

14 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) 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!! 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 February 2006 Basis-C-1/LL

18 ANSI C American National Standards Institute ”C”
Very common C standard Developed in 1989 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 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 February 2006 Basis-C-1/LL

21 Programming 2 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 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 February 2006 Basis-C-1/LL

23 Programming 4 edit compile execute 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 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; } February 2006 Basis-C-1/LL

26 printf("Hello, world!\n"); return 0; }
#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; } February 2006 Basis-C-1/LL

27 #include <stdio.h>
int main(void) the program starts at main { printf("Hello, world!\n"); return 0; } February 2006 Basis-C-1/LL

28 #include <stdio.h>
int main(void) { a new function, identified by { printf("Hello, world!\n"); return 0; } 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; } February 2006 Basis-C-1/LL

30 #include <stdio.h>
int main(void) { printf("Hello, world!\n"); return 0; return 0 indicates correct termination } February 2006 Basis-C-1/LL

31 #include <stdio.h>
int main(void) { printf("Hello, world!\n"); return 0; } function complete, indicated by } 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; } February 2006 Basis-C-1/LL

33 Example 2 #include <stdio.h> int main(void) { float x, y;
printf("The sum of x and y is %f.\n", x + y); return 0; } 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; } February 2006 Basis-C-1/LL

35 #include <stdio.h>
/*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; } February 2006 Basis-C-1/LL

36 #include <stdio.h>
#define PI 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; } 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; } 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;} 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; } 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 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 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 …. February 2006 Basis-C-1/LL

43 C program structure Pre_processor instructions main function heading {
declarations statements } 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 February 2006 Basis-C-1/LL

45 C standard libraries 2 As an example: stdio.h (contains e.g. printf)
Can be used by writing #include <stdio.h> 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 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 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 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 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) 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 ;) 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 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! 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) 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) 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) 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); } 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); February 2006 Basis-C-1/LL

59 Example 2 #include <stdio.h> instruction to the pre-processor
void 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 February 2006 Basis-C-1/LL


Download ppt "Introduction to “Programming in C”"

Similar presentations


Ads by Google