Download presentation
Presentation is loading. Please wait.
Published byCecil Nelson Modified over 9 years ago
1
1 COMP 2130 Introduction to Computer Systems Computing Science Thompson Rivers University
2
Contents 2 Introduction Data Types Arrays & Functions Control Flow Program Structure
3
Introduction: Example ‘C’ code 3 #include... /* print Fahrenheit-Celsius table for fahr = 0, 20, 40,..., 300, using a loop */ main() { int fahr, celsius; int lower, upper, step; lower = 0;/* lower limit of temperature scale */ upper = 300;// upper limit step = 20;// step size fahr = lower; while (fahr <= upper) { celsius = 5 * (fahr-32) / 9;// integer value? printf("%d\t%d\n", fahr, celsius); fahr = fahr + step; } }
4
printf("%d\t%d\n", fahr, celsius); %d specifies an integer argument (d: decimal). Output 0 -17 20-6 404 6015 8026 10037 12048 14060 16071 18082 ……..
5
5 Is there any problem in the above output? The Celsius temperatures in the previous out are not accurate. For example 0 o F is -17.8 o C. Then? We can use float data type for fahr and celsius instead of int. float fahr, celsius; Then how to print real numbers using printf() ? celsius = 5 * (fahr-32) / 9;// okay? printf("%3.0f\t%6.1f\n", fahr, celsius); %f specifies a floating point argument (f: floating point). 6.1 means Print 6 digits before the decimal point (called width) and one digit after the decimal point (called precision).
6
6 Some easy errors: printf("%d\t%6.1f\n", fahr, celsius); ??? printf("%3.0f\t%6.1f\n", fahr); ??? printf("%6.1f\n", fahr, celsius); ???
7
Review 7
8
Boolean values in ‘C’ 8 When evaluating integers as Booleans in C, are negative numbers true or false? A zero value, null pointer value, or null member pointer value is converted to false; any other value is converted to true.
9
9 If -Else
10
Arrays 10 /* * C Program to Find the Largest Number in an Array */ #include int main() { int array[50], size, i, largest; printf("\n Enter the size of the array: "); scanf("%d", &size); printf("\n Enter %d elements of the array: ", size); for (i = 0; i < size; i++) scanf("%d", &array[i]); largest = array[0]; for (i = 1; i < size; i++) { if (largest < array[i]) largest = array[i]; } printf("\n largest element present in the given array is : %d", largest); return 0; }
11
Functions 11 #include int power(int m, int n); int main() { int i; for (i = 0; i < 10; i++) printf("%d %d %d\n", i, power(2, i), power(3, i)); return 0; } int power(int base, int n) { int i, p; p = 1; for (i = 1; i <= n; i++) p = p * base; return p; }
12
2. Types, Operators, Expressions 12
13
Data Types 13
14
Data Types 14
15
Data Types 15
16
int getchar(void) 16 C library function - getchar() The C library function int getchar(void) gets a character (an unsigned char) from stdin. This function returns the character read as an unsigned char cast to an int or EOF on end of file or error. Remember that chars in C are integers. char is just another integer type but smaller than int.
17
3. Control Flow 17 Statement …; Block, or compound statement { … } The same control structures as Java if, … for, while, do-while switch break, continue You must know them all already.
18
Goto and Labels 18 for (i = 0; i < n; i++) for (j = 0; j < m; j++) if (a[i] == b[j]) goto found; /* didn't find any common element */... found: /* got one: a[i] == b[j] */... Better not use goto statements Can you convert the above code so that you would not use goto ?
19
4. Functions and Program Structure 19 Function in C is the same as method in Java. return-type function-name(argument declarations) Various parts may be absent.
20
External Variables 20 If a large number of variables must be shared among functions, external variables (or also called global variables) are more convenient and efficient than long argument lists. External variables are declared outside of any function, usually with initial values. Automatic variables (local variables and parameters) are internal to a function; they come into existence when the function is entered, and disappear when it is left. External variables, on the other hand, are permanent, so they can retain values from one function invocation to the next. Thus if two functions must share some data, yet neither calls the other, it is often most convenient if the shared data is kept in external variables rather than being passed in and out via arguments.
21
Static Variables 21 The static declaration, applied to an external variable or function, limits the scope of that object to the rest of the source file being compiled. External static thus provides a way to hide names from other files. static char buf[BUFSIZE]; // only in this file static int bufp = 0; // only in this file int getch(void) {... } void ungetch(int c) {... } Static in Java has a bit different meaning.
22
Register Variables 22 A register declaration advises the compiler that the variable in question will be heavily used. The idea is that register variables are to be placed in machine registers, which may result in smaller and faster programs. But compilers are free to ignore the advice. register int x; register char c; f(register unsigned m, register long n) { register int i;... } Usually for index variables used in loop structures
23
Initialization 23 Very similar to Java In the absence of explicit initialization, external and static variables are guaranteed to be initialized to zero; but automatic and register variables have undefined (i.e., garbage) initial values.
24
Scope Rules 24 A scope in any programming is a region of the program where a defined variable can have its existence and beyond that variable can not be accessed. There are three places where variables can be declared in C programming language: Inside a function or a block which is called local variables, Outside of all functions which is called global variables. In the definition of function parameters which is called formal parameters.
25
Scope Rules- local variables 25 Variables that are declared inside a function or block are called local variables. They can be used only by statements that are inside that function or block of code. #include int main () { /* local variable declaration */ int a, b; int c; /* actual initialization */ a = 10; b = 20; c = a + b; printf ("value of a = %d, b = %d and c = %d\n", a, b, c); return 0; }
26
Scope Rules- global variables 26 Global variables are defined outside of a function, usually on top of the program. The global variables will hold their value throughout the lifetime of your program and they can be accessed inside any of the functions defined for the program. #include /* global variable declaration */ int g; int main () { /* local variable declaration */ int a, b; /* actual initialization */ a = 10; b = 20; g = a + b; printf ("value of a = %d, b = %d and g = %d\n", a, b, g); return 0; } A program can have same name for local and global variables but value of local variable inside a function will take preference.
27
Scope Rules- formal parameters 27 Function parameters, formal parameters, are treated as local variables with-in that function and they will take preference over the global variables. #include /* global variable declaration */ int a = 20; int main () { /* local variable declaration in main function */ int a = 10; int b = 20; int c = 0; printf ("value of a in main() = %d\n", a); c = sum( a, b); printf ("value of c in main() = %d\n", c); return 0; } int sum(int a, int b) {/* function to add two integers */ printf ("value of a in sum() = %d\n", a); printf ("value of b in sum() = %d\n", b); return a + b; }
28
Questions? 28
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.