Programming in C UVic SEng 265

Similar documents
Programming in C. What is C?... What is C?

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

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

Parameter passing. Programming in C. Important. Parameter passing... C implements call-by-value parameter passing. UVic SEng 265

COMP 2355 Introduction to Systems Programming

C Language, Token, Keywords, Constant, variable

BLM2031 Structured Programming. Zeyneb KURT

Basic Elements of C. Staff Incharge: S.Sasirekha

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

In this session we will cover the following sub-topics: 1.Identifiers 2.Variables 3.Keywords 4.Statements 5.Comments 6.Whitespaces 7.Syntax 8.

Presented By : Gaurav Juneja

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

Programming in C and C++

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

Syntax and Variables

Procedures, Parameters, Values and Variables. Steven R. Bagley

EL2310 Scientific Programming

12 CREATING NEW TYPES

Computer Science & Information Technology (CS) Rank under AIR 100. Examination Oriented Theory, Practice Set Key concepts, Analysis & Summary

Ricardo Rocha. Department of Computer Science Faculty of Sciences University of Porto

C LANGUAGE AND ITS DIFFERENT TYPES OF FUNCTIONS

Laboratory 2: Programming Basics and Variables. Lecture notes: 1. A quick review of hello_comment.c 2. Some useful information

A Fast Review of C Essentials Part I

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

Array Initialization

EL2310 Scientific Programming

Problem Solving and 'C' Programming

Two s Complement Review. Two s Complement Review. Agenda. Agenda 6/21/2011

An overview of Java, Data types and variables

Outline. Lecture 1 C primer What we will cover. If-statements and blocks in Python and C. Operators in Python and C

Topic 6: A Quick Intro To C

Review of the C Programming Language for Principles of Operating Systems

CS 326 Operating Systems C Programming. Greg Benson Department of Computer Science University of San Francisco

CSE 303 Lecture 8. Intro to C programming

PROGRAMMAZIONE I A.A. 2017/2018

CMPE-013/L. Introduction to C Programming

EMBEDDED SYSTEMS PROGRAMMING Language Basics

B.V. Patel Institute of Business Management, Computer & Information Technology, Uka Tarsadia University

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

Topic 6: A Quick Intro To C. Reading. "goto Considered Harmful" History

Introduction to C Programming

!"#$% &'($) *+!$ 0!'" 0+'&"$.&0-2$ 10.+3&2),&/3+, %&&/3+, C,-"!.&/+"*0.&('1 :2 %*10% *%7)/ 30'&. 0% /4%./

Course organization. Part I: Introduction to C programming language (Week 1-12) Chapter 1: Overall Introduction (Week 1-4)

Lecture 3: C Programm

CS240: Programming in C

EC 413 Computer Organization

CS 61c: Great Ideas in Computer Architecture

CS61C : Machine Structures

Work relative to other classes

PRINCIPLES OF OPERATING SYSTEMS

by: Lizawati, Norhidayah & Muhammad Noorazlan Shah Computer Engineering, FKEKK, UTeM

CSE 303: Concepts and Tools for Software Development

ELEC 377 C Programming Tutorial. ELEC Operating Systems

Tokens, Expressions and Control Structures

Introduction to Computing Lecture 01: Introduction to C

CS133 C Programming. Instructor: Jialiang Lu Office: Information Center 703

Programming. Data Structure

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

UNIT-IV. Structure is a user-defined data type in C language which allows us to combine data of different types together.

C++: Const Function Overloading Constructors and Destructors Enumerations Assertions

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

Programming Language Basics

INTRODUCTION 1 AND REVIEW

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

Input And Output of C++

6.096 Introduction to C++ January (IAP) 2009

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

Kurt Schmidt. October 30, 2018

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

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

from Appendix B: Some C Essentials

CMPT 115. C tutorial for students who took 111 in Java. University of Saskatchewan. Mark G. Eramian, Ian McQuillan CMPT 115 1/32

Programming. Structures, enums and unions

C-Programming. CSC209: Software Tools and Systems Programming. Paul Vrbik. University of Toronto Mississauga

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

CS61C Machine Structures. Lecture 3 Introduction to the C Programming Language. 1/23/2006 John Wawrzynek. www-inst.eecs.berkeley.

C Program Development and Debugging under Unix SEEM 3460

Introduction to C. Robert Escriva. Cornell CS 4411, August 30, Geared toward programmers

C# Fundamentals. Hans-Wolfgang Loidl School of Mathematical and Computer Sciences, Heriot-Watt University, Edinburgh

CS201 - Lecture 1 The C Programming Language

ARRAYS(II Unit Part II)

Introduction to C. CS2023 Winter 2004

Introduction. Primitive Data Types: Integer. Primitive Data Types. ICOM 4036 Programming Languages

C Programming Review CSC 4320/6320

are all acceptable. With the right compiler flags, Java/C++ style comments are also acceptable.

XSEDE Scholars Program Introduction to C Programming. John Lockman III June 7 th, 2012

For each of the following variables named x, specify whether they are static, stack-dynamic, or heapdynamic:

Review of the C Programming Language

Preview from Notesale.co.uk Page 6 of 52

G52CPP C++ Programming Lecture 3. Dr Jason Atkin

edunepal_info

EL2310 Scientific Programming

gcc hello.c a.out Hello, world gcc -o hello hello.c hello Hello, world

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

Pace University. Fundamental Concepts of CS121 1

Programming refresher and intro to C programming

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

Programming and Data Structure

Fall, $ cat welcome.c #include <stdio.h>

Transcription:

Programming in C UVic SEng 265 Daniel M. German Department of Computer Science University of Victoria 1 SEng 265 dmgerman@uvic.ca

C Developed by Brian Kernighan and Dennis Ritchie of Bell Labs Earlier, in 1969, Ritchie and Thompson developed the Unix operating system We will be focusing on a version referred to as ANSI C. 2 SEng 265 dmgerman@uvic.ca

What is C? General Purpose programming language Simple language to compile Simple constructions, very close to the hardware level Low overhead of execution no run-time checking no array access bounds-checks no null-pointer checks no checks on uninitialized variables 3 SEng 265 dmgerman@uvic.ca

What is C?... Very terse programming language Keywords are often optional Limited native functionality; no built-in libraries or utilities beyond the C library (no GUI, no database, no complex datatypes, mathematical functions, etc.) Nowadays almost any architecture has a C compiler 4 SEng 265 dmgerman@uvic.ca

Hello, World! hello.c #include <stdio.h> int main(int argc, char *argv[]) { printf("hello, World!\n"); return 0; % gcc -pedantic -Wall -ansi -c hello.c # produces hello.o % gcc -o hello hello.c # produces hello %./hello Hello, World! 5 SEng 265 dmgerman@uvic.ca

Basic syntax similar to Java (Java is a descendant of C++, which is descended from C) C Java comments /*... */ //, /*... */ basic types char (byte) char (16-bit Unicode) int (natural) short int (smaller or equal to int) int (4) long int (bigger or equal to int) short (2) float (4 byte IEEE) long (8) double (8 byte IEEE) aggregate types array Vector struct byte (1-byte signed) float (4 byte IEEE) double (8 byte IEEE) class 6 SEng 265 dmgerman@uvic.ca

Important details Ground rules: All C programs must have a function called main keywords are always lowercase statements must be terminated with a semicolon Defining variables general syntax type name; declaration with initialization: type name = value; 7 SEng 265 dmgerman@uvic.ca

Programming style Any amount of white space is a considered a single space End of lines, tabs and spaces can be liberally used More white space can help improve code readability Commenting is extremely important for maintaining code Use indentation in conjunction with curly braces (f, g) to indicate different levels of nested functions. K&R is the style for this course: Opening brace of function in column 1 Opening brace in the same line as expression (if, while, for) The block inside the braces should be indented End brace at same level of opening statement 8 SEng 265 dmgerman@uvic.ca

Program Style... Indentation == 3 spaces No lines beyond 80 characters! #include <stdio.h> int get_line(char inputstring[], int maxsize) { int c, i; for (i=0; i<maxsize-1 && (c=getchar())!=eof && c== \n ; ++i) inputstring[i] = c; if (c == \n ) { inputstring[i] = c; ++i; inputstring[i] = \0 ; return i; 9 SEng 265 dmgerman@uvic.ca

Type declarations Examples: int y; /* declaration of integer y */ int x = 4; /* declaration with init. */ int a, b, c; /* decls. of ints a, b and c */ int a, b = 1; /* decl of a, and b, initialize b */ char c = A ; /* not the same as a */ 10 SEng 265 dmgerman@uvic.ca

Signed vs. unsigned integers On aserver, a 4-byte signed integer may represent the values from -2,147,483,648 (MIN INT) to 2,147,483,647 (MAX INT) Signed numbers must reserve one bit to represent sign (complement 2 representation) 4-bytes == 32 bits (8-bit bytes); with 1 bit left over, maximum value 2 is 1; minimum value is 2 31 31 An unsigned 4-byte integer can only be positive and has a range from 0 to 4,294,967,295 (MAX UINT) By default a variable is signed Unsigned declarations: unsigned char c; unsigned int i; unsigned long l; 11 SEng 265 dmgerman@uvic.ca

Arrays Elements are indexed, starting from 0 and going up to (size-1) int num[10]; num[0] = 4510; num[1] = -3312; num[9] = 214; num[10] = 31441; /* non existent */ format for one-dimensional array declarations: <type> <var name>[<size>] Example: float char vector[3]; buffer[256]; <size> must be known at compile time 12 SEng 265 dmgerman@uvic.ca

Control flow Four basic constructs: if-then, if-then-else (branching) switch (multi-way branching) while loops, do-while loops for loops meaning of these constructs is very similar to that in Java however, major difference is in the meaning of branch- and loopconditions we will not use goto or setjmp/longjmp in this course (but they are useful, so don t hate them!) 13 SEng 265 dmgerman@uvic.ca

if statement if (condition) { stmt1; stmt2; else if (condition2) { stmt3; stmt4; else { stmt5; 14 SEng 265 dmgerman@uvic.ca

Multi-way branch (switch) switch (condition) { case value1: stmt1; break; case value2: stmt2; break; default: stmt3; break; 15 SEng 265 dmgerman@uvic.ca

While loops Two varieties. Condition before the loop while (condition) { stmt1; stmt2; Condition after the loop do { stmt1; stmt2; while (condition); 16 SEng 265 dmgerman@uvic.ca

for loops for (expr1; condition; expr2) { stmt1; stmt2; This is equivalent to: expr1; while (condition) { stmt1; stmt2; expr2; 17 SEng 265 dmgerman@uvic.ca

Conditions C does not have a boolean type However, there are boolean like operators &&,, ==,!=,!, >, <, >=, <= (same as Java) Any expression that evaluates to non-zero is considered true int x = 5; if (x) { /* trivially true */ 18 SEng 265 dmgerman@uvic.ca

Careful! The assignment operator (=) and equality comparison operator (==) have different meanings Be careful not to confuse the two Legal (but possibly undesired) C code: int a = 20; if (a = 5) { /* This expression is ALWAYS true */ stmt1; /* It should be a == 5 */ 19 SEng 265 dmgerman@uvic.ca

Functions general syntax: <return type> name (<parameters>) { <statements> parameter syntax: <type> varname [, <type> varname>] 20 SEng 265 dmgerman@uvic.ca

Functions... example: int main(int argc, char *argv[]) { printf("hello, world!\n"); return 0; example: float max(float x, float y) { if (x > y) return x; else return y; 21 SEng 265 dmgerman@uvic.ca

Command line arguments passed into the main() function as parameters example: read a person s name from the command line argc number of parameters + 1 argv contains executable name and each parameter #include <stdio.h> int main(int argc, char *argv[]) { char *name; if (argc >= 2) { name = argv[1]; else { name = "anonymous"; printf("hello %s!\n", name); return(0); 22 SEng 265 dmgerman@uvic.ca

Command line arguments argc is the count of argv elements (which includes the name of the executable) argc must at least be 1 argv is declared as an array of unknown length (note the absence of a number within the square brackets) argv[] are the command-line parameters char * is a pointer to some characters. More about pointers later. 23 SEng 265 dmgerman@uvic.ca

Scope of a Variable Variables have a scope (area in which they are visible/available) In general: visible within the block in which they are defined int compute(int a) { int x = 5; int y = 5; if (a == y) { int z; z = y + 3; x = z; /* compile-time error */ return x; 24 SEng 265 dmgerman@uvic.ca

Scope of a Variable... Global variables, by default, are only available for only the file in which they are defined int count = 0; int main(void) { count = 5; printf("%i", count); 25 SEng 265 dmgerman@uvic.ca

Scope of a Variable... If a variable is used in a file, but is defined in another file, you must declare it before using it use extern example: extern int count; void increment(void) { count++; 26 SEng 265 dmgerman@uvic.ca

Function prototypes The code of a function is its definition A definition is a declaration A declaration tells the compiler the type of a function, and the type and number of parameters A function should be declared before it is used If a function name is used, but it has not been declared, the compiler assumes its type and types of its parameters Many problems arise if this assumption is wrong 27 SEng 265 dmgerman@uvic.ca

Function prototypes (contd) The declaration is known as the prototype of a function general syntax: <return type> name(<parameters>); parameters: types are necessary, but names are optional; names are recommended (improves code readability) examples: int sort(int a[]); float max3(float, float, float); void error_message(char *m); 28 SEng 265 dmgerman@uvic.ca

Function prototypes... Prototypes are declarations In general, same as a normal function except there is no function body (i.e., absence of curly brackets) and is immediately terminated with a semi-colon Prototypes come at the top of file before any function is formally declared #include <stdio.h> is used to refer to all function prototypes for standard i/o (i.e., for printf, fprintf, fread, etc.) Prototypes help the compiler find programmer errors 29 SEng 265 dmgerman@uvic.ca

Structures In some languages, referred to as records Multiple variables inside a single structure (an aggregate ) Structure itself becomes a data type Can be thought of as an ancestor to objects struct date { int month; int day; int year; ; 30 SEng 265 dmgerman@uvic.ca

Using structures Once a structure variable is declared, variables inside the structure can be accessed using <variable>.<field> syntax: struct date today; today.day = 31; today.month = 5; today.year = 2001 arrays of struct s can also be defined 31 SEng 265 dmgerman@uvic.ca

Other C data types Enumerations: A translation table can be established between integers and words Using enum: enum d_of_week { sun, mon, tue, wed, thur, fri, sat ; enum d_of_week today = mon; /* today = 1 */ Note: the elements of enumerated data type are not strings, they are integers 32 SEng 265 dmgerman@uvic.ca

Other data types... Unions Very similar to structs, but all its members share same memory Used to join two or more types together union my_union { char c; int i; ; union my_union u; u.c = A ; u.i = 80; printf("hello world [%d][%c]\n", u.i, u.c); Hello world [80][P] 33 SEng 265 dmgerman@uvic.ca