Introduction to C Programming in Unix Environment - I Abed Asi Extended System Programming Laboratory (ESPL) CS BGU Fall 2013/2014.

Slides:



Advertisements
Similar presentations
CSE 105 Structured Programming Language (C)
Advertisements

Copyright 2013 – Noah Mendelsohn Compiling C Programs Noah Mendelsohn Tufts University Web:
Making Choices in C if/else statement logical operators break and continue statements switch statement the conditional operator.
Introduction to C Programming in Unix Environment - II Abed Asi Extended System Programming Laboratory (ESPL) CS BGU Fall 2014/2015 Some slides.
Compilation & linkage.h read.h.c read.c.c main.c.c list.c.h list.h prog1 Linkage: g++ read.o main.o list.o –o prog1.o main.o.o list.o.o read.o Compilation:
Wednesday, 9/4/02, Slide #1 1 CS 106 Intro to CS 1 Wednesday, 9/4/02  Today: Introduction, course information, and basic ideas of computers and programming.
. Compilation / Pointers Debugging 101. Compilation in C/C++ hello.c Preprocessor Compiler stdio.h tmpXQ.i (C code) hello.o (object file)
1 Lecture 1  Getting ready to program  Hardware Model  Software Model  Programming Languages  The C Language  Software Engineering  Programming.
. Plab – Tirgul 3 Makefiles, Memory errors, Variables’ scope.
CS-341 Dick Steflik Introduction. C++ General purpose programming language A superset of C (except for minor details) provides new flexible ways for defining.
Introduction to C Programming Overview of C Hello World program Unix environment C programming basics.
Plab – Tirgul 3 Makefiles, Libraries, Debugging and Common Bugs.
Guide To UNIX Using Linux Third Edition
COMP 14: Intro. to Intro. to Programming May 23, 2000 Nick Vallidis.
Starting Out with C++: Early Objects 5/e © 2006 Pearson Education. All Rights Reserved Starting Out with C++: Early Objects 5 th Edition Chapter 1 Introduction.
CMSC 104, Version 9/011 Introduction to C Topics Compilation Using the gcc Compiler The Anatomy of a C Program 104 C Programming Standards and Indentation.
Introduction To C++ Programming 1.0 Basic C++ Program Structure 2.0 Program Control 3.0 Array And Structures 4.0 Function 5.0 Pointer 6.0 Secure Programming.
Unix Processes Slides are based upon IBM technical library, Speaking Unix, Part 8: Unix processes Extended System Programming Laboratory (ESPL) CS Department.
Computer Science 210 Computer Organization Introduction to C.
CSC 125 Introduction to C++ Programming Chapter 1 Introduction to Computers and Programming.
Introduction COMP104: Fundamentals and Methodology.
CIS 199 Test 01 Review. Computer Hardware  Central Processing Unit (CPU)  Brains  Operations performed here  Main Memory (RAM)  Scratchpad  Work.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 1: Introduction to Computers and Programming.
Java Introduction Lecture 1. Java Powerful, object-oriented language Free SDK and many resources at
1 Agenda Administration Background Our first C program Working environment Exercise Memory and Variables.
Creating your first C++ program
Programming With C.
Introduction to Computer Systems and the Java Programming Language.
CS 390 Unix Programming Summer Unix Programming - CS 3902 Course Details Online Information Please check.
CMSC 104, Version 9/011 Introduction to C Topics Compilation Using the gcc Compiler The Anatomy of a C Program 104 C Programming Standards and Indentation.
Fundamental Programming: Fundamental Programming K.Chinnasarn, Ph.D.
Introduction to Perl Yupu Liang cbio at MSKCC
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.
Introduction to Programming
CS2204: Introduction to Unix January 19 th, 2004 Class Meeting 1 * Notes adapted by Christian Allgood from previous work by other members of the CS faculty.
Data Display Debugger (DDD)
Topic 3: C Basics CSE 30: Computer Organization and Systems Programming Winter 2011 Prof. Ryan Kastner Dept. of Computer Science and Engineering University.
Copyright 2003 Scott/Jones Publishing Standard Version of Starting Out with C++, 4th Edition Chapter 1 Introduction to Computers and Programming.
Introduction to Assembly II Abed Asi Extended System Programming Laboratory (ESPL) CS BGU Fall 2013/2014.
1 CSC2100B Data Structure Tutorial 1 Online Judge and C.
CPS120: Introduction to Computer Science Compiling a C++ Program From The Command Line.
Announcements Assignment 1 due Wednesday at 11:59PM Quiz 1 on Thursday 1.
Brief Version of Starting Out with C++ Chapter 1 Introduction to Computers and Programming.
C Part 1 Computer Organization I 1 August 2009 © McQuain, Feng & Ribbens A History Lesson Development of language by Dennis Ritchie at Bell.
Revisiting building. Preprocessing + Compiling 2 Creates an object file for each code file (.c ->.o) Each.o file contains code of the functions and structs.
L071 Introduction to C Topics Compilation Using the gcc Compiler The Anatomy of a C Program Reading Sections
Welcome to C and Computer Structure #include int main() { printf ("hello class\n"); return 0; }
1 Building a program in C: Preprocessor, Compilation and Linkage.
Multiple file project management & Makefile
Introduction to C Topics Compilation Using the gcc Compiler
Content Coverity Static Analysis Use cases of Coverity Examples
Lecture 1b- Introduction
UMBC CMSC 104 – Section 01, Fall 2016
The Machine Model Memory
Computer Science 210 Computer Organization
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.
Introduction to C Topics Compilation Using the gcc Compiler
Introduction to C Topics Compilation Using the gcc Compiler
C Basics.
Computer Systems and Networks
Programming COMP104: Fundamentals and Methodology Introduction.
Computer Science 210 Computer Organization
Computer Science I CSC 135.
Introduction to Static Analyzer
CSE 303 Concepts and Tools for Software Development
Appendix F C Programming Environment on UNIX Systems
Introduction to Computer Science
Introduction to C Topics Compilation Using the gcc Compiler
ICS103 Programming in C 1: Overview of Computers And Programming
SPL – PS1 Introduction to C++.
Presentation transcript:

Introduction to C Programming in Unix Environment - I Abed Asi Extended System Programming Laboratory (ESPL) CS BGU Fall 2013/2014

 Lecturer  Abed Asi  Lab assistants  David Tolpin – – Monday 16:00 –  Abed Asi – Wednesday 16:00 – 19:00  Office hours  Sunday 14:00 – 16:00 (-102/37)  Website  Abed Asi - ESPL

 Lectures and Labs  One-hour lecture every 2 weeks (with two exceptions)  12 (or 11) Lab sessions  Quizzes  2 Quizzes  NO final exam  Grading policy  40% for quizzes (20% per quiz)  60% Labs (5% per lab) 3 Abed Asi – ESPL

 Abed Asi – ESPL

When ?TopicLecture October 20, 2013 Introduction to C Programming in Unix Environment - I 1 October 27, 2013 Introduction to C Programming in Unix Environment - II 2 November 3, 2013Introduction to Assembly3 November 17, 2013Functions and System Calls (Assembly)4 December 8, 2013Unix Processes5 December 15, 2013Programs Execution6 December 22, 2013Introduction to script languages (Python)7 January 5, 2014Web programming8 Abed Asi - ESPL 5

 Unix environment  Data types in C  C – simple programs  C Arrays – A Glance  Makefiles  Java vs. C Abed Asi - ESPL 6

 A program that starts up when you turn on your computer and runs underneath all other programs  It is a manager. It manages all the available resources on a computer, from the CPU, to memory, to hard disk accesses  Tasks the operating system must perform  Control Hardware - attempts to get everything working together  Run Applications – running applications software such as: word processors, web browsers, games, etc...  Memory Management - controls the mapping between logical memory and the hard disk, managing file system and more.. 7 Abed Asi - ESPL

 The UNIX operating system was born in the late 1960s.  It originally began as a one man project led by Ken Thompson of Bell Labs  Grown to become one of the most widely used operating system  It has gone through many different generations and even mutations  Some differ substantially from the original version, like Berkeley Software Distribution (BSD) or Linux.  Others, still contain major portions that are based on the original source code. Abed Asi - ESPL 8

 The Kernel - handles memory management, input and output requests, and program scheduling. Technically speaking, the kernel is the OS.  The Shell - basic UNIX shells provides a “command line” interface which allows the user to type in commands.  The Built-in System Utilities - are programs that allow a user to perform tasks which involve complex actions such as listing the content of directories, move & copy files, remove files, etc...  Application Software & Utilities – additional programs that are bundled with the OS distribution, or available separately. There are not part of UNIX Abed Asi - ESPL 9

 To develop your own program you need:  Compiler: translates your programming language to assembly  Text editor: to type down source code  Project management tool: dependencies, linking, executable..  In Unix:  You will use the gcc compiler  Any text editor that you prefer (Kate, Emacs, KWrite, …)  make as the project management tool  Use Unix Manual for function/program description and moreUnix Manual Abed Asi - ESPL 10

 Char  char is the basic type in C ▪ sizeof(char) = 1 byte by definition  Examples: ▪ char c = ‘A’; ▪ char c = 65; Abed Asi - ESPL 11

 int, short, long  sizes: machine dependent! ▪ sizeof(short) <= sizeof(int) <= sizeof(long)  For example: (linux on pentium) ▪ short: 16 bit, int: 32 bit, long: 32 bit  All can be signed/unsigned ▪ Default: signed ▪ unsigned int: [0, ] ▪ signed int: [-2 31, ] Abed Asi - ESPL 12

 Boolean types  Doesn’t exist in C !  Use char/int instead  zero = false  non-zero = true Abed Asi - ESPL 13 while (1) { … } (infinite loop) if (-1974) {.. } (true statement)

#include int main() { int i; // declares i as an integer int j = 0; // declares j as an integer, and initializes it to 0 // for( initial ; test condition ; update step ) for( i = 0; i < 10; i++ ) { j += i; // shorthand for j = j + i printf("%d %d %d\n", i, j, (i*(i+1))/2); } return 0; } Abed Asi - ESPL 14

> gcc –Wall loop.c –o loop > loop Abed Asi - ESPL 15

#include /* count number of words in sentence */ int main(int argc, char **argv) { int i; printf("There are %d words in phrase '", argc-1); for(i=1; i!=argc; ++i) { printf("%s", argv[i]); if(i!=argc-1) printf(" "); } printf("'.\n"); return 0; } Abed Asi - ESPL 16

$> prog –u danny –p 1234 argc = 5 argv[0] = “prog” argv[1] = “-u”... argv[4] = “1234” Always: argv[5] = 0 Abed Asi - ESPL 17

 C does not provide array operations int a[4]; int b[4];... a = b; // illegal if( a == b ) // illegal  int arr[5][7];  5 rows, 7 columns  continuous memory (divided to 5 blocks)  access: arr[row][col] = 0; Abed Asi - ESPL 18

 C does not provide any run time checks  This will compile and run (no errors)  But can lead to unpredictable results  It is the programmer’s responsibility to check whether the index is out of bounds… Abed Asi - ESPL 19 int a[4]; a[-1] = 0; a[4] = 0;

int arr[3] = {3, 4, 5}; // Good int arr[] = {3, 4, 5}; // Good - The same int arr[4] = {3, 4, 5}; // Bad style - The last is 0 int arr[2] = {3, 4, 5}; // Bad int arr[2][3] = {{2,5,7},{4,6,7}}; // Good int arr[2][3] = {2,5,7,4,6,7}; // Good - The same int arr[3][2] = {{2,5,7},{4,6,7}}; // Bad int arr[3]; arr = {2,5,7}; // Bad - array assignment only in initialization Abed Asi - ESPL 20

 Takes input C-code and produces machine code (object file)  gcc –c Main.c –o Main.o  Main.c  Main.o  The object file does not contain all external references  It leaves names, such as “printf”, “area”, etc. as undefined references Abed Asi - ESPL 21

 Combines several object files into an executable file  No unresolved references Abed Asi - ESPL 22 Main Preprocessor Compiler Square.c Square.o Main.c Main.o Linker libc.a

$ gcc –c Square.c –o Square.o $ gcc –c Main.c –o Main.o $ gcc Square.o Main.o –o Main Abed Asi - ESPL 23 Main Preprocessor Compiler Square.c Square.o Main.c Main.o Linker libc.a

 What is it good for ?  Automatic tool for projects management  Less boring work for the programmer  Less errors Abed Asi - ESPL 24 mywc: mywc.c gcc -Wall mywc.c -o mywc

Abed Asi - ESPL 25 prog1 read.cread.h list.clist.hmain.c read.o main.o list.o Makefile prog1: read.o main.o list.o gcc main.o read.o list.o –o prog1 main.o: main.c read.h list.h gcc -c main.c read.o: read.c read.h gcc -c read.c list.o: list.c list.h gcc -c list.c $ make prog1

Abed Asi - ESPL 26 prog1 read.cread.h list.hlist.cmain.c read.o main.o list.o  If only one file is modified, will we have to recompile all over again?  No, the Makefile uses the dependencies tree

Abed Asi - ESPL 27 prog1 read.cread.h list.hlist.cmain.c read.o main.o list.o  If read.h is modified, what should be done?  We have to recreate a subset of the files

 Aim: build only out-of-date files (use timestamps)  Makefile contains:  List of dependencies (no cycles)  “Recovery” scenario when any file is modified  If any of the files [main.c, list.h, read.h] wad modified after main.o, the command “gcc –c main.c” will be invoked  By default, make only executes the first target in the makefile Abed Asi - ESPL28 main.o: main.c list.h read.h gcc -c main.c

Read about the explicit and implicit rules for Makefiles Abed Asi - ESPL 29

Abed Asi - ESPL 30 JavaC קומפילציה byte-code, write once read everywhere native code מרכיבים בסיסיים method, classfunction, structure מודול foo.javafoo.c, foo.h ניהול זכרון new, GCmalloc, calloc, free גישה ישירה, כתובת מצביע, void * מערכת טיפוסים if(a = 1) errorif(a = 1) ok עיבוד מחזורות String, StringBufferchar *s, #include ספריה מובנת java.lang#include קלט ופלט java.io#include פונקציות מתמטיות java.lang.Math#include