1 / 20

Introduction to Computer Organization & Systems

Discover the efficiency of C programming, emphasizing conciseness and performance over readability and maintainability. Learn about variables, handling integers, arrays, and pointers, with practical examples.

garyj
Download Presentation

Introduction to Computer Organization & Systems

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


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

  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 C++ program C program

  5. Note the & A C++ program that processes three integer values C++ program C program #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() { inti; intv[SIZE]; for (i = 0; i < SIZE; i++) { printf("Enter an integer: \n"); scanf("%d", &v[i]); } for (i = 0; i < SIZE; 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) barr@comp210:~/Student/comp210/examples$ ./array1 Enter an integer: 9 Enter an integer: 8 Enter an integer: 7 Enter an integer: 6 v[0] = 9: v[1] = 8: v[2] = 7: v[3] = 6: barr@comp210:~/Student/comp210/examples$ 

  11. The for statement with an array #include <stdio.h> #define SIZE 4 Int main() { inti; intv[SIZE]; for (i = 0; i < SIZE; i++) { printf("Enter an integer: \n"); scanf("%d", &v[i]); } for (i = 0; i < SIZE; 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) barr@comp210:~/Student/comp210/examples$ ./array2 Enter an integer: 9 Enter an integer: 8 Enter an integer: 7 Enter an integer: 6 v[3] = 6: v[2] = 7: v[1] = 8: v[0] = 9: barr@comp210:~/Student/comp210/examples$ 

  13. Pointer declaration Pointer assignment Accessing value that pointer points at Changing a value in variable that pointer points at 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; } 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. No &!! Why? 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; } See Student/comp210/examples/ptr2.c

  16. Arrays // array3.c #include <stdio.h> #define SIZE 5 intreadints(int s[ ],int max) { intc,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]; inti, 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 intreadchars(char *s,int max) { inti=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 // arry4.c int main() { char *line; inti, 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 barr@comp210:~/Student/comp210/examples$ !a array4 Enter 5 characters:   abcde The char you entered are:  a b c d e

More Related