C Primer.

Slides:



Advertisements
Similar presentations
C Intro.
Advertisements

Hello World Program The source code #include int main() { printf("Hello World\n"); return(0); }
C Programming Day 1 based upon Practical C Programming by Steve Oualline CS550 Operating Systems.
More Pointers Write a program that: –Calls a function to input an integer value –The above function calls another function that will double the input value.
Introduction to C Programming in Unix Environment - II Abed Asi Extended System Programming Laboratory (ESPL) CS BGU Fall 2014/2015 Some slides.
1 Day 03 Introduction to C. 2 Memory layout and addresses r s int x = 5, y = 10; float f = 12.5, g = 9.8; char c = ‘r’, d = ‘s’;
CSSE 332 Functions, Pointers in C. 2 Functions - why and how ? If a problem is large If a problem is large Modularization – easier to: Modularization.
C For Java Programmers Tom Roeder CS sp. Why C? The language of low-level systems programming  Commonly used (legacy code)  Trades off safety.
CSSE221: Software Dev. Honors Day 27 Announcements Announcements Projects turned in? Projects turned in? The 2 required Angel surveys are due by 9 pm tonight.
Functions Definition: Instruction block called by name Good design: Each function should perform one task and do it well Functions are the basic building.
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",
C Primer CAS CS210 Ying Ye Boston University. Outline Hello, world Basics in C Comparison of C and Java.
1 Pointers, Dynamic Data, and Reference Types Review on Pointers Reference Variables Dynamic Memory Allocation –The new operator –The delete operator –Dynamic.
15213 C Primer 17 September Outline Overview comparison of C and Java Good evening Preprocessor Command line arguments Arrays and structures Pointers.
Pointers CSE 2451 Rong Shi.
University of Calgary – CPSC 441. C PROGRAM  Collection of functions  One function “main()” is called by the operating system as the starting function.
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.
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.
Functions & Pointers in C Jordan Erenrich
Computer Organization and Design Pointers, Arrays and Strings in C Montek Singh Sep 18, 2015 Lab 5 supplement.
Computer Systems Programming. This course gives you an overview of how computer systems are organized This course provides skills and knowledge of C and.
1 Lecture07: Memory Model 5/2/2012 Slides modified from Yin Lou, Cornell CS2022: Introduction to C.
What we will cover A crash course in the basics of C “Teach yourself C in 21 days”
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;
Advanced Pointer Topics. Pointers to Pointers u A pointer variable is a variable that takes some memory address as its value. Therefore, you can have.
Multi-dimensional Arrays and other Array Oddities Rudra Dutta CSC Spring 2007, Section 001.
C Primer Session – 1/25/01 Outline Hello World Command Line Arguments Bit-wise Operators Dynamic Memory / Pointers Function Parameters Structures.
Chapter 5 Pointers and Arrays Ku-Yaw Chang Assistant Professor, Department of Computer Science and Information Engineering Da-Yeh.
C Tutorial - Pointers CS 537 – Introduction to Operating Systems.
“Success consists of going from failure to failure without loss of enthusiasm.” Winston Churchill.
Pointers to pointers & multi-dimensional arrays
Stack and Heap Memory Stack resident variables include:
Computer Organization and Design Pointers, Arrays and Strings in C
C/C++ Tutorial.
Computer Science 210 Computer Organization
CSE 220 – C Programming Pointers.
Day 03 Introduction to C.
CSE 451 C refresher.
A bit of C programming Lecture 3 Uli Raich.
Command Line Arguments
Day 02 Introduction to C.
An Introduction to C Programming
Day 03 Introduction to C.
C programming language
C Basics.
Intro to C CS 3410 (Adapted from slides by Kevin Walsh
CSCI206 - Computer Organization & Programming
Programmazione I a.a. 2017/2018.
Lecture 6 C++ Programming
Computer Science 210 Computer Organization
14th September IIT Kanpur
Computer Science 210 Computer Organization
C and assembly (motivation)
Pointers Department of Computer Science-BGU יום רביעי 21 נובמבר 2018.
Pointers, Dynamic Data, and Reference Types
بنام خدا زبان برنامه نویسی C (21814( Lecture 11 Pointers
Dynamic Memory Allocation
Memory Allocation CS 217.
Outline Defining and using Pointers Operations on pointers
Pointers The C programming language gives us the ability to directly manipulate the contents of memory addresses via pointers. Unfortunately, this power.
Introduction to Static Analyzer
7. Pointers, Dynamic Memory
Homework Continue with K&R Chapter 5 Skipping sections for now
Chapter 9: Pointers and String
(PART 2) prepared by Senem Kumova Metin modified by İlker Korkmaz
15213 C Primer 17 September 2002.
Pointers, Dynamic Data, and Reference Types
C Tutorial Adapted from Wei Qian’s C tutorial slides.
COMP 2130 Intro Computer Systems Thompson Rivers University
Presentation transcript:

C Primer

Outline Overview comparison of C and Java Good evening Preprocessor Command line arguments Arrays and structures Pointers and dynamic memory

What we will cover A crash course in the basics of C

Like Java, like C Operators same as Java: Syntax same as in Java: Arithmetic i = i+1; i++; i--; i *= 2; +, -, *, /, %, Relational and Logical <, >, <=, >=, ==, != &&, ||, &, |, ! Syntax same as in Java: if ( ) { } else { } while ( ) { } do { } while ( ); for(i=1; i <= 100; i++) { } switch ( ) {case 1: … } continue; break;

Simple Data Types datatype size values char 1 -128 to 127 short 2 -32,768 to 32,767 int 4 -2,147,483,648 to 2,147,483,647 long 4 -2,147,483,648 to 2,147,483,647 float 4 3.4E+/-38 (7 digits) double 8 1.7E+/-308 (15 digits long)

Java programmer gotchas (1) { int i for(i = 0; i < 10; i++) … NOT for(int i = 0; i < 10; i++)

Java programmer gotchas (2) Uninitialized variables catch with –Wall compiler option #include <stdio.h> int main(int argc, char* argv[]) { int i; factorial(i); return 0; }

Java programmer gotchas (3) Error handling No exceptions Must look at return values

“Good evening” #include <stdio.h> int main(int argc, char* argv[]) { /* print a greeting */ printf("Good evening!\n"); return 0; } $ ./goodevening Good evening! $

Breaking down the code #include <stdio.h> int main(…) Include the contents of the file stdio.h Case sensitive – lower case only No semicolon at the end of line int main(…) The OS calls this function when the program starts running. printf(format_string, arg1, …) Prints out a string, specified by the format string and the arguments.

format_string Composed of ordinary characters (not %) Copied unchanged into the output Conversion specifications (start with %) Fetches one or more arguments For example char %c char* %s int %d float %f For more details: man 3 printf

C Preprocessor #define FIFTEEN_TWO_THIRTEEN \ "The Class That Gives CMU Its Zip\n" int main(int argc, char* argv[]) { printf(FIFTEEN_TWO_THIRTEEN); return 0; }

After the preprocessor (gcc –E) int main(int argc, char* argv) { printf("The Class That Gives CMU Its Zip\n"); return 0; }

Conditional Compilation #define CS2733 int main(int argc, char* argv) { #ifdef CS2733 printf("The Best Class You’ll Ever Take!\n"); #else printf("Some other class\n"); #endif return 0; }

After the preprocessor (gcc –E) int main(int argc, char* argv) { printf("The Best Class You’ll Ever Take!\n"); return 0; }

Command Line Arguments (1) int main(int argc, char* argv[]) argc Number of arguments (including program name) argv Array of char*s (that is, an array of ‘c’ strings) argv[0]: = program name argv[1]: = first argument … argv[argc-1]: last argument

Command Line Arguments (2) #include <stdio.h> 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; }

Command Line Arguments (3) $ ./cmdline The Best Class Youll Ever Take 8 arguments 0: ./cmdline 1: The 2: Best 3: Class 4: Youll 5: Ever 6: Take $

Arrays char foo[80]; int bar[40]; 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

Structures Aggregate data #include <stdio.h> struct name { char* name; int age; }; /* <== DO NOT FORGET the semicolon */ int main(int argc, char* argv[]) struct name bovik; bovik.name = "Harry Bovik"; bovik.age = 25; printf("%s is %d years old\n", bovik.name, bovik.age); return 0; }

Pointers Pointers are variables that hold an address in memory. That address contains another variable.

Memory layout and addresses int x = 5, y = 10; float f = 12.5, g = 9.8; char c = ‘c’, d = ‘d’; 5 10 12.5 9. 8 c d 4300 4304 4308 4312 4317 4316

Using Pointers (1) ? 4300 4304 any float any address ? 4300 4304 float f; /* data variable */ float *f_addr; /* pointer variable */ f_addr = &f; /* & = address operator */ ? f f_addr 4300 4304 any float any address ? 4300 f f_addr 4304

Pointers made easy (2) *f_addr = 3.2; /* indirection operator */ float g = *f_addr;/* indirection: g is now 3.2 */ f = 1.3; /* but g is still 3.2 */ f f_addr 4300 4304 3.2 f f_addr 4300 4304 1.3

Function Parameters Function arguments are passed “by value”. What is “pass by value”? The called function is given a copy of the arguments. What does this imply? The called function can’t alter a variable in the caller function, but its private copy. Three examples

Example 1: swap_1 void swap_1(int a, int b) { int temp; temp = a; a = b; b = temp; } Q: Let x=3, y=4, after swap_1(x,y); x =? y=? A1: x=4; y=3; A2: x=3; y=4;

Example 2: swap_2 void swap_2(int *a, int *b) { int temp; temp = *a; *a = *b; *b = temp; } Q: Let x=3, y=4, after swap_2(&x,&y); x =? y=? A1: x=3; y=4; A2: x=4; y=3;

Example 3: scanf #include <stdio.h> int main() { int x; scanf(“%d\n”, &x); printf(“%d\n”, x); } Q: Why using pointers in scanf? A: We need to assign the value to x.

Dynamic Memory Java manages memory for you, C does not C requires the programmer to explicitly allocate and deallocate memory Unknown amounts of memory can be allocated dynamically during run-time with malloc() and deallocated using free()

Not like Java No new No garbage collection You ask for n bytes Not a high-level request such as “I’d like an instance of class String”

malloc Allocates memory in the heap Example Lives between function invocations Example Allocate an integer int* iptr = (int*) malloc(sizeof(int)); Allocate a structure struct name* nameptr = (struct name*) malloc(sizeof(struct name));

free Deallocates memory in heap. Pass in a pointer that was returned by malloc. Example int* iptr = (int*) malloc(sizeof(int)); free(iptr); Caveat: don’t free the same memory block twice!