Programming in C S c o t t S c h r e m m e r

Similar documents
PRINCIPLES OF OPERATING SYSTEMS

CSE 124 Discussion (10/3) C/C++ Basics

CSci 4061 Introduction to Operating Systems. Programs in C/Unix

CSCI 171 Chapter Outlines

Class Information ANNOUCEMENTS

Lectures 5-6: Introduction to C

15213 Recitation Section C

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

CSE 351. GDB Introduction

Recitation #11 Malloc Lab. November 7th, 2017

ch = argv[i][++j]; /* why does ++j but j++ does not? */

1 A Brief Introduction To GDB

Arrays and Pointers. CSC209: Software Tools and Systems Programming (Winter 2019) Furkan Alaca & Paul Vrbik. University of Toronto Mississauga

Lectures 5-6: Introduction to C

COMP26912: Algorithms and Imperative Programming

Programming Tools. Venkatanatha Sarma Y. Lecture delivered by: Assistant Professor MSRSAS-Bangalore

CSCI 2132 Software Development. Lecture 29: Dynamic Memory Allocation

Reviewing gcc, make, gdb, and Linux Editors 1

C: Program Structure. Department of Computer Science College of Engineering Boise State University. September 11, /13

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

EL2310 Scientific Programming

COSC 2P91. Introduction Part Deux. Week 1b. Brock University. Brock University (Week 1b) Introduction Part Deux 1 / 14

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

File I/O. Last updated 10/30/18

CIS 190: C/C++ Programming. Lecture 3 Memory Management in C

JTSK Programming in C II C-Lab II. Lecture 3 & 4

High Performance Computing

Lecture 3. More About C

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

2 Compiling a C program

Programming Studio #9 ECE 190

C Programming Review CSC 4320/6320

Lecture 02 C FUNDAMENTALS

Exercise Session 6 Computer Architecture and Systems Programming

Compiling and Running a C Program in Unix

Programs. Function main. C Refresher. CSCI 4061 Introduction to Operating Systems

CSCI 2132 Final Exam Solutions

CS201 - Lecture 1 The C Programming Language

Chapter 14 - Advanced C Topics

CS354 gdb Tutorial Written by Chris Feilbach

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

6.S096: Introduction to C/C++

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

OBJECTIVE QUESTIONS: Choose the correct alternative:

NEXT SET OF SLIDES FROM DENNIS FREY S FALL 2011 CMSC313.

GDB Tutorial. A Walkthrough with Examples. CMSC Spring Last modified March 22, GDB Tutorial

INTERMEDIATE SOFTWARE DESIGN SPRING 2011 ACCESS SPECIFIER: SOURCE FILE

CMPT 300. Operating Systems. Brief Intro to UNIX and C

CSE 374 Programming Concepts & Tools

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

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

Variables in C. Variables in C. What Are Variables in C? CMSC 104, Fall 2012 John Y. Park

Systems Programming/ C and UNIX

Deep C. Multifile projects Getting it running Data types Typecasting Memory management Pointers. CS-343 Operating Systems

Lecture 2. Xiaoguang Wang. January 16th, 2014 STAT 598W. (STAT 598W) Lecture 2 1 / 41

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

Continue: How do I learn C? C Primer Continued (Makefiles, debugging, and more ) Last Time: A Simple(st) C Program 1-hello-world.c!

PERIYAR CENTENARY POLYTECHNIC COLLEGE Periyar Nagar- Vallam Thanjavur

C / C++ Coding Rules

C Programming SYLLABUS COVERAGE SYLLABUS IN DETAILS

CSE 303, Spring 2009 Final Exam Wednesday, June 10, 2009

Oregon State University School of Electrical Engineering and Computer Science. CS 261 Recitation 2. Spring 2016

C-types: basic & constructed. C basic types: int, char, float, C constructed types: pointer, array, struct

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

C introduction: part 1

independent compilation and Make

Recitation 2/18/2012

Quiz 0 Review Session. October 13th, 2014

Topic 6: A Quick Intro To C

ECEN 449 Microprocessor System Design. Review of C Programming

Lecture 07 Debugging Programs with GDB

Kurt Schmidt. October 30, 2018

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

CS61, Fall 2012 Section 2 Notes

Data and File Structures Laboratory

Computer Science 2500 Computer Organization Rensselaer Polytechnic Institute Spring Topic Notes: C and Unix Overview

CSCI 2132: Software Development. Norbert Zeh. Faculty of Computer Science Dalhousie University. Introduction to C. Winter 2019

Lecture 2: C Programm

Workspace for '3-ctips' Page 1 (row 1, column 1)

Scientific Programming in C IX. Debugging

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

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

Short Notes of CS201

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

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

CSE 333 Midterm Exam July 24, Name UW ID#

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

Key C Topics: Tutorial Pointers, Dynamic Memory allocation, Valgrind and Makefile CS370

Recitation: Cache Lab & C

CSE au Midterm Exam Nov. 2, 2018 Sample Solution

ECE 250 / CPS 250 Computer Architecture. C Programming

.. Cal Poly CPE 101: Fundamentals of Computer Science I Alexander Dekhtyar..

C Program Development and Debugging under Unix SEEM 3460

CS201 - Introduction to Programming Glossary By

Introduction to the C Programming Language

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

High Performance Programming Programming in C part 1

Programming in C. Lecture 9: Tooling. Dr Neel Krishnaswami. Michaelmas Term

Portland State University Maseeh College of Engineering and Computer Science. Proficiency Examination Process

H192 Midterm 1 Review. Tom Zajdel

Transcription:

Programming in C S c o t t S c h r e m m e r

Outline Introduction Data Types and structures Pointers, arrays and dynamic memory allocation Functions and prototypes input/output comparisons compiling/makefiles/debugging

Basic Data Types Variables must be declared before any instructions char,int,float,double no boolean! not initialized a string is represented as a character array char samplestring[20]= sample string ;

Structures No classes Create functions that work on structures of data Sample declaration: struct person { int height, int weight}bob,sue; bob.height = 100; bob.height= 2*sue.height;

Typedef Used to refer to a type with a different name typedef unsigned int wholenumber; wholenumber a,b,c; a=5; /*etc*/ typically used with struct

Typedef Struct typedef struct optionalname {int height; int weight} person; person bob,sue; bob.height = 100; /*etc*/

Arrays int anarray[10]; anarray is really a pointer to the beginning of an array no bounds checking or length available! anarray[20] may cause bizarre behavior

Pointers The equivalent of an address: int *pointertoint; int theint; pointertoint=&theint; /* the addrss of theint */ *pointertoint = 5; /*follow the pointer*/ printf( %d,theint); 5

Dynamic memory allocation int *anintpointer,*anarray; anintpointer = (int *)malloc(sizeof(int)); reserves space for 1 integer anarray=(int *)malloc(5*sizeof(int)); reserves space for a 5 element array calloc--initialize memory to zero

Dynamic Memory Allocation int * anarray; anarray = (int *)malloc(10*sizeof(int)); Equivalent: anarray[3]=3; *(anarray+3) =3; pointer arithmetic, increments by size of an integer

Dynamic Memory With Structs typedef struct {int height,int weight} person; person *bob; bob = (bob *) malloc(sizeof(person)); equivalent: (*bob).height = 5; bob->height =5; Pass to a function as a pointer

Creating a linked list typedef struct listelem{ Person *person; struct listelem *next; } ListElem; head of list frequently a double pointer last element next=null

Dynamic memory allocation No garbage collection free(apointer); Careful to free before all references are lost Free all elements of an array of pointers Memory leaks can be a significant problem

Don t return Pointers to Local variables! DON T DO: Person * somefunction() { Person *apointerperson; Person aperson; apointerperson = &aperson; return apointerperson;} INSTEAD malloc memory for new person

Functions int asamplefunction(int a,int b) { return a*b;} Passed by value Except pointers, arrays Function prototype must exist prior to location in code

Sample function prototype int asamplefunction(int,int) main() { printf( %d,asamplefunction(5,4)); } int asamplefunction(int a,int b) { return a*b;}

Prototypes and.h files Function may be in a separate file or library Link with after compiling Prototype usually contained in.h file #include file.h or #include <file.h> Put functions in file.c prototypes in file.h To include i/o functions: #include <stdio.h>

Preprocessor DIRECTIVES Preprocessor run prior to compilation #define CONSTANT value replaces CONSTANT with value (textual replace) #define SUM(a,b) a+b macros, simply replaces SUM(this,that) with this+that use -D flag to set constants at compilation #if,#else,#ifdef,#endif

Input/Output #include <stdio.h> Output: printf( formating string,arg1,arg2,etc); special sequences: (man printf) \n -- insert newline \t -- insert tab %d -- insert an integer value %g -- insert a double

Sample Output int aninteger =5; int adouble = 0.35; printf( I am printing an integer %d\nand a double %g,anint,adouble); I am printing an integer 5 and a double 0.35

Input To input from the standard input: int aninputint; scanf( %d,&aninputint); note need to pass a pointer to the int

File io FILE *filepointer; filepointer = fopen( filename,mode); /*Access the file*/ fclose(filepointer); Sample modes: (man fopen) r text file for reading w text file for writing a append to existing text file rb, wb, ra as above with binary file

File io write to file: fprint(filepointer, Astring,arg1,arg2,etc); read from a file fscan(filepointer, Astring,arg1,arg2,etc); fgetc,fread

Comparisons No boolean types! a<b returns 1 if a<b, 0 otherwise while(1) {} infinite loop if,while,do while, for etc work as expected

Compilation Typically use gnu c compiler on linux machines gcc -o outfile file1.c file2.c file3.o gcc -c compiles only but doesn t link (file.o) other options: -w inhibit warning, -Wall include all warnings -On (n=1,2,3) set optimization level -g include debugging information

Makefiles Use dependencies to determine what to compile Good for large projects as limit code that must be recompiled Quirky about formatting, tabs important

Sample Makefile all: client server client: client.c gcc client.c -o client server: server.c gcc server.c -o server clean: rm client server

More Complex makefile all: theprogram theprogram: file1.o file2.o file3.o gcc file1.o file2.o file3.o -o theprogram file1.o: file1.c headerfile.h gcc -c file1.c file2.o etc...

Makefiles Many more complex configurations possible variables for compiler and flags file lists to avoid an entry for each object file

DEBUGGER Call from command line gdb executable debugger commands run parameter list -- starts the program setting break points break file.c:10 -- break point on line 10 break function -- break point at start of function

Debugger Stepping through program next -- executes current command (steps over functions) step -- falls into functions continue -- continue to execute until next breakpoint Displaying local variables print variable_name where indicates position in program and functions called

Debugger bt back trace disp variablename displays variable every time program pause set variable variablename = 12 used to modify variables call function(arguments) immediately calls a function can be used to display structures, lists etc.