Presentation is loading. Please wait.

Presentation is loading. Please wait.

Computer Science 210 Computer Organization

Similar presentations


Presentation on theme: "Computer Science 210 Computer Organization"— 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 or 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 A First Program /* */ enclose a multi-line program comment
Author: Ken Lambert This program outputs the string "Hello world!" */ #include <stdio.h> int main(){ printf("Hello world!\n"); return 0; } /* */ enclose a multi-line program comment #include is a preprocessor directive stdio is a library of I/O resources printf (print formatted) is an output function main returns 0 on success, nonzero (-1) on failure All statements must end with a ; {} enclose blocks or sequences of statements

6 C Compilers gcc already on Linux
For MacOS, must download Xcode to get gcc Run gcc at terminal prompt Windows: Use Visual Studio C++

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

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

9 Useful Standard Libraries
Library Header File Functions 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

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

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

12 More on printf int anInt = 500; double aDouble = 4.33 double sum = anInt + aDouble; printf("The sum of %d and %.2f is %.2f\n", anInt, aDouble, sum); 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

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

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

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

16 Input with scanf /* This program prints sum of two input integers. */ #include <stdio.h> int main(){ int first, second; printf("Enter a number: "); scanf("%d", &first); scanf("%d", &second); printf("The sum is %d\n", first + second); } 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

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

18 Defining a Function /* This program prints the factorial of 6. */ #include <stdio.h> 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)); return 0; A function has a return type, which should be void if no value is returned

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

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

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

22 Modular decomposition and libraries
For Monday Modular decomposition and libraries


Download ppt "Computer Science 210 Computer Organization"

Similar presentations


Ads by Google