Computer Systems and Networks

Slides:



Advertisements
Similar presentations
Introduction to C Programming
Advertisements

A C++ Crash Course Part II UW Association for Computing Machinery Questions & Feedback.
C Language.
Introduction to C Programming Overview of C Hello World program Unix environment C programming basics.
Chapter 6 C Arrays Acknowledgment The notes are adapted from those provided by Deitel & Associates, Inc. and Pearson Education Inc. Arrays are data structures.
Guide To UNIX Using Linux Third Edition
Lecture No: 16. The scanf() function In C programming language, the scanf() function is used to read information from standard input device (keyboard).
Computer Science 210 Computer Organization Introduction to C.
CP104 Introduction to Programming File I/O Lecture 33 __ 1 File Input/Output Text file and binary files File Input/output File input / output functions.
Computer Engineering 1 nd Semester Dr. Rabie A. Ramadan 2.
1 計算機程式設計 Introduction to Computer Programming Lecture01: Introduction and Hello World 9/10/2012 Slides modified from Yin Lou, Cornell CS2022: Introduction.
Lecture 8 February 29, Topics Questions about Exercise 4, due Thursday? Object Based Programming (Chapter 8) –Basic Principles –Methods –Fields.
Arrays  Array is a collection of same type elements under the same variable identifier referenced by index number.  Arrays are widely used within programming.
Lecture 1 cis208 January 14 rd, Compiling %> gcc helloworld.c returns a.out %> gcc –o helloworld helloworld.c returns helloworld.
(language, compilation and debugging) David 09/16/2011.
15213 Recitation Section C Introduction Unix and C Playing with Bits Practice Problems Shimin Chen Sept. 9, 2002 Outline.
 2007 Pearson Education, Inc. All rights reserved C Arrays.
Lecture – Pointers1 C++ Pointers Joseph Spring/Bob Dickerson School of Computer Science Operating Systems and Computer Networks Based on notes by Bob Dickerson.
Announcements Assignment 1 due Wednesday at 11:59PM Quiz 1 on Thursday 1.
CSE 1320 Basics Dr. Sajib Datta
Sudeshna Sarkar, IIT Kharagpur 1 Programming and Data Structure Sudeshna Sarkar Lecture 3.
LECTURE 3 Translation. PROCESS MEMORY There are four general areas of memory in a process. The text area contains the instructions for the application.
INTRODUCTION TO PROGRAMING System Development Mansoura October 2015.
Lecture 3 Translation.
The Machine Model Memory
Strings CSCI 112: Programming in C.
Computer Science 210 Computer Organization
A bit of C programming Lecture 3 Uli Raich.
ECE Application Programming
2008/11/19: Lecture 18 CMSC 104, Section 0101 John Y. Park
TMF1414 Introduction to Programming
COSC 220 Computer Science II
Introduction to C CSE 2031 Fall /3/ :33 AM.
Numeric Arrays Numeric Arrays Chapter 4.
Computer Engineering 1nd Semester
BY GAWARE S.R. COMPUTER SCI. DEPARTMENT
C programming language
Module 2 Arrays and strings – example programs.
Administrative things
Input and Output Lecture 4.
Computer Science 210 Computer Organization
C Programming Lecture Series
IPC144 Introduction to Programming Using C Week 2 – Lesson 1
IPC144 Introduction to Programming Using C Week 1 – Lesson 2
CS 2308 Final Exam Review.
Chapter 6 - Arrays Outline 6.1 Introduction 6.2 Arrays
Lecture 13 Input/Output Files.
CS 2308 Exam I Review.
Chapter 11 Introduction to Programming in C
Introduction to CS Your First C Programs
Chapter 9 - Arrays Outline 6.1 Introduction 6.2 Arrays
Makefiles and Notes on Programming Assignment PA2
EKT150 : Computer Programming
Memory Allocation CS 217.
Introduction to C Topics Compilation Using the gcc Compiler
How Computers Work Part 1 6 February 2008.
Comp Org & Assembly Lang
Overloading functions
Introduction to C Topics Compilation Using the gcc Compiler
ECE 103 Engineering Programming Chapter 8 Data Types and Constants
Arrays.
Chapter 11 Programming in C
Introduction to C EECS May 2019.
Programming Languages and Paradigms
Compiler vs linker The compiler translates one .c file into a .o file
2008/11/19: Lecture 18 CMSC 104, Section 0101 John Y. Park
Introduction to C Programming
Administrative things
SPL – PS1 Introduction to C++.
Getting Started With Coding
Presentation transcript:

Computer Systems and Networks Lecture 4: C Programming 30th January 2018 Computer Systems and Networks Dr. Pallipuram (vpallipuramkrishnamani@pacific.edu) University of the Pacific

Deadlines Lab 2 January 30th, 2018 Lab 3 February 5th, 2018

Today’s Class The C compiler and Makefile printf, scanf, and format specifiers Arrays: one-dimensional and multi-dimensional Introduction to Pointers C compiler makefile ~15mins printf scanf ~10 minutes

A simple code compilation unix> gcc –o myprog main.c unix> ./myprog

Compiler Operation

Why So Many Compilation Steps? We don’t just care about 1 language or 1 processor family! C x86 C++ x86-64 Objective-C ARM GNU Compiler Collection Fortran PowerPC Ada 68000 Others… MIPS (and many more!)

When your program has multiple files unix> gcc main.c file2.c -o MyProg unix> ./MyProg

Linker + Loader

Result: Program binary (saved on disk) 110111010100000010100000011011101010000001010000001101110101000000101000000110111010100000010100000011011101010000001010000001101110101000000101000000110111010100000010100000011011101010000001010000001101110101000000101000000110111010100000010100000011011101010000001010000001101110101000000101000000110111010100000010100000011011101010000001

Operating System Goals Security: OK to run file? Memory management: Find space and create new virtual memory region for this program File system: Retrieve program binary code from disk Loader: Place program binary code into memory Scheduler: Find CPU time for program to run Context switch – Program starts running

Problem 1 Without Google search, can you identify the linux command to link object files.

Makefile Goal: Compile our program with one command: unix> make A Makefile is a text file that specifies how to compile your program The make utility reads the Makefile You’ll learn how this file works in Lab 3 unix> make

An Intermediate Makefile all: factorial_program factorial_program: main.o factorial.o output.o gcc main.o factorial.o output.o -o factorial_program main.o: main.c gcc -c main.c factorial.o: factorial.c gcc -c factorial.c output.o: output.c gcc -c output.c clean: rm -rf *.o factorial_program Explain all, factorial_program:, …

An Advanced Makefile

# The variable CC specifies which compiler will be used. # (because different unix systems may use different compilers) CC=gcc # The variable CFLAGS specifies compiler options # -c : Only compile (don't link) # -Wall: Enable all warnings about lazy / dangerous C programming CFLAGS=-c -Wall # The final program to build EXECUTABLE=factorial_program # --------------------------------------------

all: $(EXECUTABLE) $(EXECUTABLE): main.o factorial.o output.o $(CC) main.o factorial.o output.o -o $(EXECUTABLE) main.o: main.c $(CC) $(CFLAGS) main.c factorial.o: factorial.c $(CC) $(CFLAGS) factorial.c output.o: output.c $(CC) $(CFLAGS) output.c clean: rm -rf *.o $(EXECUTABLE)

C Tutorial

Print with printf() printf("This is a string\n"); printf("The integer is %i\n", num); printf("The floating-point values are %g and %g\n", num1, num2);

Corresponding Variable Type Output with printf() Format “Type” Code Corresponding Variable Type d or i int (interpret as signed 2’s comp) u int (interpret as unsigned) x int (print as hexadecimal) f or g float/double c char s string (null-terminated array of chars) p An address to which the pointer points Prefix with l or ll (i.e. “long” or “long long” for larger 64-bit data types) Lots of formatting options not listed here… # of digits before / after decimal point? Pad with zeros?

Input with scanf() Input from console scanf("%d %c", &myint, &mychar) Requires the address of the destination variable Use the & operator to obtain address Caveat: Array names are already the “address of”! char myarray[8]; scanf("%s", myarray) No & needed here!

Problem 2 – Read the man pages for printf and scanf Man(ual) pages exist for common programming functions too unix> man printf unix> man scanf

Arrays

Arrays Contiguous block of memory You can have arrays for int, char, float, double, structures… int myarray[5]; //static declaration NOTE: Name of the array is the address of the first element 4 8 12 16 20 myarray[0] myarray[1] myarray[2] myarray[3] myarray[4] address: printf(“%p”,myarray); //prints what?

2-dimensional arrays int myarray[5][5]; //static declaration Memory map: Address: 4 myarray[0][0] Address: 8 Address: 12 Address: 16 Address: 20 Address: 24 Address: 28 myarray[1][1] Address: 32 Address: 36 Address: 40 Address: 44 Address: 488 Address: 52 Address: 56 Address: 60 Address: 64 Address: 68 Address: 72 myarray[3][2] Address: 76 Address: 80 Address: 84 Address: 88 Address: 92 Address: 96 Address: 100 indices start at [0][0]

Problem 3: Looping through an array Consider a 3-D array, int image[256][256][3] for an RGB color image. The first subscript denotes the number of rows, the second subscript denotes the number of columns, and the third subscript denotes the number of color channels. For example, a pixel at row i and column j will have an R value given by image[i][j][0], G value given by image[i][j][1], and B value given by image[i][j][2]. Any pixel has a yellow color if it’s R and G values are 255 and B value is 0. Write a for loop to search for the location of the very first yellow pixel in image. The search should terminate once the yellow pixel is found. Search in row-wise manner.

Pointers

Pointers are special variables that hold/store memory addresses of other variables. When a pointer, say iptr, holds the address of an integer variable, say ivar, then we say: “iptr is an integer pointer that points to ivar.” int ivar=45; int *iptr; iptr = &ivar; //iptr points to ivar ivar: 45 iptr: 65536 address: 65536 address: 65520 ‘&’ is ‘address of variable’ operator. For example, &ivar translates to: “address of variable ivar”. ‘*’ is ‘value at address stored in pointer’ operator. For example, *iptr translates to: “value at address stored in pointer iptr”.

We can have a ‘multiple’ pointer Example pointer declaration: int *iptr; //an integer pointer that will point to an integer int **dptr; //A double pointer that will point to an integer pointer int ***tptr; //A triple pointer pointing to a double pointer. int ****quadptr //___________________________________

Problem 4

Problem 5

Next Class Pointer basics Pointers and Arrays Dynamic Allocation Pointers and Structures Linked Lists File I/O in C