A Short Course for REU Students Summer Instructor: Ben Ransford

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

A Short Course for REU Students Summer Instructor: Ben Ransford

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

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

PRINCIPLES OF OPERATING SYSTEMS

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

ISA 563 : Fundamentals of Systems Programming

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

Kurt Schmidt. October 30, 2018

Introduction to C. Systems Programming Concepts

Lecture 5: Multidimensional Arrays. Wednesday, 11 February 2009

ECET 264 C Programming Language with Applications

ECET 264 C Programming Language with Applications

CS 0449 Sample Midterm

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

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

Basic C Program: Print to stdout. Basic C Program. Basic C Program: Print to stdout. Header Files. Read argument and print. Read argument and print

int main(void) { int a, b, c; /* declaration */

LESSON 5 FUNDAMENTAL DATA TYPES. char short int long unsigned char unsigned short unsigned unsigned long

Data Representa5on. CSC 2400: Computer Systems. What kinds of data do we need to represent?

Data Representa5on. CSC 2400: Computer Systems. What kinds of data do we need to represent?

EL2310 Scientific Programming

ECEN 449 Microprocessor System Design. Review of C Programming. Texas A&M University

From Java to C. Thanks to Randal E. Bryant and David R. O'Hallaron (Carnegie-Mellon University) for providing the basis for these slides

F28HS2 Hardware-Software Interface. Lecture 3 - Programming in C 3

Midterm Exam Review Slides

Fundamental of Programming (C)

Lab Exam 1 D [1 mark] Give an example of a sample input which would make the function

Today. o main function. o cout object. o Allocate space for data to be used in the program. o The data can be changed

ECEN 449 Microprocessor System Design. Review of C Programming

C LANGUAGE A Short Course

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

CS 5513 Entry Quiz. Systems Programming (CS2213/CS3423))

Main differences with Java

Work relative to other classes

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

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

int main() { return 0; }

First of all, it is a variable, just like other variables you studied

EL2310 Scientific Programming

COMP 2001/2401 Test #1 [out of 80 marks]

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

EL2310 Scientific Programming

Programming Language A

CSC 8400: Computer Systems. Represen3ng and Manipula3ng Informa3on. Background: Number Systems

Fundamentals of Programming

Tutorial 1: C-Language

Dynamic Memory Allocation and Command-line Arguments

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

CS 61C: Great Ideas in Computer Architecture. Lecture 2: Numbers & C Language. Krste Asanović & Randy Katz

Agenda. CS 61C: Great Ideas in Computer Architecture. Lecture 2: Numbers & C Language 8/29/17. Recap: Binary Number Conversion

CS 61c: Great Ideas in Computer Architecture

/* defines are mostly used to declare constants */ #define MAX_ITER 10 // max number of iterations

Fundamental of Programming (C)

Programming refresher and intro to C programming

ECE 250 / CS 250 Computer Architecture. C to Binary: Memory & Data Representations. Benjamin Lee

CS360 Midterm 1 - February 21, James S. Plank. Put all answers on the answer sheet. In all of these questions, please assume the following:

EMBEDDED SYSTEMS PROGRAMMING Language Basics

Programming. Data Structure

COMP s1 Lecture 1

CprE 288 Introduction to Embedded Systems Exam 1 Review. 1

COMP 2355 Introduction to Systems Programming

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

CSCI 2132 Software Development. Lecture 29: Dynamic Memory Allocation


The output: The address of i is 0xbf85416c. The address of main is 0x80483e4. arrays.c. 1 #include <stdio.h> 3 int main(int argc, char **argv) 4 {

CS 261 Data Structures. Introduction to C Programming

Slide Set 2. for ENCM 335 in Fall Steve Norman, PhD, PEng

High Performance Programming Programming in C part 1

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

INTRODUCTION TO COMPUTER SCIENCE - LAB

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

CS 61C: Great Ideas in Computer Architecture Introduc=on to C, Part I. We are here!

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

Informatica e Sistemi in Tempo Reale

Fundamentals of Programming

DEPARTMENT OF COMPUTER ENGINEERING CMPE101: Foundation of Computer Engineering EXPERIMENT 3. Introduction to C Programming: Sequential code structure

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

Day05 A. Young W. Lim Sat. Young W. Lim Day05 A Sat 1 / 14

Lab 1: Introduction to C Programming

211: Computer Architecture Summer 2016

mith College Computer Science CSC352 Week #7 Spring 2017 Introduction to C Dominique Thiébaut

Pointers (continued), arrays and strings

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

Chapter 1 Programming in C

COP 3223 Introduction to Programming with C - Study Union - Fall 2017

C programming basics T3-1 -

C for C++ Programmers

CS61, Fall 2012 Section 2 Notes

High-performance computing and programming Intro to C on Unix/Linux. Uppsala universitet

C Pointers. Abdelghani Bellaachia, CSCI 1121 Page: 1

Final Intro to C Review

Understanding Pointers

Course Outline Introduction to C-Programming

School of Electrical Engineering & Computer Science Oregon State University. CS Recitation 3 Spring 2012

Quiz 0 Answer Key. Answers other than the below may be possible. Multiple Choice. 0. a 1. a 2. b 3. c 4. b 5. d. True or False.

C - Basics, Bitwise Operator. Zhaoguo Wang

THE FUNDAMENTAL DATA TYPES

large units some names for large numbers CIS 2107 Chapter 2 Notes Part 1

Transcription:

C A Short Course for REU Students Summer 2008 Instructor: Ben Ransford http://www.cs.umass.edu/~ransford/ ransford@cs.umass.edu 1

Outline Today: basic syntax, compilation Next time: pointers, I/O, libraries Goal: soundness, not completeness http://www.cs.umass.edu/~ransford/reuc/ 2

Why learn C? It s everywhere. Operating systems Networking Embedded systems Graphics Other languages (Python, Ruby, Perl,...) Space shuttles! Robots! 3

C vs. C++ C not object-oriented C C++ C easy to parse C easy to compile 80 60 40 20 0 Amount of Stuff 4

C vs. C++ C not object-oriented C C++ C easy to parse C easy to compile 80 60 40 why C is everywhere! 20 0 Amount of Stuff 4

Hello, World hello.c: 1: #include <stdio.h> 2: 3: int main (int argc, char** argv) { 4: 5: printf("moo.\n"); 6: 7: return 0; 8: } 5

Compiling C My favorite compiler: GCC (free) gcc -o executable source e.g.: 6

Math library sin.c: 1: #include <stdio.h> 2: #include <math.h> 3: 4: int main (int argc, char** argv) { 5: float angle = M_PI / 2.0; /* in radians */ 6: float sine = sin(angle); 7: 8: printf("the sine of %f is %f\n", angle, sine); 9: } 7

Things you ll find in C /* comments of any length */ // single-line comments assignment = expressions; function(calls); control flow #compiler directives 8

Control flow 1 if (<condition>) { // do this if <condition> is true } else { // otherwise do this } 9

Control flow 2 while (<test>) { } // body goes here do { What s the difference? // body goes here } while (<test>); 10

Control flow 3 for (<initialization>; <test>; <update>) { } // body goes here <initialization>; while (<test>) { (is the same as) } // body goes here <update>; 11

Control flow 4 switch (<thing>) { case <value1>: <action1>; break; case <value2>: <action2>; break; default: <default_action>; } Control cascades downward. 12

Loops example loops.c: 1: #include <stdio.h> 2: 3: int main (int argc, char** argv) { 4: 5: int i; 6: int j = 0; 7: 8: // what is the value of j after this loop? 9: for (i = 0; i < 10; i++) { 10: j = j + 10; 11: } 12: printf("j = %d\n", j); 13: 14: while (j > 20) { 15: j = j - i; 16: } 17: printf("j = %d\n", j); 18: 19: do { 20: j = j - 1; 21: } while (100!= 100); 22: printf("j = %d\n", j); 23: 24: } 13

Functions <keywords> <type> <name> (<params>) { // body goes here return <value>; } int absolute_value (int number) { /*... */ } static void say_hello () { printf( Hello\n ); } 14

Function example absval.c: 1: #include <stdio.h> 2: 3: unsigned int absval (int value) { 4: if (value < 0) 5: return -value; 6: return value; 7: } 8: 9: int main (int argc, char** argv) { 10: int a = 10, b = -20; 11: printf("a = %d; b = %d\n", a, b); 12: 13: printf("absval(a) = %d\n", absval(a)); 14: printf("absval(b) = %d\n", absval(b)); 15: } 15

{... } C is block-structured. That means: { everything s in a block. } if (1 == 1) { a(); } else { b(); } Can omit braces for single statements Same as: if (1 == 1) a() else b() 16

Types Very few built-in types. Integer & float types Casting: (type)value, e.g.: char c = 5; int d = (int)c; 17

Built-in types example types.c: 1: #include <stdio.h> 2: 3: int main (int argc, char** argv) { 4: 5: printf("integer types:\n"); 6: printf("char: %d bytes\n", sizeof(char)); 7: printf("short: %d bytes\n", sizeof(short)); 8: printf("int: %d bytes\n", sizeof(int)); 9: printf("long: %d bytes\n", sizeof(long)); 10: printf("long long: %d bytes\n", sizeof(long long)); 11: 12: printf("-------------\n"); 13: 14: printf("floating-point types:\n"); 15: printf("float: %d bytes\n", sizeof(float)); 16: printf("double: %d bytes\n", sizeof(double)); 17: 18: return 0; 19: } 18

User-defined types So-called composite types struct foo { int x; float y; }; foo.x = 5; foo.y = 8.5; 19

A user-defined type struct.c: 1: #include <stdio.h> 2: struct foo { int x; int y; }; 3: int main (int argc, char** argv) { 4: struct foo ecks; 5: ecks.x = 10; 6: ecks.y = 10; 7: printf("x=%d; y=%d\n", ecks.x, ecks.y); 8: return 0; 9: } 20

Familiar operators a+b, a-b, a*b, a/b a%b (a modulo b, the remainder of a/b) ++i, i++, --j, j-- a==b, a!=b,!(a==b) Bitwise: & (AND), (OR), ~ (NOT), ^ (XOR) 21

Bitwise operators bitmath.c: what will this print? 1: #include <stdio.h> 2: #include "bitmath.h" 3: 4: int main (int argc, char** argv) { 5: unsigned int a, b, c, d, e; 6: 7: a = 1; 8: b = a << 3; // shift left 9: c = b; // complement 10: d = c & 0x0000ffff; // bitwise AND 11: e = d 0x0f000000; // bitwise OR 12: 13: printf("a = "); print_binary(a); 14: printf("b = "); print_binary(b); 15: printf("c = "); print_binary(c); 16: printf("d = "); print_binary(d); 17: printf("e = "); print_binary(e); 18: } 22

Compiler directives #define A_NUMBER 42 Compiler replaces instances of A_NUMBER with 42 as it reads the program. #include <foo.h> #include bar.h // from standard location // from current directory 23

Memory Here goes. Ugh. In C, you manage your own memory. Allocate with malloc(), from stdlib.h Free with free(), ditto Requires pointers (next class) 24

Memory example memory.c: 1: #include <stdio.h> 2: #include <stdlib.h> /* provides malloc() and free() */ 3: 4: int main (int argc, char** argv) { 5: int* an_integer_pointer; 6: int size = sizeof(int); 7: 8: an_integer_pointer = malloc(sizeof(int)); // allocate memory 9: 10: if (an_integer_pointer == NULL) { 11: fprintf(stderr, "Failed to malloc %d bytes!\n", size); 12: return 1; 13: } else { 14: printf("successfully allocated %d bytes.\n", size); 15: free(an_integer_pointer); // release the memory 16: return 0; 17: } 18: } 25

C at UMass CS ssh username@fenris.cs.umass.edu pico someprogram.c gcc -o someprogram someprogram.c./someprogram 26

For Next Time Pointers, I/O, libraries, some tricks Assignment (optional but helpful); download from the web page http://www.cs.umass.edu/~ransford/reuc/ 27