CS349/SE382 A1 C Programming Tutorial

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

Darshan Institute of Engineering & Technology for Diploma Studies Unit 5

Structures and Pointers

Pointers, Dynamic Data, and Reference Types

Tokens, Expressions and Control Structures

Lecture 14. No in-class files today. Homework 7 (due on Wednesday) and Project 3 (due in 10 days) posted. Questions?

6.096 Introduction to C++ January (IAP) 2009

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

CS3157: Advanced Programming. Outline

Short Notes of CS201

advanced data types (2) typedef. today advanced data types (3) enum. mon 23 sep 2002 defining your own types using typedef

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

CS201 - Introduction to Programming Glossary By

Basic Types & User Defined Types

September 10,

Floating-point lab deadline moved until Wednesday Today: characters, strings, scanf Characters, strings, scanf questions clicker questions

CS2141 Software Development using C/C++ C++ Basics

Objectives. Describe ways to create constants const readonly enum

Aryan College. Fundamental of C Programming. Unit I: Q1. What will be the value of the following expression? (2017) A + 9

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

C Programming Language: C ADTs, 2d Dynamic Allocation. Math 230 Assembly Language Programming (Computer Organization) Thursday Jan 31, 2008

Class Information ANNOUCEMENTS

COMS W3101 Programming Language: C++ (Fall 2015) Ramana Isukapalli

22c:111 Programming Language Concepts. Fall Types I

Lectures 5-6: Introduction to C

Dynamic Memory Allocation and Command-line Arguments

typedef int Array[10]; String name; Array ages;

Introduction to C++ Professor Hugh C. Lauer CS-2303, System Programming Concepts

TYPES, VALUES AND DECLARATIONS

CSCI312 Principles of Programming Languages!

CS113: Lecture 9. Topics: Dynamic Allocation. Dynamic Data Structures

Introduction to C. Sean Ogden. Cornell CS 4411, August 30, Geared toward programmers

Lectures 5-6: Introduction to C

C for C++ Programmers

C Programming Basics II

Compiling and Running a C Program in Unix

Input And Output of C++

Warmup January 9th, What is the value of the following C expression? 8*9 % 10/ 2

Introduction to C++ Systems Programming

Fundamentals of Programming Languages

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

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

DAY 3. CS3600, Northeastern University. Alan Mislove

M4.1-R3: PROGRAMMING AND PROBLEM SOLVING THROUGH C LANGUAGE

Fast Introduction to Object Oriented Programming and C++

(2-1) Data Structures & The Basics of a Linked List I. Instructor - Andrew S. O Fallon CptS 122 (August 27, 2018) Washington State University

Fundamental of C programming. - Ompal Singh

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

Review: C Strings. A string in C is just an array of characters. Lecture #4 C Strings, Arrays, & Malloc

Data Types and Variables in C language

A Fast Review of C Essentials Part I

QUIZ. 1. Explain the meaning of the angle brackets in the declaration of v below:

PRINCIPLES OF OPERATING SYSTEMS

Introduction to C. Ayush Dubey. Cornell CS 4411, August 31, Geared toward programmers

C Programming SYLLABUS COVERAGE SYLLABUS IN DETAILS

CS201- Introduction to Programming Current Quizzes

COMS W3101 Programming Language: C++ (Fall 2015) Ramana Isukapalli

Variables. Data Types.

Non-numeric types, boolean types, arithmetic. operators. Comp Sci 1570 Introduction to C++ Non-numeric types. const. Reserved words.

Basic program The following is a basic program in C++; Basic C++ Source Code Compiler Object Code Linker (with libraries) Executable

Chapter 11: Structured Data

CS3157: Advanced Programming. Announcement

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

CS Introduction to Programming Midterm Exam #2 - Prof. Reed Fall 2015

COMS W3101 Programming Language: C++ (Fall 2016) Ramana Isukapalli

C Programming Review CSC 4320/6320

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

C Concepts - I/O. Lecture 19 COP 3014 Fall November 29, 2017

Chapter 2 (Dynamic variable (i.e. pointer), Static variable)

Kurt Schmidt. October 30, 2018

Programming Language. Control Structures: Selection (switch) Eng. Anis Nazer First Semester


PROGRAMMING IN C++ KAUSIK DATTA 18-Oct-2017

Chapter 15 - C++ As A "Better C"

Dynamic Data Structures. CSCI 112: Programming in C

CS113: Lecture 9. Topics: Dynamic Allocation. Dynamic Data Structures

CSC C69: OPERATING SYSTEMS

C C C C++ 2 ( ) C C++ 4 C C

Chapter 11: Structured Data

Chapter 3, Selection. Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved.

Writing Functions in C

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

ALQUDS University Department of Computer Engineering

OBJECTIVE QUESTIONS: Choose the correct alternative:

Review of the C Programming Language for Principles of Operating Systems

AMCAT Automata Coding Sample Questions And Answers

CS24 Week 2 Lecture 1

Review of the C Programming Language

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

COMP26120: Linked List in C (2018/19) Lucas Cordeiro

Announcements. assign0 due tonight. Labs start this week. No late submissions. Very helpful for assign1

Memory. What is memory? How is memory organized? Storage for variables, data, code etc. Text (Code) Data (Constants) BSS (Global and static variables)

Linked-List Basic Examples. A linked-list is Linear collection of self-referential class objects, called nodes Connected by pointer links

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

Introduction to C. Zhiyuan Teo. Cornell CS 4411, August 26, Geared toward programmers

EEE145 Computer Programming

CSCI 171 Chapter Outlines

BLM2031 Structured Programming. Zeyneb KURT

Data Structures and Algorithms for Engineers

EECS402 Lecture 24. Something New (Sort Of) Intro To Function Pointers

Transcription:

CS349/SE382 A1 C Programming Tutorial Erin Lester January 2005

Outline Comments Variable Declarations Objects Dynamic Memory Boolean Type structs, enums and unions Other Differences The Event Loop

Comments Comments: C has no one-line comments, only block comments /* this comment is valid in C */ /* so is this one */ // but this one is not i = 2; // and neither is this one

Variable Declarations Variable Declarations: Variables must be declared at the beginning of code blocks, before any other types of statements #include <stdio.h> void main() { char c; int i; i = 10 + 2; char *mystring = "Hello World"; /* not allowed */ c = \n ; printf("%s%c", mystring, c); }

Objects Objects: C has no objects use ADTs remember cs246 to be modular

Objects Creating a mock class/adt Step 1. Create a header file for your mock class include: empty structure declaration function headers w/ structure as parameter

Objects Creating a mock class/adt Step 2. Implement the code for the mock class in a.c file: include your.h file declare the structure implement the ADT s functions

Dynamic Memory Dynamic Memory: Dynamic Memory Allocation memory is allocated using malloc() malloc() takes the number of bytes of memory as its parameter it returns a (void*) pointer to the memory allocated the pointer can then be cast to the desired type and the memory assigned to a variable

Dynamic Memory typedef struct {... } mystruct;... mystruct *m = (mystruct *) malloc( sizeof( mystruct ) );

Dynamic Memory Freeing Dynamic Memory memory is freed using free() free() takes the address of a non-null variable as its parameter there is no garbage collection, so don t forget to clean-up what you create free( m );

Boolean Type Boolean Type: zero evaluates to false non-zero evaluates to true use typedef and defines to mimic the boolean type

Boolean Type /* Here s how to mimic a boolean type */ #define true 1 #define false 0 typedef int bool;... bool mybool = true;

structs, enums and unions Structures: struct types must be qualified by the word struct struct is prepended to their name when referencing them can use typedef to give a single name to a C struct

structs, enums and unions Declaring a C struct normally struct yourstruct {... /* variables in the structure */ }; versus using the typedef-method typedef struct {... /* variables in the structure */ } mystruct;

structs, enums and unions Differences in declaring the two structures: struct yourstruct boohoo; mystruct haha; C enumerations and unions are similar: you have to use the syntax enum xxx and union xxx in referencing them.

structs, enums and unions Enumeration Differences: C++ enumerations are a set of ints C enumerations are typedefed ints (i.e. no range restrictions) compiler does not catch assignments outside of the declared range typedef enum day = {SUNDAY, MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, SATURDAY} Day;... Day today = 9999; /* Logical error, today should be between 0 and 6, but compiler will not complain! */

Other Differences Other Differences: Libraries: C uses different libraries than C++ For example C I/O uses stdio.h instead of iostream.h, printf instead of cout, and scanf instead of cin See a C reference for more details C does not allow function overloading. C does not permit default values for function arguments.

The Event Loop Basic Event Loop while( 1 ) { if ( getnextevent( &event ) ) processevent( event ); doeventindependentprocessing(); updatedisplay(); }

The Event Loop Basic Event Loop getnextevent(): blocking or non-blocking processevent(): depends on event type switch stmt doeventindependentprocessing(): do non-event specific processing (i.e. time-based processing) updatedisplay(): reflect changes made by event (& possibly time)

The Event Loop Questions? Feel free to ask questions now or via email: eclester@cgl.uwaterloo.ca