CS 211 Recitation 1 TA: Ishani Chakraborty. Admin stuff Office hours: 5:00-6:00 pm, Tuesdays at Core.

Slides:



Advertisements
Similar presentations
C Language.
Advertisements

CMSC 341 Makefile Review. 1 Make Overview make is a program that automates the compilation of programs whose files are dependent on each other A program.
Miguel Garzon CrUise Lab - SITE. #include dynamic memory allocationdynamic memory allocation Input/output String handlingString handling Mathematical.
Command Line arguments Main function: int main(argc, char *argv[]) –argc is the count of command line arguments –argv is an array of strings argv[0] is.
Environment & tools Assistant Anssi Jääskeläinen Visiting hours: Tuesdays Room: 6606
Unix Continuum of Tools Do something once: use the command line Do something many times: –Use an alias –Use a shell script Do something that is complex.
. Compilation / Pointers Debugging 101. Compilation in C/C++ hello.c Preprocessor Compiler stdio.h tmpXQ.i (C code) hello.o (object file)
What does this program do ? #include int main(int argc, char* argv[]) { int i; printf("%d arguments\n", argc); for(i = 0; i < argc; i++) printf(" %d: %s\n",
Introduction to C Programming Overview of C Hello World program Unix environment C programming basics.
Guide To UNIX Using Linux Third Edition
15213 C Primer 17 September Outline Overview comparison of C and Java Good evening Preprocessor Command line arguments Arrays and structures Pointers.
CS465 - Unix C Programming (cc/make and configuration control)
CSE : Programming in C Instructor: Lei Wang Office: Dreese Lab 474 Office Hour: Friday.
Xin Liu Sep 16, Introduction Xin (Shane) Liu PhD Candidate in Computer Science Research Area: Computer Graphics Tutorial Page: pages.cpsc.ucalgary.ca/~liuxin/CPSC453.
Introduction to C Programming. A Brief History u Created by Dennis Ritchie at AT&T Labs in 1972 u Originally created to design and support the Unix operating.
1 Lab 2: The Unix environment, Using vi, C programming SITE, uOttawa.
Programming Tools gcc make utility Open Source code Static and Shared Libraries gdb Memory debugging tools.
리눅스 : Lecture 5 UNIX 유틸리티 : text editor, compilation (make), …
CPT: Arrays of Pointers/ Computer Programming Techniques Semester 1, 1998 Objectives of these slides: –to illustrate the use of arrays.
Compiling & Debugging Quick tutorial. What is gcc? Gcc is the GNU Project C compiler A command-line program Gcc takes C source files as input Outputs.
Computer Engineering 1 nd Semester Dr. Rabie A. Ramadan 2.
C Tutorial Session #2 Type conversions More on looping Common errors Control statements Pointers and Arrays C Pre-processor Makefile Debugging.
Some Basics && GDB overview Ram Sheshadri –
1 Lab 2 “Hello world” in Unix/Linux #include "std_lib_facilities_4.h" int main(){ cout
C Tutorial - Program Organization CS Introduction to Operating Systems.
C Programming in Linux Jacob Chan. C/C++ and Java  Portable  Code written in one system and works in another  But in C, there are some libraries that.
CSE 232: C++ debugging in Visual Studio and emacs C++ Debugging (in Visual Studio and emacs) We’ve looked at programs from a text-based mode –Shell commands.
Operating Systems CMPSC 473 Processes August 31, Lecture 3 Instructor: Bhuvan Urgaonkar.
Algorithms  Problem: Write pseudocode for a program that keeps asking the user to input integers until the user enters zero, and then determines and outputs.
Makefiles. Multiple Source Files (1) u Obviously, large programs are not going to be contained within single files. u C provides several techniques to.
(language, compilation and debugging) David 09/16/2011.
Data Display Debugger (DDD)
1 CS503: Operating Systems Spring 2014 Part 0: Program Structure Dongyan Xu Department of Computer Science Purdue University.
COP 3530 Spring2012 Data Structures & Algorithms Discussion Session Week 2.
Makefiles CARYL RAHN. Separate compilation Large programs are generally separated into multiple files, e.g. main.c addmoney.c removemoney.c money.h With.
What we will cover A crash course in the basics of C “Teach yourself C in 21 days”
COP 3530 Spring 12 Discussion Session 1. Agenda 1.Introduction 2.Remote programming 3.Separate code 4.Compile -- g++,makefile 5.Debug -- gdb 6.Questions?
Multiple File Compilation and linking By Bhumik Sapara.
1 CS 416- Fall 2008 Session 02 TA: Tuan Phan (ext 9644) : Just leaving msg( prefer using ,
C P ROGRAMMING T OOLS. C OMPILING AND R UNNING S INGLE M ODULE P ROGRAM.
Introduction to C Zhengwei Yang CSC2100 Data Structures Tutorial 1.
Advanced UNIX progamming Fall 2002 Instructor: Ashok Srinivasan Lecture 2 Class web site:
CMSC 104, Version 8/061L14AssignmentOps.ppt Assignment Operators Topics Increment and Decrement Operators Assignment Operators Debugging Tips Reading Section.
CS252: Systems Programming Ninghui Li Based on Slides by Gustavo Rodriguez-Rivera Topic 2: Program Structure and Using GDB.
CSE 303 Concepts and Tools for Software Development Richard C. Davis UW CSE – 10/11/2006 Lecture 7 – Introduction to C.
Sung-Dong Kim Dept. of Computer Engineering, Hansung University Chapter 3 Programming Tools.
Institute of Radio Physics and Electronics ILug-Cal Introduction to GDB Institute of Radio Physics and Electronics and Indian GNU/Linux Users Group Kolkata.
CSCI 4061 Recitation 2 1.
Winter 2009 Tutorial #6 Arrays Part 2, Structures, Debugger
How to Start Programming in Linux Environment
Prof: Dr. Shu-Ching Chen TA: Hsin-Yu Ha Fall 2015
C/C++ Tutorial.
C Primer.
Command Line Arguments
Makefiles Caryl Rahn.
Computer Engineering 1nd Semester
C programming language
Computer Systems and Networks
Makefile Tutorial CIS5027 Prof: Dr. Shu-Ching Chen
Prof: Dr. Shu-Ching Chen TA: Yimin Yang
Prof: Dr. Shu-Ching Chen TA: Hsin-Yu Ha
Govt. Polytechnic,Dhangar
Introduction to C Topics Compilation Using the gcc Compiler
CMSC 202 Additional Lecture – Makefiles
C Programming Getting started Variables Basic C operators Conditionals
Introduction to C Topics Compilation Using the gcc Compiler
Appendix F C Programming Environment on UNIX Systems
Debugging.
Makefiles, GDB, Valgrind
15213 C Primer 17 September 2002.
Presentation transcript:

CS 211 Recitation 1 TA: Ishani Chakraborty

Admin stuff Office hours: 5:00-6:00 pm, Tuesdays at Core Bldg, room 246. Switching recitations is allowed, missing them is not ! Please follow the instructions for turning in projects. Check the course webpage every day. -

Programming review Programming paradigms –Procedural(C)– programming task broken into Variables Data structures PROCEDURES –Object oriented (Java) – task broken into OBJECTS

Sample C program /*The first C program*/ #include int main(void) { printf(“hello, world!\n"); return 0; }

Editing –How do you write the code? Popular editors: vi, emacs… Compiling –How to convert source code to executable code? We use gcc (GNU Compiler Collection) as the compiler.

Compiling code $ gcc hello.c –creates file a.out –Execute by typing $./a.out $ gcc –o hello hello.c –Creates file hello –Execute by typing $./hello make hello

The three steps $ vi hello.c –Write code, save, exit. $ gcc –o hello hello.c $./hello

vi editor: Quickstart $ vi hello.c Press i (for inserting text) Start writing code When finished, press escape key (to exit editing mode) Type $ :wq to save and exit Type $ :q! to quit without saving changes

vi editor: Quickstart To make any changes eg delete, cut, copy etc. exit the editing mode by pressing the escape key. xDelete a character ddDelete a line yyCopy a line pPaste a copied line

Emacs editor $ emacs hello.c –A good tutorial link –

Example of Makefile Project1: Project1.o Inventory.o Cd.o Date.o gcc -o proj1 Project1.o Inventory.o Cd.o Date.o Project1.o: Project1.c Inventory.h gcc -c Project1.c Inventory.o: Inventory.c Inventory.h Cd.h gcc -c Inventory.c Cd.o: Cd.c Cd.h Date.h gcc -c Cd.c Date.o: Date.c Date.h gcc -c Date.c Example taken from course webpage of 341 from

Multiple files A complex program can and should be broken into multiple source files. To illustrate we look at a simple program with 4 files –main.c –hello.c –factorial.c –functions.h

#include #include "functions.h“ int main(void) { print_hello(); printf("\n"); printf("The factorial of 5 is %d\n",factorial(5)); return 0; } #include #include "functions.h“ void print_hello() { printf("Hello World!\n"); } #include "functions.h" int factorial(int n) { if(n!=1){ return(n * factorial(n-1)); } else return 1; } void print_hello(void); int factorial(int n); main.c functions.h factorial.c hello.c

$ gcc main.c $ gcc –c main.c

gcc-ing with –c flag creates an object file that is independent of other files. gcc-ing without –c flag = compiling and linking files, so interdependent files have to be compiled together. $ gcc main.c hello.c factorial.c –o hello

Makefile Compilation of several programs whose files are dependent on one other ? –When making changes to one(few) file(s), several files remain unchanged so don’t need to compile them. –make recompiles only those files that need to be updated (Looks at the timestamps of source files, if source files newer than object files, recompile.)

Makefile Set of rules target: dependencies [tab] system command myprog.o: myprog.c myprog.h gcc –o myprog myprog.c TargetDependency list [tab]Action(s) to create the target

Makefile-1 all: gcc -o hello main.c hello.c factorial.c

Makefile-2 all: hello hello: main.o factorial.o hello.o gcc main.o factorial.o hello.o –o hello main.o: main.c gcc -c main.c factorial.o: factorial.c gcc -c factorial.c hello.o: hello.c gcc -c hello.c

Makefile-3 # Variable CC will be the compiler to use. CC=gcc # Set the CFLAGS options to be passed to the compiler. CFLAGS=-c –Wall all: hello hello: main.o factorial.o hello.o $(CC) main.o factorial.o hello.o -o hello main.o: main.c $(CC) $(CFLAGS) main.c factorial.o: factorial.c $(CC) $(CFLAGS) factorial.c hello.o: hello.c $(CC) $(CFLAGS) hello.c

Admin stuff Recitation ppts uploaded at: Project 1 due: 16 th Feb ’09. Single tar file –If your files are in folder called project1 use the command tar -cvf project1.tar project1

Debugging Bug: Error/fault in a computer program producing incorrect/unexpected results. A simple debugger: print statements in code. –Code needs recompilation every time. –Guess the source of the problem. –Impractical in complex programs. Debuggers are powerful and flexible.

Factorial function (fact.c) #include int factorial(int); void main(void) { int number; printf("Please enter a positive integer: “); scanf("%d",&number); if (number < 0) printf("That is not a positive integer.\n”); else printf(“The factorial of %d is %d“,number,factorial(number)); } int factorial(int number) { int temp; if(number <= 1) return 1; temp = number * factorial(number - 1); return temp; }

gdb basics GNU debugger –gcc –g source –gdb executable –run –list –break –step –backtrace –print

list

So we have written, compiled, ran and debugged a code. Now we will focus on what goes into a code, namely data and their structures. Data structures eg arrays, structures, unions, stacks, queues, trees etc…

Arrays Group of elements accessed by indexing. Elements have same data type. Placed in contiguous memory locations.

Arrays char foo[80]; – An array of 80 characters – sizeof(foo) = 80 × sizeof(char) = 80 × 1 = 80 bytes int bar[40]; – An array of 40 integers – sizeof(bar) = 40 × sizeof(int) = 40 × 4 = 160 bytes

Nested Arrays 2D array A int A[4][3] Space required to store A ?

A[0][0] A[0][1] A[0][2] A[1][0] A[1][1] A[1][2] A[2][0] A[2][1] A[2][2] A[3][0] A[3][1] A[3][2] xAxA x A + 4 x A + 8 x A + 12 x A + 16 x A + 20 x A + 24 x A + 28 x A + 32 x A + 36 x A + 40 x A + 44 ElementAddress A[0][0]A[0][1]A[0][2] A[1][0]A[1][1]A[1][2] A[2][0]A[2][1]A[2][2] A[3][0]A[3][1]A[3][2] int A[4][3] = { {1, 2, 3}, {4, 5, 6}, {7,8, 9}, {10, 11, 12} };

Structures Heterogeneous data structure: – Elements can have different data types. Contiguous memory locations.

structures #include struct data { char name; struct { int age; int height; } s; }; int main(void) { struct data cs211; cs211.name = ‘H’; cs211.s.age = 21; printf("%c is %d years old\n", cs211.name,cs211.s.age); return 0; }

What does this program do ? #include #define ITERATIONS 20 int main() { int num[3]={0,1}; int count1, count2; printf("%d %d ", num[0], num[1]); for (count1=0; count1 < ITERATIONS-2; count1++) { num[2] = num[0] + num[1]; printf("%d ",num[2]); for (count2 =0; count2 < 2; count2++) num[count2] = num[count2+1]; } printf(“\n”); return 0; }

What does this program do ? #include int main(int argc, char* argv[]) { int i; printf("%d arguments\n", argc); for(i = 0; i < argc; i++) printf(" %d: %s\n", i, argv[i]); return 0; }