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

Slides:



Advertisements
Présentations similaires
Primary French Presentation 2 Saying How You Are.
Advertisements

Les Questions dInformation. Information Questions Information questions are open-ended. They request new information and cannot be answered with a simple.
Miguel Garzon CrUise Lab - SITE. Introduction Data Types and Sizes Constants Logic Operators Type conversions Example.
Pthread Ordonnancement. #define _MULTI_THREADED #include #ifndef _CHECK_H #define _CHECK_H /* headers used by a majority of the example program */ #include.
Making PowerPoint Slides Avoiding the Pitfalls of Bad Slides.
PERFORMANCE One important issue in networking is the performance of the network—how good is it? We discuss quality of service, an overall measurement.
1 Case Study 1: UNIX and LINUX Chapter History of unix 10.2 Overview of unix 10.3 Processes in unix 10.4 Memory management in unix 10.5 Input/output.
An Introduction To Two – Port Networks The University of Tennessee Electrical and Computer Engineering Knoxville, TN wlg.
A POWER POINT DEMONSTRATION. The End. Just kidding! This is serious stuff.
IP Multicast Text available on
Template Provided By Genigraphics – Replace This Text With Your Title John Smith, MD 1 ; Jane Doe, PhD 2 ; Frederick Smith, MD, PhD 1,2 1.
Update on Edge BI pricing January ©2011 SAP AG. All rights reserved.2 Confidential What you told us about the new Edge BI pricing Full Web Intelligence.
Les Questions.
Usage Guidelines for Jeopardy PowerPoint Game
CONJUGAISON.
Reflexive verbs and morning routine FR2
Formules en 2 étapes 1MPES4
Infinitive There are 3 groups of REGULAR verbs in French: verbs ending with -ER = 1st group verbs ending with -IR = 2nd group verbs ending with -RE = 3rd.
Speaking Exam Preparation
Connecting with language pupils in the UK
Year 7 French Homework Mme Janickyj
How many young people? Young people take drugs
mardi, le douze septembre
Quantum Computer A New Era of Future Computing Ahmed WAFDI ??????
mardi, le douze septembre
Present Perfect Simple X Present Perfect Continuous.
Conjugating regular –re verbs in French
Conjugating regular –er verbs en français
Les Fruits :.
Theme One Speaking Questions
A level French 8th December 2017.
Exercices: Système d’Information
F RIENDS AND FRIENDSHIP Project by: POPA BIANCA IONELA.
Warm-up Get out a sheet of notebook paper.
P&ID SYMBOLS. P&IDs Piping and Instrumentation Diagrams or simply P&IDs are the “schematics” used in the field of instrumentation and control (Automation)
in French and in English
Essai
High-Availability Linux Services And Newtork Administration Bourbita Mahdi 2016.
C’est quel numéro? Count the numbers with pupils.
The Passé Composé In the previous lesson we looked at the formation of the passé composé (perfect tense) with Avoir verbs. In this lesson we will further.
Le soir Objectifs: Talking about what you do in the evening
Qu’est-ce que tu as dans ta trousse?
Pablo Picasso P____ P_______
Quelle est la date aujourd’hui?
Bonjour !! Hello!! Welcome!! Bienvenue!!! Year 7 Module 1
Qu’est-ce que tu as dans ta trousse?
Français Les animaux (2).
C’est quel numéro? Count the numbers with pupils.
Roots of a Polynomial: Root of a polynomial is the value of the independent variable at which the polynomial intersects the horizontal axis (the function.
Quelle est la date aujourd’hui?
1-1 Introduction to ArcGIS Introductions Who are you? Any GIS background? What do you want to get out of the class?
Question formation In English, you can change a statement into a question by adding a helping verb (auxiliary): does he sing? do we sing? did they sing.
Manometer lower pressure higher pressure P1P1 PaPa height 750 mm Hg 130 mm higher pressure 880 mm Hg P a = h = +- lower pressure 620 mm Hg.
WRITING A PROS AND CONS ESSAY. Instructions 1. Begin your essay by introducing your topic Explaining that you are exploring the advantages and disadvantages.
Y8 Homework (Mr Hodgson)
Making PowerPoint Slides Avoiding the Pitfalls of Bad Slides.
POWERPOINT PRESENTATION FOR INTRODUCTION TO THE USE OF SPSS SOFTWARE FOR STATISTICAL ANALISYS BY AMINOU Faozyath UIL/PG2018/1866 JANUARY 2019.
Basics Concepts of Parallel Programming. What is parallel computing It’s a form of computation where many calculations can be carried out simultaneously.
© by Vista Higher Learning, Inc. All rights reserved.4A.1-1 Point de départ In Leçon 1A, you saw a form of the verb aller (to go) in the expression ça.
By : HOUSNA hebbaz Computer NetWork. Plane What is Computer Network? Type of Network Protocols Topology.
C021TV-I1-S4.
les instructions Bonjour la classe, sortez vos affaires
1 Sensitivity Analysis Introduction to Sensitivity Analysis Introduction to Sensitivity Analysis Graphical Sensitivity Analysis Graphical Sensitivity Analysis.
Avoiding the Pitfalls of Bad Slides Tips to be Covered Outlines Slide Structure Fonts Colour Background Graphs Spelling and Grammar Conclusions Questions.
Les Mots Intérrogatifs
Lequel The Last Part.
Les opinions Les opinions = Opinions. In this lesson pupils will learn to understand and give their own opinions about singular items.
C’est quel instrument?.
J’écris à mon correspondent
IMPROVING PF’s M&E APPROACH AND LEARNING STRATEGY Sylvain N’CHO M&E Manager IPA-Cote d’Ivoire.
Transcription de la présentation:

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 You should read the K&R C book for lots more details

Like Java, like C Operators same as 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;

datatypesizevalues char1-128 to 127 short2-32,768 to 32,767 int4-2,147,483,648 to 2,147,483,647 long4-2,147,483,648 to 2,147,483,647 float43.4E+/-38 (7 digits) double81.7E+/-308 (15 digits long) Simple Data Types

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 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 int main(int argc, char* argv[]) { /* print a greeting */ printf("Good evening!\n"); return 0; } $./goodevening Good evening! $

Breaking down the code #include –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 CS213 int main(int argc, char* argv) { #ifdef CS213 printf("The Class That Gives CMU Its Zip\n"); #else printf("Some other class\n"); #endif return 0; }

After the preprocessor (gcc –E) int main(int argc, char* argv) { printf("The Class That Gives CMU Its Zip\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 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 Class That Gives CMU Its Zip 8 arguments 0:./cmdline 1: The 2: Class 3: That 4: Gives 5: CMU 6: Its 7: Zip $

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

Structures Aggregate data #include 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.

c d int x = 5, y = 10; float f = 12.5, g = 9.8; char c = ‘c’, d = ‘d’; Memory layout and addresses

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

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 f f_addr

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 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 –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!