Computer Science 210 Computer Organization

Slides:



Advertisements
Similar presentations
Fundamentals of Computer and programming in C Programming Languages, Programs and Programming Rohit Khokher.
Advertisements

C Programming Day 1 based upon Practical C Programming by Steve Oualline CS550 Operating Systems.
Unix Continuum of Tools Do something once: use the command line Do something many times: –Use an alias –Use a shell script Do something that is complex.
1 Key Concepts:  Why C?  Life Cycle Of a C program,  What is a computer program?  A program statement?  Basic parts of a C program,  Printf() function?
Introduction to C Programming Overview of C Hello World program Unix environment C programming basics.
CS100A, Fall 1997, Lectures 221 CS100A, Fall 1997 Lecture 22, Tuesday 18 November Introduction To C Goal: Acquire a reading knowledge of basic C. Concepts:
1 ICS103 Programming in C Lecture 2: Introduction to C (1)
Guide To UNIX Using Linux Third Edition
Chapter 3: Introduction to C Programming Language C development environment A simple program example Characters and tokens Structure of a C program –comment.
An Introduction to C Programming Geb Thomas. Learning Objectives Learn how to write and compile a C program Learn what C libraries are Understand the.
Computer Science 210 Computer Organization Introduction to C.
“C” Programming Language CIS 218. Description C is a procedural languages designed to provide lowlevel access to computer system resources, provide language.
Introduction to Computer Algorithmics and Programming Ceng 113 Variables and Operators in C.
CS140: Intro to CS An Overview of Programming in C by Erin Chambers.
CECS 121 EXAM 1. /* C Programming for the Absolute Beginner */ // by Michael Vine #include main() { printf(“\nC you later\n”); system(“pause”); }
Introduction to “Programming in C”
Chapter 11 Programming in C Compilation vs. Interpretation Different ways of translating high-level language Compilation translates code into machine.
Chapter 3 Processing and Interactive Input. 2 Assignment  The general syntax for an assignment statement is variable = operand; The operand to the right.
Programming With C.
1 計算機程式設計 Introduction to Computer Programming Lecture01: Introduction and Hello World 9/10/2012 Slides modified from Yin Lou, Cornell CS2022: Introduction.
History of C 1950 – FORTRAN (Formula Translator) 1959 – COBOL (Common Business Oriented Language) 1971 – Pascal Between Ada.
1 Programming in C Hello World! Soon I will control the world! Soon I will control the world!
Structure of a C program Preprocessor directive (header file) Program statement } Preprocessor directive Global variable declaration Comments Local variable.
Algorithms  Problem: Write pseudocode for a program that keeps asking the user to input integers until the user enters zero, and then determines and outputs.
Introduction to Programming
CECS 130 EXAM 1. To declare a constant (read only) value: const int x = 20; const float PI = 3.14; Can we do this? const int x;
Khalid Rasheed Shaikh Computer Programming Theory 1.
1 Programming a Computer Lecture Ten. 2 Outline  A quick introduction to the programming language C  Introduction to software packages: Matlab for numerical.
Operating System Discussion Section. The Basics of C Reference: Lecture note 2 and 3 notes.html.
Department of Electronic & Electrical Engineering Introduction to C - The Development cycle. Why C? The development cycle. Using Visual Studio ? A simple.
CMPE13Cyrus Bazeghi 1 Chapter 11 Introduction to Programming in C.
An overview of C Language. Overview of C C language is a general purpose and structured programming language developed by 'Dennis Ritchie' at AT &T's.
C is a high level language (HLL)
Sudeshna Sarkar, IIT Kharagpur 1 Programming and Data Structure Sudeshna Sarkar Lecture 3.
Chapter 1 slides1 What is C? A high-level language that is extremely useful for engineering computations. A computer language that has endured for almost.
From Algorithms to Programs Both are sets of instructions on how to do a task Algorithm: –talking to humans, easy to understand –in plain (English) language.
C language--Introduction. History 1970: B by Ken Thompson at AT&T Bell Lab 1972: C by Dennis Ritchie and Ken Tompson at At&T Bell Lab for UNIX 1978: “The.
UMBC CMSC 104 – Section 01, Fall 2016
Computer Science 210 Computer Organization
Computer Science 210 Computer Organization
Computer Science 210 Computer Organization
Introduction to C Language
CS1010 Discussion Group 11 Week 4 – Overview of C programming.
C Programming Tutorial – Part I
Programming Languages and Paradigms
Algorithms Problem: Write pseudocode for a program that keeps asking the user to input integers until the user enters zero, and then determines and outputs.
Introduction to C Topics Compilation Using the gcc Compiler
BY GAWARE S.R. COMPUTER SCI. DEPARTMENT
Programming Paradigms
Computer Science 210 Computer Organization
Introduction to C Programming Language
CS1101 Computational Engineering
Computer Science 210 Computer Organization
Introduction to the C Language
Chapter 11 Introduction to Programming in C
Introduction to CS Your First C Programs
Computer Science 210 Computer Organization
Chapter 11 Introduction to Programming in C
Govt. Polytechnic,Dhangar
Introduction to C Topics Compilation Using the gcc Compiler
Chapter 11 Introduction to Programming in C
C Programming Getting started Variables Basic C operators Conditionals
Introduction to C Topics Compilation Using the gcc Compiler
Chapter 11 Programming in C
CSC215 Lecture Introduction.
C – Programming Language
Programming Languages and Paradigms
DATA TYPES There are four basic data types associated with variables:
Introduction to C Topics Compilation Using the gcc Compiler
Introduction to C Programming
Presentation transcript:

Computer Science 210 Computer Organization Introduction to C

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

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)

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.

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

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

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>

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

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

Constants, Numbers, and Variables /* This program prints the amount of interest. */ #include <stdio.h> #define INTEREST_RATE .06 int main(){ double principal = 10000.00; 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> ];

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 500 4.33 97 32767 anInt, aDouble, aLetter, garbage); Basic numeric types are double, float, int, and char Uninitialized variables contain garbage!

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

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

Weak Data Typing! 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'); All numeric types are compatible with each other, no need for explicit type conversions (casts)

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)

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

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)

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

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)

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

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 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)); return 0; } double area(double r){ return PI * r * r; Global: PI, main, area Local: radius, r

Modular decomposition and libraries For Monday Modular decomposition and libraries