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.

Slides:



Advertisements
Similar presentations
Dynamic Memory Allocation in C.  What is Memory What is Memory  Memory Allocation in C Memory Allocation in C  Difference b\w static memory allocation.
Advertisements

Chapter 7 Process Environment Chien-Chung Shen CIS, UD
CS162B: POSIX Threads Jacob Chan. Objectives ▪ Review on fork() and exec() – Some issues on forking and exec-ing ▪ POSIX Threads ▪ Lab 8.
C Programming - Lecture 5
Kernighan/Ritchie: Kelley/Pohl:
Memory allocation CSE 2451 Matt Boggus. sizeof The sizeof unary operator will return the number of bytes reserved for a variable or data type. Determine:
1 Pointers A pointer variable holds an address We may add or subtract an integer to get a different address. Adding an integer k to a pointer p with base.
Pointers Discussion 5 Section Housekeeping HW 1 Issues Array Issues Exam 1 Questions? Submitting on Time!
C For Java Programmers Tom Roeder CS sp. Why C? The language of low-level systems programming  Commonly used (legacy code)  Trades off safety.
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.
Functions Definition: Instruction block called by name Good design: Each function should perform one task and do it well Functions are the basic building.
Pointers. Addresses in Memory When a variable is declared, enough memory to hold a value of that type is allocated for it at an unused memory location.
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",
Pointers and Arrays C and Data Structures Baojian Hua
C Primer CAS CS210 Ying Ye Boston University. Outline Hello, world Basics in C Comparison of C and Java.
1 CSE 303 Lecture 8 Intro to C programming read C Reference Manual pp. Ch. 1, , 2.6, 3.1, 5.1, , , , Ch. 8 ; Programming.
1 Pointers, Dynamic Data, and Reference Types Review on Pointers Reference Variables Dynamic Memory Allocation –The new operator –The delete operator –Dynamic.
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.
1 Procedural Concept The main program coordinates calls to procedures and hands over appropriate data as parameters.
Arrays and Pointers in C Alan L. Cox
CSE 332: C++ program structure and development environment C++ Program Structure (and tools) Today we’ll talk generally about C++ development (plus a few.
CS162B: Assembly and C Jacob T. Chan. Objectives ▪ System calls ▪ Relation of System calls to Assembly and C ▪ Special System Calls (exit, write, print,
University of Calgary – CPSC 441. C PROGRAM  Collection of functions  One function “main()” is called by the operating system as the starting function.
Outline Midterm results Static variables Memory model
CPT: Arrays of Pointers/ Computer Programming Techniques Semester 1, 1998 Objectives of these slides: –to illustrate the use of arrays.
CMPSC 16 Problem Solving with Computers I Spring 2014 Instructor: Tevfik Bultan Lecture 12: Pointers continued, C strings.
Comp 245 Data Structures Linked Lists. An Array Based List Usually is statically allocated; may not use memory efficiently Direct access to data; faster.
David Notkin Autumn 2009 CSE303 Lecture 12 October 24, 2009: Space Needle.
6. More on Pointers 14 th September IIT Kanpur C Course, Programming club, Fall
Computer Science and Software Engineering University of Wisconsin - Platteville 2. Pointer Yan Shi CS/SE2630 Lecture Notes.
File ▪ File – Unit of logical storage – Aid in manipulating exact sector of file data ▪ Abstract view of secondary physical storage devices ▪ Without files.
Dynamic Memory Allocation. Domain A subset of the total domain name space. A domain represents a level of the hierarchy in the Domain Name Space, and.
C Programming Day 4. 2 Copyright © 2005, Infosys Technologies Ltd ER/CORP/CRS/LA07/003 Version No. 1.0 More on Pointers Constant Pointers Two ways to.
Weeks 5-6 Pointers and Arrays Basic pointer type Pointers and Arrays Address arithmetic Pointer Arrays User-defined data types Structures Unions Pointers.
Topic 3: C Basics CSE 30: Computer Organization and Systems Programming Winter 2011 Prof. Ryan Kastner Dept. of Computer Science and Engineering University.
Functions & Pointers in C Jordan Erenrich
Pointers *, &, array similarities, functions, sizeof.
Computer Organization and Design Pointers, Arrays and Strings in C Montek Singh Sep 18, 2015 Lab 5 supplement.
What we will cover A crash course in the basics of C “Teach yourself C in 21 days”
CSE 374 Programming Concepts & Tools Hal Perkins Fall 2015 Lecture 7 – Introduction to C: The C Level of Abstraction.
Arrays, Strings, and Memory. Command Line Arguments #include int main(int argc, char *argv[]) { int i; printf("Arg# Contents\n"); for (i = 0; i < argc;
Announcements Assignment 1 due Wednesday at 11:59PM Quiz 1 on Thursday 1.
1 Recall that... char str [ 8 ]; str is the base address of the array. We say str is a pointer because its value is an address. It is a pointer constant.
Multi-dimensional Arrays and other Array Oddities Rudra Dutta CSC Spring 2007, Section 001.
1 C Basics Monday, August 30, 2010 CS 241. Announcements MP1, a short machine problem, will be released today. Due: Tuesday, Sept. 7 th at 11:59pm via.
C Primer Session – 1/25/01 Outline Hello World Command Line Arguments Bit-wise Operators Dynamic Memory / Pointers Function Parameters Structures.
MORE POINTERS Plus: Memory Allocation Heap versus Stack.
CSE 303 Concepts and Tools for Software Development Richard C. Davis UW CSE – 10/11/2006 Lecture 7 – Introduction to C.
C Tutorial - Pointers CS 537 – Introduction to Operating Systems.
Chapter 7 Process Environment Chien-Chung Shen CIS/UD
DYNAMIC MEMORY ALLOCATION. Disadvantages of ARRAYS MEMORY ALLOCATION OF ARRAY IS STATIC: Less resource utilization. For example: If the maximum elements.
CSE 220 – C Programming malloc, calloc, realloc.
Dynamic Allocation in C
Computer Organization and Design Pointers, Arrays and Strings in C
Winter 2009 Tutorial #6 Arrays Part 2, Structures, Debugger
C Primer.
A bit of C programming Lecture 3 Uli Raich.
Program Execution in Linux
C Basics.
14th September IIT Kanpur
Pointers, Dynamic Data, and Reference Types
Dynamic Memory Allocation
Program Execution in Linux
C Programming - Lecture 5
15213 C Primer 17 September 2002.
EE 312 Exam I Review.
Pointers, Dynamic Data, and Reference Types
CSE 303 Concepts and Tools for Software Development
EE 312 Exam I Review.
Presentation transcript:

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 need to be run on a certain environment  Pthred not in Windows  Similarity in programming syntax  Looping, conditionals, switches, functions, arrays, declarations/assignments (different in C, but almost similar in C++)

Differences Between C/C++ and Java  C is faster  JVM has a write-once, read anywhere code (this adds overhead)  Games are usually written in C/C++  Java is easier to learn  C/C++ is hard to grasp because of POINTERS!  Java uses pointers, but they are blackboxed  Every object in Java is a pointer  Alternative to Java: C#

Differences Between C/C++ and Java  C/C++ is more prone to runtime errors  Declaring new variables in C/C++ usually has garbage values  In Java, they are automatically set them to 0, null, or default value  POINTERS  Segmentation faults  Memory allocation is sometimes manually set by programmer  a[10] with 5 elements will return garbage values for the next 5  Memory errors will print out a segmentation fault error  Effect: CRASH!!

Differences Between C/C++ and Java  Object-oriented-ness of Java  Java is object-oriented, so everything is stored in an object  C is more on procedural  There are no classes in C, but there are structs  C++ is a mix of both object-oriented and procedural programming  What you can do in C, you can also do in C++ (in most cases)

Differences Between C/C++ and Java  Why is Java taught first here?  Debate over Java vs C/C++ (Java won)  C/C++ needs to be taught still because it is better in some cases than Java  Implementation wise, packages in java are more functional than libraries in C/C++  C/C++ needs external libraries (OpenGL, OpenCV)  Java is “younger” and more standardized than C/C++  Packages

First C Program (Since we will use this in class) #include int main(void) { printf("Hello, world!\n"); return 0; } // Save this program as hello.c

First C Program (Since we will use this in class)  Then, compile the program with: gcc –o hello hello.cpp  And run it as:./hello  The./ is required in Linux  In Windows, it’s okay to ignore this already

Exercise  Try printing out command line arguments int main(int argc, char* argv[]) { int I; for(i = 0; i < argc; i++) { printf("arg %s\n",argv[i]); } return 0; // non-zero means an error }

Exercise  Try adding this line after printf: printf("%c\n", argv[i][0]); printf("%s\n", argv[i]+1);  What happens?

More on POINTERS  Any variable type can be a pointer type  Java declares everything as a pointer int a; int *b; //int pointer b = &a; // & (used for accessing memory address of variable) a = 5; printf(“%d\n”,*a); //what happens?

Functions  Same as creating methods in Java, but needs to have been declared first before being used void fxn(){ printf(“I am at fxn\n”); } Int main(){ fxn(); return 0; }

Functions  C/C++ has this feature called prototyping (allows functions to be declared before being used) void fxn(); int main() { fxn(); return 0; } void fxn() { printf(“I am fxn\n”); }

Functions: Exercise  What is the difference between these two swap methods? void swap(int a, int b){ int temp; temp = a; a = b; b = temp; } void swap (int *a, int *b) { int temp; temp = *a; *a = *b; *b = temp; }

Inputting in Console printf () – to print out in console scanf () – to input in console int main(){ char aChar; printf("\nEnter a character:"); scanf("%c", &aChar); printf(“Character is %c”, aChar); }

Array assignment with malloc() and free()  Normal array assignment int i[20]; //create an array of int with 20 elements  Heap array assignment int a[] = malloc(sizeof(int) * 20); //create a heap  Difference: dynamic vs static memory (because or realloc() )  Deallocate memory (needed after memory is used. Otherwise, there will be a MEMORY LEAK) free(a);

LAB 2: Sorting algorithm  Write a program sort.c which sorts a list of names  I should be able to run this command:./sort  The first line consists of the number of elements in the list  Then the program should be able to print out the sorted list  The entire thing should be able to sort the names by first name. (to make things simpler) Example input: 4 Jack Frost Jake Long Jane Doe Johnny Appleseed  Make sure that each line should be read as well

LAB 2: Sorting algorithm  Sample first lines: int N; //number of elements in list char *name[]; //string = char * in C char ipline[128]; //read each line scanf("%d\n", &N); name = (char **)malloc(sizeof(char *) * N); for(j = 0; j < N; ++j) { gets(ipline); name[j] = (char *) malloc(strlen(ipline)+1); strcpy(name[j], ipline); } //sort //printout sorted array //free

LAB 2: Sorting algorithm  Of course, I will have to check if you have a CERTIFICATE OF AUTHORSHIP (failure to do so will null and void your lab)  File name: CS162B_Lab2_ _ _.tar  Submit on the link provided for your section. Failure to do so will incur a -10  Deadline: 11:55pm

Next Meeting  More on C  Reading files  Using file pointers  Other features of C

THE END “Faith is taking the first step even if you do not see the whole staircase”