Presentation is loading. Please wait.

Presentation is loading. Please wait.

Introduction to Computer Organization & Systems

Similar presentations


Presentation on theme: "Introduction to Computer Organization & Systems"— Presentation transcript:

1 Introduction to Computer Organization & Systems
John Barr Topics: Intro to C for C++ programmers Types in C: int and floating point C I/O

2 C Programming & Systems Programming
Specific type of programming Not used in the development of most applications Emphasis is on conciseness and efficiency (space and time) Ignores readability and maintainability You will get fired if you program like this in most situations!

3 The three attributes of a C++/C variable
A name A type A value

4 A C++ program that processes three integer values

5 A C++ program that processes three integer values
Note the & #include <stdio.h> #define bonus 5 int exam1, exam2, score; main( ){ scanf(“%d %d”, &exam1, &exam2); score = (exam1 + exam2)/2 + bonus; printf(“score = %d\n”, score); } See Student/comp210/examples/testRead.c

6 A C++ program that processes three integer values (Cont’d)

7 Output: printf printf(“score = %d\n”,score);

8 Input: scanf scanf(“%d %d”,&exam1, &exam2);

9 The for statement with an array
#include <stdio.h> #define SIZE 4 Int main() { int i; int v[SIZE]; for (i = 0; i < SIZE; i++) printf("Enter an integer: \n"); scanf("%d", &v[i]); } printf("v[%d] = %d: \n", i, v[i]); return 0; See Student/comp210/examples/array1.c

10 The for statement with an array (Cont’d)
./array1 Enter an integer: v[0] = 9: v[1] = 8: v[2] = 7: v[3] = 6:

11 The for statement with an array
#include <stdio.h> #define SIZE 4 Int main() { int i; int v[SIZE]; for (i = 0; i < SIZE; i++) printf("Enter an integer: \n"); scanf("%d", &v[i]); } printf("v[%d] = %d: \n", SIZE-i-1, v[SIZE - i - 1]); return 0; Why the “- 1” ?? See Student/comp210/examples/array2.c

12 The for statement with an array (Cont’d)
./array2 Enter an integer: v[3] = 6: v[2] = 7: v[1] = 8: v[0] = 9:

13 Pointers #include <stdio.h> int main() { int n1, n2, *intPtr; intPtr = &n1; printf ( "Enter two numbers\n"); scanf("%d%d",&n1,&n2); printf ( "The numbers you entered are: %d and %d \n", n1, n2); printf ("n1 is %d\n", *intPtr); intPtr = &n2; printf ( "n2 is %d\n",*intPtr); *intPtr = n1 + n2; printf ( "%d + %d = %d\n",n1, *intPtr, n1 + *intPtr); return 0; } Pointer declaration Pointer assignment Accessing value that pointer points at Changing a value in variable that pointer points at See Student/comp210/examples/ptr1.c

14 Pointers Can create a pointer variable without memory
To use must create memory There is a library function to do this: malloc #include <stdio.h> #include <stdlib.h> //need this library int main() { int *intPtr; // intPtr is a pointer to an int *intPtr = 10; // causes a seg fault!!

15 Pointers and Memory #include <stdio.h> #include <stdlib.h> //need this library for malloc int main() { int *intPtr; // intPtr is a pointer to an int intPtr = (int *)malloc(sizeof(int)); // must first allocate memory printf(“Enter an integer: “); scanf(“%d”, intPtr); printf(“You entered %d\n”, *intPtr); // why no & in front of intPtr? return 0; } No &!! Why? See Student/comp210/examples/ptr2.c

16 Arrays // array3.c #include <stdio.h> #define SIZE 5 int readints(int s[ ],int max) { int c,i=0; printf("Enter %d numbers: \n",max); while (i < max) scanf("%d",&s[i++]); return(i); } You don’t have to specify the size of an array that is a parameter See Student/comp210/examples/array3.c

17 Arrays // array3.c continued int main() { int line[SIZE]; int i, n; n = readints(line, SIZE); printf("The numbers you entered are: \n"); for (i = 0; i < n; i++) printf("%d\n", line[i]); } You do have to specify the size of an array that is a variable (unless you want to use dynamic memory) See Student/comp210/examples/array3.c

18 Arrays // array4.c #include <stdio.h> #define SIZE 5 int readchars(char *s,int max) { int i=0; printf("Enter %d characters: \n",max); while (i < max){ scanf("%c",&s[i++]); } return(i); Instead of an array you could receive a pointer to an int and use that as an array. See Student/comp210/examples/array4.c

19 Arrays A pointer can be used as an array
// arry4.c int main() { char *line; int i, n; line = (char *)malloc(SIZE*sizeof(char)); n = readchars(line, SIZE); printf("The char you entered are: \n"); for (i = 0; i < n; i++) printf("%c\n", *(line + i)); } return 0; A pointer can be used as an array Allocate memory for every array element (there are SIZE elements) This is pointer syntax and uses pointer arithmetic

20 Arrays !a array4 Enter 5 characters: abcde The char you entered are: a b c d e


Download ppt "Introduction to Computer Organization & Systems"

Similar presentations


Ads by Google