Presentation is loading. Please wait.

Presentation is loading. Please wait.

Computer Science 210 Computer Organization Introduction to C.

Similar presentations


Presentation on theme: "Computer Science 210 Computer Organization Introduction to C."— Presentation transcript:

1 Computer Science 210 Computer Organization Introduction to C

2 Origins Developed in the 1970s by Brian Kernighan and Dennis Ritchie at ATT Bell Labs They also developed UNIX Much of UNIX is written in C, and C is the preferred language for developing UNIX system tools

3 Classification C is an imperative language (function definitions, but no classes and objects) Produces very efficient compiled code Not very safe (missing some compile-time and run-time error checks)

4 A Simple C Program One or more #include directives (like Python imports) One and only one main function (like Java’s main method) Can have other function definitions, global variables, type synonyms, etc.

5 /* Author: Ken Lambert This program outputs the string "Hello world!" */ #include int main(){ printf("Hello world!\n"); } A First Program /* */ enclose a program comment #include is a preprocessor directive stdio is a library of I/O resources printf is an output function main does not return a value for now All statements must end with a ; {} enclose blocks or sequences of statements

6 Compile, Link, and Run Edit and save the source file with a.c extension Run gcc –o.c If no errors, run./

7 Program Development Compiler Linker Loader Runtime System Editor Translate to machine code Add library code Place code in appropriate memory locations Execute code Create source code Preprocessor Include code from library header files, expand macros, etc.

8 Useful Standard Libraries Library Header FileFunctions stdio.h Input and output, including file I/O stdlib.h Arithmetic, random numbers, terminating execution, sorting and searching math.h Advanced math (trig) string.h String processing A header file contains function headers that allow your program to reference the library’s functions The preprocessor combines these headers with your source program The linker hooks these references to the compiled object code of the library

9 /* This program prints the amount of interest. */ #include #define INTEREST_RATE.06 int main(){ double principal = 10000.00; printf("Interest is %.2f\n", INTEREST_RATE * principal); } Constants, Numbers, and Variables #define is a preprocessor macro that defines a global constant Variables must be declared with a data type, using the syntax [ = ];

10 int anInt = 500; double aDouble = 4.33; char aLetter = 'a'; int garbage; printf("%d %.2f %d %d\n", // Outputs 500 4.33 97 32767 anInt, aDouble, aLetter, garbage); More on Variables Basic numeric types are double, float, int, and char Uninitialized variables contain garbage!

11 int anInt = 500; double aDouble = 4.33 double sum = anInt + aDouble; printf("The sum of %d and %.2f is %.2f\n", anInt, aDouble, sum); More on printf printf builds a formatted string, like Python’s % operator The format flags d, f, and s are used for integers, doubles, and strings, respectively Field widths can be specified (a signed integer following % ) Precision for doubles can also be specified

12 char aLetter = 'a'; printf("%d\n", aLetter); // Outputs 97 putchar(aLetter); // Outputs a putchar('\n'); Character Output with putchar A character is automatically treated as an integer (its ASCII value) in many contexts

13 int anInt = 97; double aDouble = 4.56; char aLetter = 'A'; int four = aDouble; int letterB = aLetter + 1; char lettera = anInt; // Outputs 4 66 97 B a printf("%d %d %d ", four, letterB, lettera); putchar(letterB); putchar(' '); putchar(lettera); putchar('\n'); Weak Data Typing! All numeric types are compatible with each other, no need for explicit type conversions (casts)

14 char aLetter; for (aLetter = 'A'; aLetter <= 'Z'; aLetter++) putchar(aLetter); Count-Controlled Loops Increment operator ++ modifies the variable Boolean values are 0 (meaning false) and any other value (meaning true) char aLetter = 'A'; while (aLetter <= 'Z'){ putchar(aLetter); aLetter++; // Same as aLetter = aLetter + 1; }

15 /* This program prints sum of two input integers. */ #include int main(){ int first, second; printf("Enter a number: "); scanf("%d", &first); printf("Enter a number: "); scanf("%d", &second); printf("The sum is %d\n", first + second); } Input with scanf scanf expects a format string with an input type flag ( %f works with float, but not double ) The input variable is preceded by the & symbol, which indicates “address of”

16 int sum = 0; int count = 0; int number; printf("Enter a number or 0 to stop: "); scanf("%d", &number); while (number){ sum += number; count++; printf("Enter a number or 0 to stop: "); scanf("%d", &number); } if (count > 0) printf("The average is %f\n", sum / (double) count); else printf("No numbers entered\n"); Sentinel-Based Loop and Cast Operators The cast operator has the syntax ( ) Note the Boolean expression (number)

17 /* This program prints the factorial of 6. */ #include int factorial(int n){ if (n == 1) return 1; else return n * factorial(n – 1); } int main(){ printf("The factorial of 6 is %d\n", factorial(6)); } Defining a Function A function has a return type, which should be void if no value is returned

18 /* This program prints the factorial of 6. */ #include int main(){ printf("The factorial of 6 is %d\n", factorial(6)); } int factorial(int n){ if (n == 1) return 1; else return n * factorial(n - 1); } Top-Down Implementation Functions can be defined in any order (this one goes top-down)

19 /* This program prints the factorial of 6. */ #include int factorial(int n); // Prototype of factorial int main(){ printf("The factorial of 6 is %d\n", factorial(6)); } int factorial(int n){ // Implementation of factorial if (n == 1) return 1; else return n * factorial(n - 1); } Using Function Prototypes A function prototype is the function’s header The function implementation comes later

20 /* This program prints the area of a circle, given its input radius. */ #include #define PI 3.1416 double area(double r); int main(){ float radius; printf("Enter the radius: "); scanf("%f", &radius); printf("The area is %f\n", area(radius)); } double area(double r){ return PI * r * r; } Scope Rules Global: PI, main, area Local: radius, r


Download ppt "Computer Science 210 Computer Organization Introduction to C."

Similar presentations


Ads by Google