Binghamton University. CS-120 Summer Introduction to C. Text: Introduction to Computer Systems : Chapters 11, 12, 14, 13

Similar documents
Binghamton University. CS-211 Fall Syntax. What the Compiler needs to understand your program

UNIT- 3 Introduction to C++

Programming in C and Data Structures [15PCD13/23] 1. PROGRAMMING IN C AND DATA STRUCTURES [As per Choice Based Credit System (CBCS) scheme]

C Language Part 1 Digital Computer Concept and Practice Copyright 2012 by Jaejin Lee

Syntax and Variables

Presented By : Gaurav Juneja

CSCE 110 PROGRAMMING FUNDAMENTALS

Character Set. The character set of C represents alphabet, digit or any symbol used to represent information. Digits 0, 1, 2, 3, 9

UNIT - I. Introduction to C Programming. BY A. Vijay Bharath

Features of C. Portable Procedural / Modular Structured Language Statically typed Middle level language

Computers Programming Course 5. Iulian Năstac

COMPUTER SCIENCE HIGHER SECONDARY FIRST YEAR. VOLUME II - CHAPTER 10 PROBLEM SOLVING TECHNIQUES AND C PROGRAMMING 1,2,3 & 5 MARKS

Fundamental of Programming (C)

EL2310 Scientific Programming

IECD Institute for Entrepreneurship and Career Development Bharathidasan University, Tiruchirappalli 23.

Programming in C and C++

C/Java Syntax. January 13, Slides by Mark Hancock (adapted from notes by Craig Schock)

C/Java Syntax. Lecture 02 Summary. Keywords Variable Declarations Data Types Operators Statements. Functions. if, switch, while, do-while, for

Fundamental of C programming. - Ompal Singh

Programming in C and C++

Chapter 1 & 2 Introduction to C Language

BCA-105 C Language What is C? History of C

Lecture 02 Summary. C/Java Syntax 1/14/2009. Keywords Variable Declarations Data Types Operators Statements. Functions

Structure of this course. C and C++ Past Exam Questions. Text books

INTRODUCTION 1 AND REVIEW

DEPARTMENT OF MATHS, MJ COLLEGE

6.096 Introduction to C++ January (IAP) 2009

Programming Fundamentals (CS 302 ) Dr. Ihsan Ullah. Lecturer Department of Computer Science & IT University of Balochistan

>B<82. 2Soft ware. C Language manual. Copyright COSMIC Software 1999, 2001 All rights reserved.

The component base of C language. Nguyễn Dũng Faculty of IT Hue College of Science

C LANGUAGE AND ITS DIFFERENT TYPES OF FUNCTIONS

Variables Data types Variable I/O. C introduction. Variables. Variables 1 / 14

Preview from Notesale.co.uk Page 6 of 52

Introduction to C. Why C? Difference between Python and C C compiler stages Basic syntax in C

(heavily based on last year s notes (Andrew Moore) with thanks to Alastair R. Beresford. 1. Types Variables Expressions & Statements 2/23

Java Notes. 10th ICSE. Saravanan Ganesh

C Language, Token, Keywords, Constant, variable

1 Lexical Considerations

C OVERVIEW BASIC C PROGRAM STRUCTURE. C Overview. Basic C Program Structure

Lexical Considerations

Appendix. Grammar. A.1 Introduction. A.2 Keywords. There is no worse danger for a teacher than to teach words instead of things.

Differentiate Between Keywords and Identifiers

C OVERVIEW. C Overview. Goals speed portability allow access to features of the architecture speed

Programming in C. What is C?... What is C?

Programming in C UVic SEng 265

CprE 288 Introduction to Embedded Systems Exam 1 Review. 1

Language Design COMS W4115. Prof. Stephen A. Edwards Fall 2006 Columbia University Department of Computer Science

Class B.Com. III Sem.

Programming in C. What is C?... What is C?

CS Prof J.P.Morrison

Chapter 3: Operators, Expressions and Type Conversion

PES INSTITUTE OF TECHNOLOGY (BSC) I MCA, First IA Test, November 2015 Programming Using C (13MCA11) Solution Set Faculty: Jeny Jijo

Lexical Considerations

CS 251 Intermediate Programming Java Basics

Typescript on LLVM Language Reference Manual

Lecture 05 I/O statements Printf, Scanf Simple statements, Compound statements

EC 413 Computer Organization

Appendix G C/C++ Notes. C/C++ Coding Style Guidelines Ray Mitchell 475

Object oriented programming. Instructor: Masoud Asghari Web page: Ch: 3

Review of the C Programming Language

ITC213: STRUCTURED PROGRAMMING. Bhaskar Shrestha National College of Computer Studies Tribhuvan University

PART I. Part II Answer to all the questions 1. What is meant by a token? Name the token available in C++.

edunepal_info

Unit-II Programming and Problem Solving (BE1/4 CSE-2)

Mechatronics and Microcontrollers. Szilárd Aradi PhD Refresh of C

Course Outline Introduction to C-Programming

Department of Computer Applications

Introduction to C Language

Fundamentals of Programming

C Syntax Out: 15 September, 1995

C: How to Program. Week /Mar/05

GAWK Language Reference Manual

Review of the C Programming Language for Principles of Operating Systems

Language Design COMS W4115. Prof. Stephen A. Edwards Spring 2003 Columbia University Department of Computer Science

Rule 1-3: Use white space to break a function into paragraphs. Rule 1-5: Avoid very long statements. Use multiple shorter statements instead.

EL2310 Scientific Programming

Introduction to C programming. By Avani M. Sakhapara Asst Professor, IT Dept, KJSCE

OBJECT ORIENTED PROGRAMMING

ARG! Language Reference Manual

Programming Language Basics

Chapter 12 Variables and Operators

from Appendix B: Some C Essentials

Computer Components. Software{ User Programs. Operating System. Hardware

Flow Control. CSC215 Lecture

Introduction to Computing Lecture 01: Introduction to C

Basics of Java Programming

Expressions and Data Types CSC 121 Spring 2015 Howard Rosenthal

Chapter 2. Lexical Elements & Operators

The C Language Reference Manual

C Programming Class I

Computer programming & Data Structures Unit I Introduction to Computers

In 1978, Brian Kernighan and Dennis Ritchie produced the first publicly available description of C, now known as the K&R standard.

Chapter 2 - Introduction to C Programming

Overview of C. Basic Data Types Constants Variables Identifiers Keywords Basic I/O

I BCA[ ] SEMESTER I CORE: C PROGRAMMING - 106A Multiple Choice Questions.

Variables in C. Variables in C. What Are Variables in C? CMSC 104, Fall 2012 John Y. Park

HISTORY OF C LANGUAGE. Facts about C. Why Use C?

Control Flow. COMS W1007 Introduction to Computer Science. Christopher Conway 3 June 2003

Programming. Elementary Concepts

4.1. Structured program development Overview of C language

Transcription:

Introduction to C Text: Introduction to Computer Systems : Chapters 11, 12, 14, 13

Problem: Too Many Details For example: Lab 7 Bubble Sort Needed to keep track of too many details! Outer Loop When do I quit? How many times have I been through already? Inner Loop How far am I in the vector? Really, need to pseudo-code! do { ordered=1 for(i=0;i<cnt; i++) { if (VEC[i]>VEC[i+1]) { swap VEC[i], VEC[i+1] ordered=0 } } until (ordered==1)

Problem: Translating from Psuedo-Code Translation is Mechanical, Laborious, and Error-Prone Why not let computer perform translation? Write in Pseudo-Code Allow computer to translate Pseudo-Code to Assembler Pseudo-Code is a High Level Language Translator is a Compiler Example: Bubble Sort in C C code translated to LC3

Origins of C MULTICs operating system MIT / General Electric / Bell Labs Collaboration, 1964 1970 Design an operating system to run GE-645 Main Frame Programmed in Assembler and PL/I BCPL (Basic Combined Programming Language) Martin Richards ( of Cambridge) 1966 Used to write Compilers First use of {}, //, First Hello World No Types (interpretation based on operator)

Origins of C UNIX (1969-1970 s) Operating System Bell Labs PDP-7, PDP-11 B Language (BCPL Lite ) Ken Thompson and Dennis Ritchie Still no types

How C got it s name.

Origins of C B + types + structures = C 1978 The C Programming Language (Kernighan and Ritchie) K&R 1983 ANSI Standard 1988 POSIX standard (libraries) 1990 ISO C89 1999 ISO C99 2011 ISO C11

C Syntax Comments: /* comment */ or // comment to end of line Statement: instruction that ends with a semi-colon White space (blanks, tabs, new-lines) Ignored! (except as a separator between tokens) Not like python, where indentation used to define block structure!

C Functions Functions are the primary building block in C All executable C code is contained in a function To explain functions, we need to know about C types of data

C Types Built-In Integers signed / unsigned char / short / int / long Real Numbers float / double / long double Character (char) void Derived Arrays: <type>[<count>] Pointers: *<type> User Defined: struct / enum / union typedef

LC3 vs.c - Subroutines LC3 - Subroutines PUSH ; put value in R0 on stack ret C - Functions void push(int value) { } group of LC3 instructions args by convention w/ caller return value by convention regs shared with caller memory shared w/ caller group of C statements args specified in function def. return value type in def. no regs variables usually not shared

C Function Conventions Argument list: comma separated list of variables type/names specified in function signature A function works on a copy of the arguments Type of return value specified in function signature A function returns a single value via a return statement exception: void indicates zero values returned Compiler handles the details Argument copying Setting return values in caller Keeping track of return location etc. etc. etc.

The main function Every C program must have a main function When the C program is executed, the main function is invoked Arguments to main yet to come When the main function ends, the C program ends return value of type int is the return code of the program 0 indicates success ~0 indicates some kind of error occurred

Function Signature <return_type> <name> ( <argument_list> ) <return_type> - type of function s return value <name> - Your choice (starting with letter) <argument list> - comma separated list of arguments each argument consists of <type> <argname> int main() int addemup(int a, int b) void print_error_message(int error_type)

Function Declaration vs. Definition Function Declaration <function signature>; Function Definition <function signature> { <function statements> }

Function Statements Variable Declaration: <type> <name> = <initval> Assignment: <lhs> = <expression> Expressions Return: return <expression> Control Statements

LC3 vs. C - Variables LC3 Labeled Words COUNT.fill #3 C Variable Declarations int count=3; everything is 16 bits required initial value program decides type size depends on type optional initial value type must be specified

Variable Declarations <type> <name> =<initial value>; <type> - one of the built-in or derived type names <name> - your choice starts with letter, no embedded blanks =<initial value> is optional int count=17; float area=2.89; int vec[10]; int *vec_ptr;

Function Statements - Assignment <left_hand_side> = <expression>; <left_hand_side> or <LHS> - Something that can be assigned to <expression> - Something that can be evaluated Expression is evaluated, and the result is copied to the <LHS> count = 12; first_initial= T ; area = height * width; take_off_time=(now + delta)%24; distance = sqroot(x*x + y*y);

C Expressions - Constants <constant> Decimal Integers: <digits> e.g. 0, 123, 82, Octal Integers: 0<digits> e.g. 07612 Hexadecimal Integers: 0x<digits> e.g. 0xFA3B, 0x00a1 Floating Point numbers: <digits>.<digits>[e<digits>] e.g. 1.0, 3.141, 6.23e23, Single characters: <char> e.g. a, 3 List of characters: <chars> e.g. this is a test.

C Expressions: Variables <variable_name> The value of any variable argument local variable global variable

C Expressions: Unary Operators <UnaryOp> <expression> Operators with a single argument Invert: ~,! Sign: +, - Increment/Decrement: ++, -- Side effects Prefix vs. Postfix Address/Pointer: @, * Casting : (<type>) ~flags,!empty +12, -num count++, blocks-- ++count, count++ @vec /* like lea vec */, *num_ptr (int) first_initial

C Expressions: Parenthesis (<expression>) Evaluate what is inside parenthesis before using that value Remove ambiguity e.g. - - x vs. ( - x)

C Expressions: Binary Operators <expression> <BinOp> <expression> Arithmetic: +, -, *, /, %, ** a+b, time%24 Relational: <, <=, ==, >=, >,!= WARNING: = is assignment!!!!! Logical: &,, ^, &&, a>b, count <= max (a>b)&&(a>c) Shift: <<, >> a<<2 /* fast a*4 */

C Expressions: Ternary Operator <Condition>?<T_expression>:<F_expression> Evaluates <Condition> If <Condition> is true, returns <T_expression> If <Condition> is false, returns <F_expression> (x>y)? is greater than : is less than or equal to

C Truth Value In C, any value OTHER THAN zero is Logically True In C, a value of zero is Logically False (blocks_left)? There are blocks left : There are no blocks left Question: What is blocks_left==-7?

C Expression: Function Invocation <function_name>(<function_arguments>) Invokes function with arguments specified arguments may be epxressions evaluated before function invoked Function s return value becomes the value of the expression

C blocks List of statements surrounded by curly braces {} A block is treated as a single statement For example, a function definition is a function signature followed by a C statement that defines what that function does. However, the C statement is almost always replaced by a block of statements Modern C compilers require a block of statements for a function definition

C Control Statements If/Then/Else if (<condition>) <then-statement>; if (<condition>) <then-statement>; else <else-statement>; Evaluate condition far enough to determine truth value if the condition is true, execute the <then-statement> if the condition is false, execute the <else-statement> (if present) if (a>0) a--; else a++;

C Control Statements While loop while <condition> <loop_statement>; Evaluate condition If condition is true, execute <loop_statement>, then re-execute loop. while(i>0) sum+=a[i--];

C control Statements Do / While loop do <loop-statement>; while ( <condition> ); Execute loop statement then test <condition> if true, reexecute loop statement do resetarray(i++); while(i<array_size);

C Control Statements For loops for(<init>;<condition>;<increment>) <loop_statement>; Execute <init> statement Test <condition> if true, execute <loop_statement>, then execute <increment> statement, then test condition again for(i=0; i<max; i++) array[i]=0;

C Function Statements: return return <expression> ; Evaluates <expression> Returns the value of the expression to the caller

Example C code int bignuff(int n) { if (n>10) return 1; return 0; } int add3(int n) { n=n+3; return n; } int main() { int x=5; return (bignuff(add3(add3(x)))?0:x; }