200 likes | 224 Views
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.
E N D
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
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!
The three attributes of a C++/C variable • A name • A type • A value
A C++ program that processes three integer values C++ program C program
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
Output: printf printf(“score = %d\n”,score);
Input: scanf scanf(“%d %d”,&exam1, &exam2);
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
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$
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
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$
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
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!!
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
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
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
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
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
Arrays barr@comp210:~/Student/comp210/examples$ !a array4 Enter 5 characters: abcde The char you entered are: a b c d e