Princeton University COS 333: Advanced Programming Techniques A Subset of C90

Similar documents
Programming Languages: Part 2. Robert M. Dondero, Ph.D. Princeton University

Programming Languages Part 2. Copyright 2017 by Robert M. Dondero, Ph.D. Princeton University

Princeton University COS 333: Advanced Programming Techniques A Subset of PHP

P.G.TRB - COMPUTER SCIENCE. c) data processing language d) none of the above

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

Lecture 02 C FUNDAMENTALS

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

Review of the C Programming Language for Principles of Operating Systems

The Design of C: A Rational Reconstruction: Part 2

Fundamentals of Programming

Fundamental of Programming (C)

Continued from previous lecture

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

PRINCIPLES OF OPERATING SYSTEMS

Review of the C Programming Language

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

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

DEPARTMENT OF MATHS, MJ COLLEGE

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

2. C99 standard guarantees uniqueness of characters for internal names. A. 12 B. 26 C. 31 D. 48

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

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

Topic 6: A Quick Intro To C

CprE 288 Introduction to Embedded Systems Exam 1 Review. 1

Lectures 5-6: Introduction to C

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

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

C Syntax Out: 15 September, 1995

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

C Review. MaxMSP Developers Workshop Summer 2009 CNMAT

A flow chart is a graphical or symbolic representation of a process.

C Language Summary (Continued)


Work relative to other classes

C Programming Language (Chapter 2 of K&R) Variables and Constants

Princeton University Computer Science 217: Introduction to Programming Systems The Design of C

COMP 2355 Introduction to Systems Programming

Design and development of embedded systems for the Internet of Things (IoT) Fabio Angeletti Fabrizio Gattuso

Tutorial 1: Introduction to C Computer Architecture and Systems Programming ( )

Lectures 5-6: Introduction to C

EL6483: Brief Overview of C Programming Language

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

IMPORTANT QUESTIONS IN C FOR THE INTERVIEW

C LANGUAGE A Short Course

C Overview Fall 2014 Jinkyu Jeong

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

Writing an ANSI C Program Getting Ready to Program A First Program Variables, Expressions, and Assignments Initialization The Use of #define and

Contents. A Review of C language. Visual C Visual C++ 6.0

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

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

Princeton University COS 333: Advanced Programming Techniques A Subset of Python 2.7

C - Basics, Bitwise Operator. Zhaoguo Wang

Introduction to the C Programming Language

Programming Languages: Part 4. Robert M. Dondero, Ph.D. Princeton University

CSCI-243 Exam 1 Review February 22, 2015 Presented by the RIT Computer Science Community

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

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

C Programming. Course Outline. C Programming. Code: MBD101. Duration: 10 Hours. Prerequisites:

Main differences with Java

EMBEDDED SYSTEMS PROGRAMMING Language Basics

SECTION II: LANGUAGE BASICS

Main Program. C Programming Notes. #include <stdio.h> main() { printf( Hello ); } Comments: /* comment */ //comment. Dr. Karne Towson University

The C Programming Language

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

Programming. Elementary Concepts

DETAILED SYLLABUS INTRODUCTION TO C LANGUAGE

UNIT- 3 Introduction to C++

LESSON 1. A C program is constructed as a sequence of characters. Among the characters that can be used in a program are:

Introduction. Following are the types of operators: Unary requires a single operand Binary requires two operands Ternary requires three operands

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

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

APPENDIX A : KEYWORDS... 2 APPENDIX B : OPERATORS... 3 APPENDIX C : OPERATOR PRECEDENCE... 4 APPENDIX D : ESCAPE SEQUENCES... 5

Arrays and Strings. Antonio Carzaniga. February 23, Faculty of Informatics Università della Svizzera italiana Antonio Carzaniga

A Fast Review of C Essentials Part I

Chapter 21: Introduction to C Programming Language

6.096 Introduction to C++ January (IAP) 2009

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

Programming in C UVic SEng 265

Lecture 3. More About C

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

Princeton University COS 333: Advanced Programming Techniques A Subset of Java

High-performance computing and programming I. Introduction to C programming in *NIX

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

Modifiers. int foo(int x) { static int y=0; /* value of y is saved */ y = x + y + 7; /* across invocations of foo */ return y; }


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

Faculty of Science Final Examination. Computer Science B Introduction to Software Systems

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

Kurt Schmidt. October 30, 2018

Array Initialization

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

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

Signals, Instruments, and Systems W2. C Programming (continued)

Data types, variables, constants

BLM2031 Structured Programming. Zeyneb KURT

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

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

Model Viva Questions for Programming in C lab

Applied Programming and Computer Science, DD2325/appcs15 PODF, Programmering och datalogi för fysiker, DA7011

Transcription:

Princeton University COS 333: Advanced Programming Techniques A Subset of C90 Program Structure /* Print "hello, world" to stdout. Return 0. */ { printf("hello, world\n"); ----------------------------------------------------------------------------------- int sqr(int i) { return i * i; { printf("%d\n", sqr(5)); ----------------------------------------------------------------------------------- int sqr(int i); { printf("%d\n", sqr(5)); int sqr(int i) { return i * i; ----------------------------------------------------------------------------------- Compiler must see definition or declaration of function before call of function. Building and Running gcc -Wall -ansi -pedantic program.c -o program program Terminal I/O Reading from stdin: ivaluesread = scanf("%d %lf", &i, &d); Writing to stdout: icharswritten = printf("%d %f", i, d); Page 1 of 7

Writing to stderr: icharswritten = fprintf(stderr, "%d %f", i, d); Keywords auto, break, case, char, const, continue, default, do, double, else, enum, extern, float, for, goto, if, int, long, register, return, short, signed, sizeof, static, switch, typedef, union, unsigned, void, volatile Primitive Data Types char 1 byte (char)-128, (char)127, 'a', '\n', '\t', '\0' unsigned char 1 byte (unsigned char)0, (unsigned char)256 short 2? bytes (short)-32768, (short)32767 unsigned short 2? bytes (unsigned short)0, (unsigned short)65535 int 4? bytes -2147483648, 2147483647 unsigned int 4? bytes 0U, 4294967295U, 0123, 0x123 long 4? bytes -2147483648L, 2147483647L unsigned long 4? bytes 0UL, 4294967295UL, 0123L, 0x123L float 4? bytes 123.456F double 8? bytes 123.456 long double 12? bytes 123.456L Use sizeof operator to determine sizes on your system. Variables must be declared before used. Operators fun(params) (1) function call (parameters passed by value) a[i] (1) array element selector s.f (1) structure field selector ps->f (1) structure dereference and field selector (type) (2) typecast *p, &x (2) dereference, address of i++, ++i, i-, --i (2) increment, decrement +x, -x (2) unary positive, unary negative ~i (2) bitwise NOT!i (2) logical NOT sizeof(type) (2) sizeof (performed at compile-time) x*y, x/y, i%j (3) multiplication, division, remainder x+y, x-y (4) addition, subtraction i<<j, i>>j (5) left-shift right-shift x<y, x>y, x<=y, x>=y (6) obvious x==y, x!=y (7) equality, inequality i&j (8) bitwise AND i^j (9) bitwise EXCLUSIVE OR i j (10) bitwise OR i&&j (11) logical AND i j (12) logical OR i?x:y (13) conditional expression x=y (14) assignment x+=y, x-=y, x*=y, x/=y, i%=j (14) assignment i&=j, i^=j, i =j (14) assignment i<<=j, i>>=j (14) assignment x,y (15) sequence Page 2 of 7

Statements Expression statement: expr; Declaration statement: modifiers datatype var [= expr][, var [= expr]]...; Common modifiers: const, static Compound statement: { statement; statement;... Selection statements: if (intorptrexpr) statement else statement Note: 0 or NULL pointer => false; anything else => true switch (intexpr) { case (intvalue1): statement;...; break; case (intvalue2): statement;...; break;... default: statement;...; Iteration statements: while (intorptrexpr) statement; do statement while (expr); for (initexpr; intorptrexpr; increxpr) statement; Note: 0 or NULL pointer => false; anything else => true break; continue; Return statement: return; return expr; Classes, Objects, and Object References None! But can use the opaque pointer pattern. File myclass.h: #ifndef MYCLASS_INCLUDED #define MYCLASS_INCLUDED Page 3 of 7

typedef struct MyClass *MyClass; MyClass MyClass_new(int i); void MyClass_free(MyClass obj); int MyClass_get(MyClass obj); void MyClass_set(MyClass obj, int i); int MyClass_equals(MyClass obj, MyClass objother); void MyClass_print(MyClass obj, FILE *psfile); #endif File myclass.c: #include "myclass.h" #include <stdlib.h> struct MyClass { int i; ; MyClass MyClass_new(int i) { MyClass obj = (MyClass)malloc(sizeof(struct MyClass)); if (obj == NULL) return NULL; obj->i = i; return obj; void MyClass_free(MyClass obj) { free(obj); int MyClass_get(MyClass obj) { return obj->i; void MyClass_set(MyClass obj, int i) { obj->i = i; int MyClass_equals(MyClass obj, MyClass objother) { return obj->i == objother->i; void MyClass_print(MyClass obj, FILE *psfile) { fprintf(psfile, "%d", obj->i); /*--------------------------------------------------------------------*/ #ifdef TEST_MYCLASS { MyClass obj1; MyClass obj2; obj1 = MyClass_new(5); if (obj1 == NULL) { fprintf(stderr, "Memory is exhausted.\n"); exit(exit_failure); Page 4 of 7

MyClass_set(obj1, 10); printf("%d\n", MyClass_get(obj1)); MyClass_print(obj1, stdout); printf("\n"); obj2 = MyClass_new(10); if (obj2 == NULL) { fprintf(stderr, "Memory is exhausted.\n"); exit(exit_failure); if (MyClass_equals(obj1, obj2)) printf("equal\n"); if (! MyClass_equals(obj1, obj2)) printf("not equal\n"); MyClass_free(obj2); MyClass_free(obj1); #endif Data Structures Arrays int a[5]; int a[] = {1,2,3; The name of an array is a constant pointer. Pointers int *p; NULL (the NULL pointer) is defined in stddef.h. Arrays and pointers a is the same as &a[0] a[i] is the same as *(a+i) Pointer arithmetic strides over an array. Structures struct S {int i; double d; ; /* Defines a structure type. */ struct S s; /* Defines a structure. */ Enumerations enum E {CONST1, CONST2, ; /* Defines an enumeration type. */ enum E e; /* Defines an enumeration. */ Dynamic data structures #include <stdlib.h> p = malloc(ibytes); p = calloc(ielements, ibytesperelement); p = realloc(p, inewbytes); free(p); Strings Page 5 of 7

A string is an array of chars terminated with '\0' "abc" denotes a string #include <string.h> i = strlen(pcstr); strcpy(pcdest, pcsrc); strncpy(pcdest, pcsrc, ilen); strcat(pcdest, pcsrc); strncat(pcdest, pcsrc, ilen); i = strcmp(pcfirst, pcsecond); i = strncmp(pcfirst, pcsecond, ilen); pc = strstr(pchaystack, pcneedle); Files FILE *psfile; psfile = fopen("filename", "mode"); /* mode: r, w, a, r+, w+, a+ */ fclose(psfile); Write/read a char: istatus = fputc(i, psfile); /* Returns i or EOF. */ istatus = putc(i, psfile); /* Returns i or EOF. */ istatus = putchar(i); /* Returns i or EOF. */ i = fgetc(psfile); /* Returns char within i, or EOF. */ i = getc(psfile); /* Returns char within i, or EOF. */ i = getchar(); /* Returns char within i, or EOF. */ Write/read a string: istatus = fputs(pcstr, psfile); /* Does not write trailing '\n'. */ istatus = puts(pcstr); /* Writes trailing '\n' */ pcstatus = fgets(pcstr, ibufsize, psfile); /* Reads trailing '\n'. Returns pcstr or NULL. */ pcstatus = gets(pcstr); /* Discards trailing '\n'. Returns pcstr or NULL. DANGEROUS!!! */ Write/read formatted data: icharswritten = fprintf(psfile, "%d", i); icharswritten = printf("%d", i); ivaluesread = fscanf(psfile, "%d", &i); ivaluesread = scanf("%d", &i); Command-Line Arguments The argv parameter of main() is an array of strings. argv[0] is the name of the program. argv[1], argv[2],... are command-line arguments. #include <stdlib.h> Page 6 of 7

int main(int argc, char *argv[]) {... if (argc!= 3) { fprintf(stderr, "Usage: %s arg1 arg2\n", argv[0]); exit(exit_failure);... Preprocessor Directives and Macros #include <somefile.h> /* Search system directories. */ #include "somefile.h" /* Search my directories first. */ #define name value #ifdef name... #endif #ifndef name... #endif assert(expr); Debugging To use the gdb debugger: gcc -Wall -ansi -pedantic -g program.c -o program emacs <Esc>x gdb<enter>program<enter> Debugger commands: help break function break filename:linenum run list next step continue print expr where quit Etc. Common commands have abbreviations: h, b r, l, n, s, c, p, q. Blank line means repeat the same command. We'll cover other features of C throughout the course as necessary. Copyright 2011 by Robert M. Dondero, Jr. Page 7 of 7