A Shotgun Introduction to C

Similar documents
A Shotgun Introduction to C

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

The C Language Reference Manual

Arrays and Pointers (part 1)

Pointers (part 1) What are pointers? EECS We have seen pointers before. scanf( %f, &inches );! 25 September 2017

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

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

Arrays and Pointers (part 1)

SYSC 2006 C Winter 2012

Chapter 12 Variables and Operators

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

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

DEPARTMENT OF MATHS, MJ COLLEGE

Presented By : Gaurav Juneja

Tutorial-2a: First steps with C++ programming

... Lecture 12. Pointers

Chapter 12 Variables and Operators

Preview from Notesale.co.uk Page 6 of 52

Chapter 12 Variables and Operators

Arrays and Pointers. CSE 2031 Fall November 11, 2013

Course Outline Introduction to C-Programming

Chapter 1 & 2 Introduction to C Language

ME 461 C review Session Fall 2009 S. Keres

UNIT- 3 Introduction to C++

Arrays and Pointers. Arrays. Arrays: Example. Arrays: Definition and Access. Arrays Stored in Memory. Initialization. EECS 2031 Fall 2014.

Lecture 3. More About C

2/29/2016. Definition: Computer Program. A simple model of the computer. Example: Computer Program. Data types, variables, constants

Expressions and Data Types CSC 121 Fall 2015 Howard Rosenthal

Chapter 12 Variables and Operators

Objectives. Introduce the core C# language features class Main types variables basic input and output operators arrays control constructs comments

Recap. ANSI C Reserved Words C++ Multimedia Programming Lecture 2. Erwin M. Bakker Joachim Rijsdam

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

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

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

Introduction to C Language

Expressions and Data Types CSC 121 Spring 2015 Howard Rosenthal

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

COMP322 - Introduction to C++ Lecture 02 - Basics of C++

BITG 1233: Introduction to C++

Chapter 2: Introduction to C++

CGS 3066: Spring 2015 JavaScript Reference

Lecture 5: C programming

More on C programming

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

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

Chapter 2: Special Characters. Parts of a C++ Program. Introduction to C++ Displays output on the computer screen

Chapter 1 Introduction to Computers and C++ Programming

Basic memory model Using functions Writing functions. Basics Prototypes Parameters Return types Functions and memory Names and namespaces

Intermediate Programming, Spring 2017*

Creating a C++ Program

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

Syntax and Variables

Computers Programming Course 11. Iulian Năstac

Data types, variables, constants

C: How to Program. Week /Mar/05

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

Under the Hood: Data Representations, Memory and Bit Operations. Computer Science 104 Lecture 3

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. By Avani M. Sakhapara Asst Professor, IT Dept, KJSCE

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

Lecture 8: Pointer Arithmetic (review) Endianness Functions and pointers

Programming in C Quick Start! Biostatistics 615 Lecture 4

To declare an array in C, a programmer specifies the type of the elements and the number of elements required by an array as follows

COMP322 - Introduction to C++ Lecture 01 - Introduction

Objectives. Chapter 2: Basic Elements of C++ Introduction. Objectives (cont d.) A C++ Program (cont d.) A C++ Program

Chapter 2: Basic Elements of C++

Chapter 2: Basic Elements of C++ Objectives. Objectives (cont d.) A C++ Program. Introduction

Variables and literals

Lecture 4: Outline. Arrays. I. Pointers II. III. Pointer arithmetic IV. Strings

Guide for The C Programming Language Chapter 1. Q1. Explain the structure of a C program Answer: Structure of the C program is shown below:

Understanding Pointers

7/8/10 KEY CONCEPTS. Problem COMP 10 EXPLORING COMPUTER SCIENCE. Algorithm. Lecture 2 Variables, Types, and Programs. Program PROBLEM SOLVING

BASIC ELEMENTS OF A COMPUTER PROGRAM

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

Multidimension array, array of strings

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

Quick Reference Guide

C - Basic Introduction

CHAPTER 3 BASIC INSTRUCTION OF C++

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

BIL 104E Introduction to Scientific and Engineering Computing. Lecture 1

Storage class and Scope:

Pointers. Pointers. Pointers (cont) CS 217

Memory, Arrays & Pointers

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

Information Science 1

C++ Basics. Data Processing Course, I. Hrivnacova, IPN Orsay

3. Java - Language Constructs I

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

C: Pointers. C: Pointers. Department of Computer Science College of Engineering Boise State University. September 11, /21

A complex expression to evaluate we need to reduce it to a series of simple expressions. E.g * 7 =>2+ 35 => 37. E.g.

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

Fall 2018 Discussion 2: September 3, 2018

C Syntax Out: 15 September, 1995

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

EL2310 Scientific Programming

EL2310 Scientific Programming

2.1. Chapter 2: Parts of a C++ Program. Parts of a C++ Program. Introduction to C++ Parts of a C++ Program

GOLD Language Reference Manual

Transcription:

A Shotgun Introduction to C Stephen A. Edwards Columbia University Fall 2011

C History Developed between 1969 and 1973 along with Unix Due mostly to Dennis Ritchie Designed for systems programming Operating systems Utility programs Compilers Filters Evolved from B, which evolved from BCPL

C History Original machine, a DEC PDP-11, was very small: 24K bytes of memory, 12K used for operating system Written when computers were big, capital equipment Group would get one, develop new language, OS

Adding Two Numbers int add() /* Function that returns an integer */ { int x, y, z; /* Variables x, y, and z are integers */ x = 38; /* Set x to 38 */ y = 4; /* Set y to 4 */ z = x + y; /* Set z to the sum of x and y */ return z; /* Return z as the result of add() */ End statements with semicolons Text between /* and */ is ignored (a comment) Programs are mostly function definitions and global variables.

Variables Names must start with a letter; may contain letters, numbers, and underscores. a A a_variable avariable a50 ex 12_ /* OK */ two-words 42_is_the_answer /* BAD */ Must be declared before they re used int a, b, c; /* 32-bit signed binary integers */ char c, d; /* Single letter, digit, etc. */ a = 42; b = 18; f = 3; /* BAD: f not declared */ c = o ; d = # ; q = 4 ; /* BAD: q not declared */

Types of Integers int a; /* 32 bits: -2147483648 to 2147483647 */ unsigned b; /* 32 bits: 0 to 4294967295 */ short c; /* 16 bits: -32768 to 32767 */ unsigned short d; /* 16 bits: 0 to 65535 */ signed char e; /* 8 bits: -128 to 127 */ unsigned char f; /* 8 bits: 0 to 255 */

Constants #define ROWS 10 #define COLUMNS 40 pos = y * COLUMNS + rows; This turns into pos = y * 40 + rows; The # must be in the leftmost column.

Common Operators int a, b, c; a = b + c; /* Addition */ a = b - c; /* Subtraction */ a = -(b + c); /* Negation */ a = b * c; /* Multiplication */ a = b / c; /* Division (integer result) */ a = b < c; /* a is non-zero if b is less than c */ a = b > c; /* non-zero if b is greater than c */ a = b <= c; /* b less than or equal to c */ a = b >= c; /* b greater than or equal to c */ a = b == c; /* a is non-zero if b is equal to c */ a = b!= c; /* a is non-zero if b different than c */

The If-Else Statement if (a == 3) c = 2; /* Runs if a is 3. One statement: braces optional */ if (b == 4 && c == 2) { /* && is logical AND */ c = 5; /* Runs if b is 4 and c is 2 */ a = a + 3; /* Two statements: braces mandatory */ if (a > b) { c = 1; /* Runs if a is greater than b */ else { c = 5; /* Runs if a is not greater than b */ if (a > b c == 3) { /* is logical OR */ c = 5; /* Runs if a is greater than b or c is 3 */ else { a = b + 2;

The Switch Statement: A Multiway Conditional switch (a + 1) { case 2: c = 8; /* Runs if a is 1 */ b = 2; break; case 0: case 1: /* Multiple cases allowed */ b = 3; /* Runs if a is -1 or 0 */ break; case 42: /* Case labels need not be contiguous */ c = 12; /* No break: falls through to next case! */ case 4: c = 15; /* Runs if a is 3 or 41 */ break; default: /* a default is optional */ c = 0; /* Runs if no other case matches */ break; /* Good style */

Assignment Operators A convenient shorthand: a += 3; /* Increase a by 3 */ a = a + 3; /* Equivalent */ b *= 2; /* Double b */ b = b * 2; /* Equivalent */ Most operators have assignment variants.

Bitwise Operators Internally, numbers represented in binary. 10100101 2 = 1 2 7 + 0 2 6 + 1 2 5 + 0 2 4 + 0 2 3 + 1 2 2 + 0 2 1 + 1 2 0 = 128 + 32 + 4 + 1 = 165 10 Bitwise operators work directly on bits: AND: 10110 & 10011 10010 OR: 10110 10011 10111 XOR: 10110 ^ 10011 00101

Arrays int i; int a[10]; /* Array of 10 integers */ int b[] = { 2,3,7,6 ; /* Initial values */ a[0] = 3; a[2] = 5; a[9] = 18; a[10] = 42; /* BAD: only a[0]... a[9] */ a[-1] = 2; /* BAD: positive indexes only */ a[1] = b[0]; /* a[0] = 2 */ a[3] = b[1]; /* a[3] = 3 */ b[3] = 42; b[4] = 18; /* BAD: only b[0].. b[3] */ i = 5; a[i] = 42; /* a[5] = 42; */ i = 4; a[i] = 10; /* a[4] = 10; */

Strings /* Strings are null-terminated arrays of characters */ char name1[] = "Stephen"; /* is equivalent to */ char name2[] = { S, t, e, p, h, e, n, 0; name1[5] = a ; /* name1 now "Stephan" */

The While Statement int gcd(int a, int b) { while (a!= b) { /* Repeat while a and b are different */ if (a > b) a -= b; /* a is larger; subtract b from it */ else b -= a; /* b is larger; subtract a from it */ return a;

The For Statement /* Sum the numbers from 1 to n */ int sumup(int n) { int i, s; i = 0; s = 0; while (i <= n) { s += i; i += 1; return s; /* Sum the numbers from 1 to n */ int sumup(int n) { int i, s; s = 0; for (i = 0 ; i <= n ; i += 1) s += i; return s;

Increment/Decrement Operators a = a + 1; /* Common operation */ a += 1; /* One shorthand */ a++; /* Even more succinct */ for (i = 0 ; i < 10 ; i++) { /* Very common idiom */ /* i = 0, 1, 2,..., 9 */ a = 3; b = a++; /* Postincrement: means b = 3; a = 4; */ b = ++a; /* Preincrement: means a = 5; b = 5; */ a = 3; b = a--; /* Postdecrement: means b = 3; a = 2; */ b = --a; /* Predecrement: means a = 1; b = 1; */

Functions int num_calls = 0; /* global variable */ int power(int base, int n) { int p; /* Different than main s p */ for ( p = 1 ; n > 0 ; --n ) p *= base; num_calls++; return p; int main() /* main function always runs first */ { int n, p; n = power(2, 5); /* n = 32 */ p = power(3, 3); /* p = 27 */ p = num_calls; /* p = 2 */

Pointers void swap(int x, int y) { int temp; temp = x; x = y; y = temp; Does this work?

Pointers void swap(int x, int y) { int temp; temp = x; x = y; y = temp; Does this work? Nope. void swap(int *px, int *py) { int temp; temp = *px; /* get data at px */ *px = *py; /* get data at py */ *py = temp; /* write data at py */ void main() { int a = 1, b = 2; /* Pass addresses of a and b */ swap(&a, &b); /* a = 2 and b = 1 */

Arrays and Pointers a: a[0] a[1] a[9] int a[10];

Arrays and Pointers pa: a: a[0] a[1] a[9] int a[10]; int *pa = &a[0];

Arrays and Pointers pa: a: a[0] a[1] a[9] int a[10]; int *pa = &a[0]; pa = pa + 1;

Arrays and Pointers pa: a: a[0] a[1] a[9] int a[10]; int *pa = &a[0]; pa = pa + 1; pa = &a[1];

Strlen: An Example s S t e p h e n \0 int strlen(const char *s) { int n; for (n = 0 ; *s!= \0 ; s++) n++; return n; void main() { char ste[] = "Stephen"; int l = strlen(ste);

Separate Compilation file1.c extern void bar(); char a[] = "Hello"; int main() { bar(); void baz(char *s) { printf("%\%%s", s); file2.c extern char a[]; extern void baz(char *); static char b[6]; void bar() { strcpy(b, a); baz(b);

Better Style: Header Files myfiles.h #ifndef _MYFILES_H #define _MYFILES_H /* in file1.c */ extern void bar(); extern char a[]; /* in file2.c */ extern void baz(char *); #endif file1.c #include "myfiles.h" char a[] = "Hello"; int main() { bar(); void baz(char *s) { printf("%\%%s", s); file2.c #include "myfiles.h" static char b[6]; void bar() { strcpy(b, a); baz(b);