CSCI 2132: Software Development. Norbert Zeh. Faculty of Computer Science Dalhousie University. Introduction to C. Winter 2019

Similar documents
CSCI 2132 Software Development. Lecture 8: Introduction to C

27-Sep CSCI 2132 Software Development Lecture 10: Formatted Input and Output. Faculty of Computer Science, Dalhousie University. Lecture 10 p.

C Fundamentals & Formatted Input/Output. adopted from KNK C Programming : A Modern Approach

EL2310 Scientific Programming

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

Basic Types and Formatted I/O

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

A Fast Review of C Essentials Part I

Work relative to other classes

CS113: Lecture 3. Topics: Variables. Data types. Arithmetic and Bitwise Operators. Order of Evaluation

Chapter 2. Lexical Elements & Operators

Unit 1: Introduction to C Language. Saurabh Khatri Lecturer Department of Computer Technology VIT, Pune

Differentiate Between Keywords and Identifiers

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

Fundamental Data Types. CSE 130: Introduction to Programming in C Stony Brook University

COP 3275: Chapter 02. Jonathan C.L. Liu, Ph.D. CISE Department University of Florida, USA

Basic C Programming. Bin Li Assistant Professor Dept. of Electrical, Computer and Biomedical Engineering University of Rhode Island

Overview. - General Data Types - Categories of Words. - Define Before Use. - The Three S s. - End of Statement - My First Program

Programming for Engineers Introduction to C

Computers Programming Course 5. Iulian Năstac

C: How to Program. Week /Mar/05

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

CSC 1107: Structured Programming

C++ Basics. Lecture 2 COP 3014 Spring January 8, 2018

EL2310 Scientific Programming

CSE 303 Lecture 8. Intro to C programming

Intro to Computer Programming (ICP) Rab Nawaz Jadoon

EL2310 Scientific Programming

Chapter 2 - Introduction to C Programming

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

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

C Programming

c) Comments do not cause any machine language object code to be generated. d) Lengthy comments can cause poor execution-time performance.

Fundamental of C programming. - Ompal Singh

Fundamentals of Programming Session 4

COMP s1 Lecture 1

Number Systems, Scalar Types, and Input and Output

AN OVERVIEW OF C. CSE 130: Introduction to Programming in C Stony Brook University

CC112 Structured Programming

These are reserved words of the C language. For example int, float, if, else, for, while etc.

Fundamentals of Programming. Lecture 3: Introduction to C Programming

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

Lecture 2: C Programming Basic

Introduction to C. Systems Programming Concepts

CS 61C: Great Ideas in Computer Architecture Introduction to C

UNIT IV 2 MARKS. ( Word to PDF Converter - Unregistered ) FUNDAMENTALS OF COMPUTING & COMPUTER PROGRAMMING

Room 3P16 Telephone: extension ~irjohnson/uqc146s1.html

Course Outline Introduction to C-Programming

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

Introduction to C Programming. Chih-Wei Tang ( 唐之瑋 ) Department of Communication Engineering National Central University JhongLi, Taiwan

AN OVERVIEW OF C, PART 3. CSE 130: Introduction to Programming in C Stony Brook University

Physics 2660: Fundamentals of Scientific Computing. Lecture 3 Instructor: Prof. Chris Neu

C Language, Token, Keywords, Constant, variable

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

Basics of Java Programming

Introduction to C Language

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

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

Programming Language Basics

Typescript on LLVM Language Reference Manual

EC 413 Computer Organization

The C language. Introductory course #1

BLM2031 Structured Programming. Zeyneb KURT

CMPT 102 Introduction to Scientific Computer Programming. Input and Output. Your first program

File Handling in C. EECS 2031 Fall October 27, 2014

C Introduction. Comparison w/ Java, Memory Model, and Pointers

CS 220: Introduction to Parallel Computing. Beginning C. Lecture 2

C Language Summary. Chris J Michael 28 August CSC 4103 Operating Systems Fall 2008 Lecture 2 C Summary

ET156 Introduction to C Programming

Should you know scanf and printf?

9/5/2018. Overview. The C Programming Language. Transitioning to C from Python. Why C? Hello, world! Programming in C

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

CS 261 Fall C Introduction. Variables, Memory Model, Pointers, and Debugging. Mike Lam, Professor

BBM 101 Introduc/on to Programming I Fall 2014, Lecture 3. Aykut Erdem, Erkut Erdem, Fuat Akal

Topic 6: A Quick Intro To C

Chapter 1 & 2 Introduction to C Language

Full file at C How to Program, 6/e Multiple Choice Test Bank

The C Programming Language. (with material from Dr. Bin Ren, William & Mary Computer Science)

Lecture 2 Tao Wang 1

Chapter 2, Part I Introduction to C Programming

SEQUENTIAL STRUCTURE. Erkut ERDEM Hacettepe University October 2010

Programming. Data Structure

VARIABLES AND CONSTANTS

Lexical Considerations

2. Numbers In, Numbers Out

Week 2 C Fundamentals; Formatted Input/Output; int and float Types

Computer System and programming in C

Lexical Structure (Chapter 3, JLS)

Reserved Words and Identifiers


CPE 101, reusing/mod slides from a UW course (used by permission) Lecture 5: Input and Output (I/O)

Presented By : Gaurav Juneja

Visual C# Instructor s Manual Table of Contents

Fundamental of Programming (C)

Lecture 3. More About C

.. Cal Poly CPE 101: Fundamentals of Computer Science I Alexander Dekhtyar..

Introduction to Computing Lecture 03: Basic input / output operations

F28HS2 Hardware-Software Interface. Lecture 1: Programming in C 1

4. Inputting data or messages to a function is called passing data to the function.

BSM540 Basics of C Language

Transcription:

CSCI 2132: Software Development Introduction to C Norbert Zeh Faculty of Computer Science Dalhousie University Winter 2019

The C Programming Language Originally invented for writing OS and other system software Inventor: Dennis Ritchie Characteristics: Optimized for speed and programming close to machine Manual memory management, no garbage collection 0-overhead rule: What you don t write doesn t happen No safety checks: E.g.: No out of bounds checks You need to know what you are doing

A Simple C Program hello.c #include <stdio.h> int main() { printf( Hello, world!\n ); return 0; }

Compiling a C Program $ gcc hello.c $ ls -l -rwx------ 1 nzeh staff 8432 7 Feb 23:02 a.out -rw------- 1 nzeh staff 80 7 Feb 23:02 hello.c $./a.out Hello, world! $ gcc -o hello hello.c $ ls -l -rwx------ 1 nzeh staff 8432 7 Feb 23:02 hello -rw------- 1 nzeh staff 80 7 Feb 23:02 hello.c $./hello Hello, world!

The Compilation Process Preprocessor: source1.c source2.c source3.c Modify source code E.g., expand macros Preprocessor Preprocessor Preprocessor Compiler: Modified source1.c Modified source2.c Modified source3.c Translate source into object code Compiler Compiler Compiler Linker: Combine one or more object files into executable code source1.o source2.o source3.o Linker Executable program

General Form of a Simple Program preprocessor directives int main() { statements No } arguments Return type = int #include <stdio.h> preprocessor directive main function int main() { printf( Hello, world!\n ); return 0; } statements

Functions = building blocks from which C programs are constructed Function = named group of statements (for now) Special kinds of functions: Main function = entry point of the program, called when the program is started Library functions = functions provided as part of the standard library Nesting of functions is not allowed.

Statements = commands to be executed Must end with a semicolon Examples: printf( Hello, world!\n ); return 0;

Printing Strings: printf printf can be used to print string literals String literal = sequence of characters and escape sequences enclosed by... Escape sequences (similar to Java): \n = newline \t = tab \xhh = character code in hexadecimal \ooo = character code in octal \\ = literal backslash \ = literal Others: \r, \a, \b, \f, \v, \, \?

printf Examples printf( Hello, world!\n ); printf( Hello, world! ); printf( Hello,\nworld!\n );

Comments /* This is a one-line comment */ /* This is a multi-line comment */ // C99 also allows this style of comments

Variables Variable = name for a memory location where data can be stored Variables in C are statically typed: Type declared as part of program text Only values of this type can be assigned to variable Type casting allows us to cheat (use with care, as a last resort) Contrast with Python (anything can be stored in a variable) Common types: int = integer char = character float = single-precision floating point number double = double-precision floating point number

Variable Declarations int number_of_lines; Type name (before variable name) Variable name (after type name) double length_in_inches; Before C99, declarations must precede statements in function code. C99 lifts this restriction. (It s still good practice.)

Operators A rich and powerful set of operators was one of the innovations in C Some operators (by decreasing precedence): Unary (2): +, -, ++, --,!, ~ Arithmetic (3): *, /, % Arithmetic (4): +, - Bitwise shifts (5): <<, >> Comparison (6): <, >, <=, >= Equality (7): ==,!= Bitwise operations: & (8), (10) Logical operations: && (11), (12) Assignment (14): = Update (14): +=, *=, >>=, <<=, &=,... Precedence can (of course) be overruled using parentheses.

Printing Variables: printf printf allows us to print also variables by including placeholders in the string Placeholders: %d = print an integer %f = print a single-precision float %lf = print a double-precision float %s = print a string %c = print a character %.2f = print single-precision float with 2 digits after decimal point

Printing Variables: printf printf( Height: %d\n, height); printf( %s: %.2f\n, Profit, profit);

Initializing Variables Variables may have random variables if not initialized. Declaration and initialization can happen in one step: int height = 8; double profit = 1030.56; float profit = 1030.56f; char c = A ; char c = \n ;

Reading Input: scanf Reading an int value: scanf( %d, &height); Reading a float value: scanf( %f, &a_float); Reading a double value: scanf( %lf, &a_double); Reading a char value: scanf( %c, &ch); The & is very important. You ll learn later why.

Defining Names for Constants Macro: #define NAME <some text> Preprocessor replaces every occurrence of NAME with <some text> NAME is not a variable! No checks whether the replacement of NAME with <some text> results in valid code. <some text> can be any sequence of tokens. Example: #define PI 3.14159

Defining Expressions as Macros The value of a macro can be an expression: #define RECIPROCAL_OF_PI (1.0 / 3.14159) Be generous with parentheses: What would happen without parentheses in this example? double pi = 1.0 / RECIPROCAL_OF_PI;

Identifiers = names for variables, functions, user-defined types, macros, etc. May contain letters, underscores, and digits Must start with a letter or underscore (For now, avoid using underscore as the first letter) Conventions: Functions, variables, types: transpose_matrix, vector,... Macros: PI, NUM_ROWS,...

A Simple Example Write a program to help your Walmart cashier: Inputs: Price of product before HST Payment made by customer Output: Change due to customer = payment - price * (1 + HST)

hst.c #include <stdio.h> #define HST 15 int main() { double price, payment, balance; printf( Enter price: ); scanf( %lf, &price); printf( Enter payment: ); scanf( %lf, &payment); } balance = payment - prince * (1.0 + HST / 100); printf( Change due: %.2lf\n, balance); return 0;