1/16 Programski jezik C Vladimir Filipović

Slides:



Advertisements
Similar presentations
COMP 2130 Intro Computer Systems Thompson Rivers University
Advertisements

Lectures 10 & 11.
1 Chapter 10 Strings and Pointers. 2 Introduction  String Constant  Example: printf(“Hello”); “Hello” : a string constant oA string constant is a series.
What is a pointer? First of all, it is a variable, just like other variables you studied So it has type, storage etc. Difference: it can only store the.
Engineering Computing I Chapter 1 – Part B A Tutorial Introduction continued.
1 Homework Assignments Turn in HW1 (If not done yet, catch up!) Questions about HW1? Anyone still stuck on apply / UNIX account? Everyone have the books?
The C programming language: Introduction Fall 2003, Jen-Chang Liu.
Character Input and Output C and Data Structures Baojian Hua
C workshop Yuli Kaplunovsky - Today - Introduction to C Recommended book: The C programming Language / Kernighan & Ritchie.
1 Review of Class on Oct Outline of Chapter 4  How to write a function?  Function Prototypes  Function Invocation  Function Definition  The.
Introduction to C Systems Programming. Systems Programming: Introduction to C 2 Systems Programming: 2 Introduction to C  A ‘C’ Program –Variable Declarations.
Introduction to Computers and Programming Class 15 Introduction to C Professor Avi Rosenfeld.
流程控制: while loop 迴圈 Test condition Enter loop Yes (non-0) Execute Loop body no exit F=0 F=F+20 … F=F
Imperative Programming Prof. Béat Hirsbrunner Amine Tafat, PhD Student Matthias Buchs and Raphaël Lesceux, Graduate Students Department of Informatics.
0 Arrays (1/2) #include /* count digits, white space, others */ main() { int c, i, nwhite, nother; int ndigit[10]; nwhite = nother = 0; for (i = 0; i
Functions / Procedures
Prof. Béat Hirsbrunner Fulvio Frapolli, PhD Student (exercises) Bachelor students : - Major in computer science (first year, 2nd term) - Major in information.
1 CSE1301 Computer Programming Lecture 5: Components of a C Program (Part 1)
Character Input and Output
Pointers Example Use int main() { int *x; int y; int z; y = 10; x = &y; y = 11; *x = 12; z = 15; x = &z; *x = 5; z = 8; printf(“%d %d %d\n”, *x, y, z);
Review: midterm #9 #include void main(void) { int c; c = getchar(); if(c>=48){ if(c
C programming---basic 1 Introduction to C 2 C Fundamentals 3 Formatted Input/Output 4 Expression 5 Selection Statement 6 Loops 7 Basic Types 8 Arrays 9.
A simple C program: Printing a line of text #include main() { printf(“hello, world\n”); } Program output: hello, world.
Introduction to C Language
C Programming A Modern Approach
1 COMP 2130 Introduction to Computer Systems Computing Science Thompson Rivers University.
Introduction to C Programming Summer 2014 COMP 2130 Intro Computer Systems Computing Science Thompson Rivers University.
By Sidhant Garg.  C was developed between by Dennis Ritchie at Bell Laboratories for use with the Unix Operating System.  Unlike previously.
C Programming Tutorial – Part I CS Introduction to Operating Systems.
Programming I Introduction Introduction The only way to learn a new programming language is by writing programs in it. The first program to.
CSC 270 – Survey of Programming Languages C Lecture 4 – Strings Heavily borrowed from Dr. Robert Siegfried and Dietel How to Program in C Powerpoint Presentations.
Outline Symbolic Constants Character Input and Output if … else switch…case.
C Program Design Functions and Program Structure 主講人:虞台文.
Programming Language  C Tutorial Introduction 主講人:虞台文.
1 COMP 2130 Introduction to Computer Systems Computing Science Thompson Rivers University.
Introduction to C Programming Chapter 2 : Data Input, Processing and Output.
1 CSE1301 Computer Programming Lecture 12 Functions (Part 1)
Functions #include int sum(int i, int j); main() { int a, b, res; a = 1; b = 2; res = sum(a, b); printf("%d + %d = %d\n", a, b, res); } int sum(int i,
CS140: Intro to CS An Overview of Programming in C by Erin Chambers.
1 CSE1301 Computer Programming Lecture 13 Functions (Part 1)
Solution to Midterm Exam
Prof. Béat Hirsbrunner Ammar Halabi, PhD student (exercises) Dani Rotzetter, Master student (exercises) Bachelor students : Major in computer science (3rd.
Introduction to Computer Organization & Systems Topics: Types in C: floating point COMP C Part III.
1 Homework Done the reading? –K&R –Glass Chapters 1 and 2 Applied for cs240? (If not, keep at it!) Gotten a UNIX account? (If not, keep at it!)
CS 261 C Basics Page 2 1/14/2016 CS 261, WSU Vancouver Primitive Types Notes: 4 A is a constant; B is a variable.
C Homework Write mypaste.c and mycomm.c – mypaste is like paste & mycomm is like comm – Both take two files as arguments – Paste reads a line from each.
Operating System Discussion Section. The Basics of C Reference: Lecture note 2 and 3 notes.html.
Chapter 1 Basic C Programming
2/22/2016 1R. Smith - University of St Thomas - Minnesota CISC 130: Today’s Class History Paper scheduleHistory Paper schedule RecapRecap Plus PlusPlus.
Review (before the 1 st test): while (conditions) { statements; } while loop: if/else if/else statements: if (conditions) { statements; } else if (different.
The Cast Operator The cast operator converts explicitly from one data type of an expression to another. For example, if x is of type int, the value of.
LOOPING IN C. What would be the output of the following program main( ) { int j ; while ( j
2. C FUNDAMENTALS. Example: Printing a Message /* Illustrates comments, strings, and the printf function */ #include int main(void) { printf("To C, or.
Introduction to Programming Lecture 5: Interaction.
CPT: Chars/ Computer Programming Techniques Semester 1, 1998 Objectives of these slides: –to look at how C processes characters 4. Character.
CS113 Introduction to C Instructor: Ioannis A. Vetsikas Lecture 2 : August 28 webpage:
D. Gotseva PL - Lectures 1 Programming Languages Lectures Assoc. Prof. PhD Daniela Gotseva
Formatted Input and Output
Control Flow (Chapter 3)
C Programming Tutorial – Part I
Kontrola toka izvršenja
C programming---basic
CS 1430: Programming in C++.
Structured Programming (Top Down Step Refinement)
CSC215 Homework Homework 04 Due date: Oct 14, 2016.
CS Programming Languages - C Lecture 1
CSC215 Homework Homework 03 Due date: Oct 07, 2016.
Your questions from last session
Homework Applied for cs240? (If not, keep at it!) 8/10 Done with HW1?
Strings #include <stdio.h>
Presentation transcript:

1/16 Programski jezik C Vladimir Filipović

2/16 Prednosti upotrebe jezika C Kompaktnost asemblerskog jezika Mali skup komandi Efikasnost Nije jezik sa jakom tipizacijom Strukturni jezik visokog nivoa Modularan dizajn Integracija sa asemblerskim jezikom Rad sa bitovima Pokazivačke promenljive Prilagođene strukture Prenosivost Podrška nezavisnih proizvođača

3/16 Nedostatci jezika C Nema jake tipizacije Nemogućnost provere tokom izvršavanja Odgovornost programera

4/16 UVOD Priprema za početak Promenljive i aritmetički izrazi Naredba for Simboličke konstante Ulaz i izlaz znakova Nizovi Funkcije Argumenti - pozivanje po vrednosti Znakovni nizovi Spoljašnje promenljive i domet

5/16 Pripremanje za početak hello, world cc hello.c a.out hello, world #include main() { printf("hello, world\n"); }

6/16 Promenljive i aritmetički izrazi #include /* print Fahrenheit-Celsius table for fahr = 0, 20,..., 300 */ main() { int fahr, celsius; int lower, upper, step; lower = 0; /* lower limit of temperature scale */ upper = 300; /* upper limit */ step = 20; /* step size */ fahr = lower; while (fahr <= upper) { celsius = 5 * (fahr-32) / 9; printf("%d\t%d\n", fahr, celsius); fahr = fahr + step; }

7/16 #include /* print Fahrenheit-Celsius table for fahr = 0, 20,..., 300; floating-point version */ main() { float fahr, celsius; float lower, upper, step; lower = 0; /* lower limit of temperature scale */ upper = 300; /* upper limit */ step = 20; /* step size */ fahr = lower; while (fahr <= upper) { celsius = (5.0/9.0) * (fahr-32.0); printf("%3.0f %6.1f\n", fahr, celsius); fahr = fahr + step; } …

8/16 Naredba for #include /* print Fahrenheit-Celsius table */ main() { int fahr; for (fahr = 0; fahr <= 300; fahr = fahr + 20) printf("%3d %6.1f\n", fahr, (5.0/9.0)*(fahr-32)); }

9/16 Simboličke konstante #include #define LOWER 0 /* lower limit of table */ #define UPPER 300 /* upper limit */ #define STEP 20 /* step size */ /* print Fahrenheit-Celsius table */ main() { int fahr; for (fahr = LOWER; fahr <= UPPER; fahr = fahr + STEP) printf("%3d %6.1f\n", fahr, (5.0/9.0)*(fahr-32)); }

10/16 Ulaz i izlaz znakova #include /* copy input to output; */ main() { int c; c = getchar(); while (c != EOF) { putchar(c); c = getchar(); } #include /* copy input to output; 2nd version */ main() { int c; while ((c = getchar()) != EOF) putchar(c); }

11/16 #include /* count characters in input; 1st version */ main() { long nc; nc = 0; while (getchar() != EOF) ++nc; printf("%ld\n", nc); } #include /* count characters in input; 2nd version */ main() { double nc; for (nc = 0; gechar() != EOF; ++nc) ; printf("%.0f\n", nc); }

12/16 #include /* count lines in input */ main() { int c, nl; nl = 0; while ((c = getchar()) != EOF) if (c == '\n') ++nl; printf("%d\n", nl); }

13/16 #include #define IN 1 /* inside a word */ #define OUT 0 /* outside a word */ /* count lines, words, and characters in input */ main() { int c, nl, nw, nc, state; state = OUT; nl = nw = nc = 0; while ((c = getchar()) != EOF) { ++nc; if (c == '\n') ++nl; if (c == ' ' || c == '\n' || c == '\t') state = OUT; else if (state == OUT) { state = IN; ++nw; } printf("%d %d %d\n", nl, nw, nc); }

14/16 Nizovi #include /* count digits, white space, others */ main() { int c, i, nwhite, nother; int ndigit[10]; nwhite = nother = 0; for (i = 0; i < 10; ++i) ndigit[i] = 0; while ((c = getchar()) != EOF) if (c >= '0' && c <= '9') ++ndigit[c-'0']; else if (c == ' ' || c == '\n' || c == '\t') ++nwhite; else ++nother; printf("digits ="); for (i = 0; i < 10; ++i) printf(" %d", ndigit[i]); printf(", white space = %d, other = %d\n", nwhite, nother); }

15/16 Funkcije #include int power(int m, int n); /* test power function */ main() { int i; for (i = 0; i < 10; ++i) printf("%d %d %d\n", i, power(2,i), power(-3,i)); return 0; } /* power: raise base to n-th power; n >= 0 */ int power(int base, int n) { int i, p; p = 1; for (i = 1; i <= n; ++i) p = p * base; return p; } /* power: raise base to n-th power; n >= 0 */ /* (old-style version) */ power(base, n) int base, n; { int i, p; p = 1; for (i = 1; i <= n; ++i) p = p * base; return p; }

16/16 Argumenti - pozivanje po vrednosti /* power: raise base to n-th power; n >= 0; version 2 */ power(int base, int n) { int p; for (p = 1; n > 0; --n) p = p * base; return p; }

17/16 Znakovni nizovi #include #define MAXLINE 1000 /* maximum input line length */ int getline(char line[], int maxline); void copy(char to[], char from[]); /* print the longest input line */ main() { int len; /* current line length */ int max; /* maximum length seen so far */ char line[MAXLINE]; /* current input line */ char longest[MAXLINE]; /* longest line saved here */ max = 0; while ((len = getline(line, MAXLINE)) > 0) if (len > max) { max = len; copy(longest, line); } if (max > 0) /* there was a line */ printf("%s", longest); return 0; } /* getline: read a line into s, return length */ int getline(char s[],int lim) { int c, i; for (i=0; i < lim-1 && (c=getchar())!=EOF && c!='\n'; ++i) s[i] = c; if (c == '\n') { s[i] = c; ++i; } s[i] = '\0'; return i; } /* copy: copy 'from' into 'to'; assume to is big enough */ void copy(char to[], char from[]) { int i; i = 0; while ((to[i] = from[i]) != '\0') ++i; }

18/16 Spoljašnje promenljive i opseg #include #define MAXLINE 1000 /* maximum input line size */ int max; /* maximum length seen so far */ char line[MAXLINE]; /* current input line */ char longest[MAXLINE]; /* longest line saved here */ int getline(void); void copy(void); /* print longest input line; specialized version */ main() { int len; extern int max; extern char longest[]; max = 0; while ((len = getline()) > 0) if (len > max) { max = len; copy(); } if (max > 0) /* there was a line */ printf("%s", longest); return 0; } /* getline: specialized version */ int getline(void) { int c, i; extern char line[]; for (i = 0; i < MAXLINE - 1 && (c=getchar)) != EOF && c != '\n'; ++i) line[i] = c; if (c == '\n') { line[i] = c; ++i; } line[i] = '\0'; return i; } /* copy: specialized version */ void copy(void) { int i; extern char line[], longest[]; i = 0; while ((longest[i] = line[i]) != '\0') ++i; }