Computer Science 210 Computer Organization Introduction to C.

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.
CSE1301 Computer Programming Lecture 4: C Primitives I.
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
C programming Language and Data Structure For DIT Students.
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.
“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”
Goals of Course Introduction to the programming language C Learn how to program Learn ‘good’ programming practices.
Chapter 11 Programming in C Compilation vs. Interpretation Different ways of translating high-level language Compilation translates code into machine.
C Programming Tutorial – Part I CS Introduction to Operating Systems.
Chapter 3 Processing and Interactive Input. 2 Assignment  The general syntax for an assignment statement is variable = operand; The operand to the right.
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.
Computer Science 210 Computer Organization Arrays.
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.
CSC141 Introduction to Computer Programming Teacher: AHMED MUMTAZ MUSTEHSAN Lecture - 6.
CSCI 3133 Programming with C Instructor: Bindra Shrestha University of Houston – Clear Lake.
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.
1 Lexical Elements, Operators, and the C System. 2 Outline Characters and Lexical Elements Syntax Rules Comments Keywords Identifiers Constants String.
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.
NOTE: C programs consist of functions one of which must be main. C programs consist of functions one of which must be main. Every C program begins executing.
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.
Computer Science 210 Computer Organization
Computer Science 210 Computer Organization
ECE Application Programming
Introduction to C Language
Input/output.
CS1010 Discussion Group 11 Week 4 – Overview of C programming.
C Programming Tutorial – Part I
Getting Started with C.
BY GAWARE S.R. COMPUTER SCI. DEPARTMENT
C Basics.
Introduction to C Programming Language
CS1101 Computational Engineering
Computer Science 210 Computer Organization
Computer Science 210 Computer Organization
Introduction to the C Language
Chapter 11 Introduction to Programming in C
Introduction to CS Your First C Programs
Govt. Polytechnic,Dhangar
Introduction to C Topics Compilation Using the gcc Compiler
CS100A Lecture November 1998 Prelim 3 Statistics Maximum 94
Introduction to C Topics Compilation Using the gcc Compiler
Chapter 11 Programming in C
C – Programming Language
Programming Languages and Paradigms
An Overview of C.
DATA TYPES There are four basic data types associated with variables:
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 and 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.

/* 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

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

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.

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

/* This program prints the amount of interest. */ #include #define INTEREST_RATE.06 int main(){ double principal = ; 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 [ = ];

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

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

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

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

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; }

/* 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”

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)

/* 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

/* 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)

/* 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

/* This program prints the area of a circle, given its input radius. */ #include #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)); } double area(double r){ return PI * r * r; } Scope Rules Global: PI, main, area Local: radius, r