136x Filetype PDF File size 0.85 MB Source: cw.fel.cvut.cz
Course Goals Means of Achieving the Course Goals Evaluation and Exam Overview of the Lecture Introduction to C Programming Part 1 – Course Organization Course Goals Jan Faigl Means of Achieving the Course Goals Part I Evaluation and Exam Part 1 – Course Organization Department of Computer Science Part 2 – Introduction to C Programming Faculty of Electrical Engineering Czech Technical University in Prague Program in C Lecture 01 Values and Variables BE5B99CPL – C Programming Language Expressions Standard Input/Output K. N. King: chapters 1, 2, and 3 Jan Faigl, 2017 BE5B99CPL – Lecture 01: Introduction to C Programming 1 / 77 Jan Faigl, 2017 BE5B99CPL – Lecture 01: Introduction to C Programming 2 / 77 Jan Faigl, 2017 BE5B99CPL – Lecture 01: Introduction to C Programming 3 / 77 Course Goals Means of Achieving the Course Goals Evaluation and Exam Course Goals Means of Achieving the Course Goals Evaluation and Exam Course Goals Means of Achieving the Course Goals Evaluation and Exam Course and Lecturer Course Goals Course Organization and Evaluation BE5B99CPL – C Programming Language Master (yourself) programming skills Labs, homeworks, exam Course web page Acquire knowledge of C programming language BE5B99CPL– C Programming Language https://cw.fel.cvut.cz/wiki/courses/be5b99cpl Acquire experience of C programming to use it efficiently Extent of teaching: 2(lec)+2(lab)+5(hw); Submission of the homework Your own experience! Completion: Z,ZK; Credits: 6; Individually during the labs Lecturer: Gain experience to read, write, and understand small C programs Z – ungraded assessment, ZK – exam Acquire programming habits to write doc. Ing. Jan Faigl, Ph.D. Ongoing work during the semester – homeworks and test easy to read and understandable source codes; reusable programs. Exam: test and implementation exam Department of Computer Science – http://cs.fel.cvut.cz Experience programming with Be able to independently work with the computer in the lab (class room) Artificial Intelligence Center (AIC) http://aic.fel.cvut.cz Workstation/desktop computers – using services of operating Attendance to labs and submission of homeworks Center for Robotics and Autonomous Systems (CRAS) system http://robotics.fel.cvut.cz E.g., system calls, read/write files, input and outputs Computational Robotics Laboratory (ComRob) Multithreading applications; http://comrob.fel.cvut.cz Embedded applications – Nucleo F401RE Jan Faigl, 2017 BE5B99CPL – Lecture 01: Introduction to C Programming 5 / 77 Jan Faigl, 2017 BE5B99CPL – Lecture 01: Introduction to C Programming 6 / 77 Jan Faigl, 2017 BE5B99CPL – Lecture 01: Introduction to C Programming 7 / 77 Course Goals Means of Achieving the Course Goals Evaluation and Exam Course Goals Means of Achieving the Course Goals Evaluation and Exam Course Goals Means of Achieving the Course Goals Evaluation and Exam Resources and Literature Further Books Further Resources Textbook Programming in C, 4th Edition, „C Programming: A Modern Approach“ (King, 2008) Stephen G. Kochan, Addison-Wesley, 2014, The C++ Programming Language, 4th Edition ISBN 978-0321776419 (C++11) , Bjarne Stroustrup, Addison-Wesley, C Programming: A Modern Approach, 2nd 21st Century C: C Tips from the New School, 2013, ISBN 978-0321563842 Edition, K. N. King, W. W. Norton & Company, Ben Klemens, O’Reilly Media, 2012, 2008, ISBN 860-1406428577 ISBN 978-1449327149 Introduction to Algorithms, 3rd Edition, Cormen, The main course textbook The C Programming Language, 2nd Edition Leiserson, Rivest, and Stein, The MIT Press, (ANSI C) , Brian W. Kernighan, Dennis M. 2009, ISBN 978-0262033848 Lectures – support for the textbook, slides, comments, and your Ritchie, Prentice Hall, 1988 (1st edition – 1978) notes Algorithms, 4th Edition , Robert Sedgewick, Demonstration source codes are provided as a part of the lecture materials! Advanced Programming in the UNIX Kevin Wayne, Addison-Wesley, 2011, ISBN Laboratory Exercises – gain practical skills by doing homeworks Environment, 3rd edition, W. Richard Stevens, 978-0321573513 (yourself). Stephen A. Rago Addison-Wesley, 2013, ISBN 978-0-321-63773-4 Jan Faigl, 2017 BE5B99CPL – Lecture 01: Introduction to C Programming 9 / 77 Jan Faigl, 2017 BE5B99CPL – Lecture 01: Introduction to C Programming 10 / 77 Jan Faigl, 2017 BE5B99CPL – Lecture 01: Introduction to C Programming 11 / 77 Course Goals Means of Achieving the Course Goals Evaluation and Exam Course Goals Means of Achieving the Course Goals Evaluation and Exam Course Goals Means of Achieving the Course Goals Evaluation and Exam Lectures – Winter Semester (WS) Academic Year 2017/2018 Teachers Communicating Any Issues Related to the Course doc. Ing. Pavel Pačes, Ph.D. Schedule for the academic year 2017/2018 http://www.fel.cvut.cz/en/education/calendar.html Ask the lab teacher or the lecturer Lectures: Department of Computer Science Measurements – Use e-mail for communication http://www.pacespavel.net/ Use your faculty e-mail Karlovo náměstí, Room No. KN:E-307, Wednesday, 9:15–10:45 Put CPL or BE5B99CPL to the subject of your message Center for Advanced Simulation and Technology Send copy (Cc) to lecturer/teacher 14 teaching weeks 13 lectures Jan Faigl, 2017 BE5B99CPL – Lecture 01: Introduction to C Programming 12 / 77 Jan Faigl, 2017 BE5B99CPL – Lecture 01: Introduction to C Programming 13 / 77 Jan Faigl, 2017 BE5B99CPL – Lecture 01: Introduction to C Programming 14 / 77 Course Goals Means of Achieving the Course Goals Evaluation and Exam Course Goals Means of Achieving the Course Goals Evaluation and Exam Course Goals Means of Achieving the Course Goals Evaluation and Exam Computers and Development Tools Services – Academic Network, FEE, CTU Homeworks Network boot with home directories (NFS v4) http://www.fel.cvut.cz/cz/user-info/index.html Six homeworks for the workstation and embedded Nucleo platform Data transfer and file synchronizations – ownCloud, SSH, FTP, USB Compilers gcc or clang https://gcc.gnu.org or http://clang.llvm.org Cloud storage ownCloud – https://owncloud.cesnet.cz https://cw.fel.cvut.cz/wiki/courses/be5b99cpl/hw/start Project building make (GNU make) Examples of usage on lectures and labs Sending large files – https://filesender.cesnet.cz The final homework HW 06 combines an application running on the Text editor – gedit, atom, sublime, vim Schedule, deadlines – FEL Portal, https://portal.fel.cvut.cz Nucleo board that communicates with the workstation program. https://atom.io/, http://www.sublimetext.com/ FEL Google Account – access to Google Apps for Education http://www.root.cz/clanky/textovy-editor-vim-jako-ide 1. HW 01 (5 points) – Processing input data and computing statistics C/C++ development environments – WARNING: Do Not Use An IDE See http://google-apps.fel.cvut.cz/ 2. HW 02 (5 points) – First program on embedded platform http://c.learncodethehardway.org/book/ex0.html Gitlab FEL – https://gitlab.fel.cvut.cz/ 3. HW 03 (10 points) – Reading/writing (files and other communications) Debugging – gdb, cgdb, ddd 4. HW 04 (10 points) – Readings and visualization on the embedded platform Code::Blocks, CodeLite Information resources (IEEE Xplore, ACM, Science Direct, 5. HW 05 (10 points) – Multi-thread computation http://www.codeblocks.org, http://codelite.org Springer Link) https://dialog.cvut.cz 6. HW 06 (20 points) – A complex multi-threaded application with communica- NetBeans 8.0 (C/C++), Eclipse–CDT tion Workstation/Nucleo CLion – https://www.jetbrains.com/clion Academic and campus software license https://download.cvut.cz Some adjustments are expected Embedded development for the Nucleo – System Workbench for STM32 National Super Computing Grid Infrastructure – MetaCentrum All homeworks must be submitted to award an ungraded assessment based on Eclipse http://www.metacentrum.cz/cs/index.html Late submission will be penalized! Jan Faigl, 2017 BE5B99CPL – Lecture 01: Introduction to C Programming 15 / 77 Jan Faigl, 2017 BE5B99CPL – Lecture 01: Introduction to C Programming 16 / 77 Jan Faigl, 2017 BE5B99CPL – Lecture 01: Introduction to C Programming 17 / 77 Course Goals Means of Achieving the Course Goals Evaluation and Exam Course Goals Means of Achieving the Course Goals Evaluation and Exam Course Goals Means of Achieving the Course Goals Evaluation and Exam Course Evaluation Grading Scale Overview of the Lectures Maximum Required Minimum Grade Points Mark Evaluation 1. Course information, Introduction to C programming Points K. N. King: chapters 1, 2, and 3 Points Points A ≥90 1 Excellent 2. Writing your program in C, control structures (loops), expressions B 80–89 1,5 Very Good K. N. King: chapters 4, 5, and 6 Homeworks 60 30 C 70–79 2 Good 3. Data types, arrays, pointer, memory storage classes, function call Test 10 0 D 60–69 2,5 Satisfactory K. N. King: chapters 7, 8, 9, 10, and 11 E 50–59 3 Sufficient 4. Data types: arrays, strings, and pointers K. N. King: chapters 11, 12, and 13 Exam test 20 10 F <50 4 Fail 5. Data types: Struct, Union, Enum, Bit fields. Preprocessor and Large Programs. Implementation exam 10 0 K. N. King: chapters 14, 15, and 16 Expected results 6. Input/Output – reading/writing from/to files and other communication channels Total 100 points 40 points is F! All homeworks work and have been submitted before the deadlines (60 points) Standard C Library – selected functions and libraries Test (10 points) 7. Parallel and multi-thread programming – methods and synchronizations primitives Exam (test) (20 points) 8. Multi-thread application models, POSIX threads and C11 threads 30 points from the semester are required for awarding ungraded Exam (implementation) (10 points) 9. Version Control Systems (VCS) assessment 95 points and more (A – Excellent) – with small imperfection 10. ANSI C, C99, C11 – differences and extensions The course can be passed with ungraded assessment and exam 76 points (C – Good) for 20% loss 11. Differences between C and C++: Introduction to object oriented programming in C++ All homeworks must be submitted 76 and more points represents a solid background for fur- 12. Object oriented programming in C++: classes, objects, encapsulation, and polymor- ther development of your programming skills. phism Jan Faigl, 2017 BE5B99CPL – Lecture 01: Introduction to C Programming 19 / 77 Jan Faigl, 2017 BE5B99CPL – Lecture 01: Introduction to C Programming 20 / 77 Jan Faigl, 2017 BE5B99CPL – Lecture 01: Introduction to C Programming 21 / 77 Program in C Values and Variables Expressions Standard Input/Output Program in C Values and Variables Expressions Standard Input/Output Program in C Values and Variables Expressions Standard Input/Output C Programming Language Writing Your C Program Low-level programming language System programming language (operating system) Language for (embedded) systems — MCU, cross-compilation Source code of the C program is written in text files A user (programmer) can do almost everything Header files usually with the suffix .h Part II Initialization of the variables, release of the dynamically allocated memory, etc. Sources files usually named with the suffix .c Very close to the hardware resources of the computer Part 2 – Introduction to C Programming Direct calls of OS services, direct access to registers and ports Header and source files together with declaration and definition Dealing with memory is crucial for correct behaviour of the program (of functions) support OneofthegoalsoftheCPLcourseistoacquirefundamentalprinciplesthatcan Organization of sources into several files (modules) and libraries be further generalized for other programming languages. The C programming Modularity – Header file declares a visible interface to others language provides great opportunity to became familiar with the memory model Adescription (list) of functions and their arguments without particular and key elements for writting efficient programs. implementation It is highly recommended to have compilation of your Reusability program fully under control. Only the “interface” declared in the header files is need to use functions from available binary libraries It may look difficult at the beginning, but it is relatively easy and straight- forward. Therefore, we highly recommend to use fundamental tools for your program compilation. After you acquire basic skills, you can profit from them also in more complex development environments. Jan Faigl, 2017 BE5B99CPL – Lecture 01: Introduction to C Programming 22 / 77 Jan Faigl, 2017 BE5B99CPL – Lecture 01: Introduction to C Programming 24 / 77 Jan Faigl, 2017 BE5B99CPL – Lecture 01: Introduction to C Programming 25 / 77 Program in C Values and Variables Expressions Standard Input/Output Program in C Values and Variables Expressions Standard Input/Output Program in C Values and Variables Expressions Standard Input/Output Valid Characters for Writing Source Codes in C Writing Identifiers in C Writing Codes in C Lowercase and uppercase letters, numeric characters, symbols and Identifiers are names of variables (custom types and functions) separators ASCII – American Standard Code for Information Interchange Types and functions, viz further lectures a–z A–Z 0—9 Each executable program must have at least one function and the ! " # % & ’ ( ) * + , - . / : ; < = > ? [ \ ] ˆ _ { | } ∼ Rules for the identifiers function has to be main() space, tabular, new line Characters a–z, A–Z, 0–9 a _ The run of the program starts at the beginning of the function Escape sequences for writting special symbols The first character is not a numeral main(), e.g., \’ – ’, \" – ", \? – ?, \\– \ Case sensitive Escape sequences for writting numeric values in a text string Length of the identifier is not limited 1 #includeFirst 31 characters are significant – depends on the implementation / compiler 2 \o, \oo, where o is an octal numeral 3 int main(void) \xh, \xhh, where h is a hexadecimal numeral Keywords 4 { 32 5 printf("I like BE5B99CPL!\n"); 1 int i = ’a’; auto break case char const continue default do 6 2 int h = 0x61; 7 return 0; 3 int o = 0141; double else enum extern float for goto if int long 8 } 4 5 printf("i: %i h: %i o: %i c: %c\n", i, h, o, i); register return short signed sizeof static struct The form of the main() function is prescribed 6 printf("oct: \141 hex: \x61\n"); switch typedef union unsigned void volatile while E.g., \141, \x61 lec01/esqdho.c C98 See further examples in this lecture \0 – character reserved for the end of the text string (null C99 introduces, e.g., inline, restrict, _Bool, _Complex, _Imaginary character) C11 further adds, e.g., _Alignas, _Alignof, _Atomic, _Generic, _Static_assert, _Thread_local Jan Faigl, 2017 BE5B99CPL – Lecture 01: Introduction to C Programming 26 / 77 Jan Faigl, 2017 BE5B99CPL – Lecture 01: Introduction to C Programming 27 / 77 Jan Faigl, 2017 BE5B99CPL – Lecture 01: Introduction to C Programming 28 / 77 Program in C Values and Variables Expressions Standard Input/Output Program in C Values and Variables Expressions Standard Input/Output Program in C Values and Variables Expressions Standard Input/Output Simple C Program Program Compilation and Execution Structure of the Source Code – Commented Example Source file program.c is compiled into runnable form by the Commented source file program.c 1 #include compiler, e.g., clang or gcc 1 /* Comment is inside the markers (two characters) 2 clang program.c 2 and it can be split to multiple lines */ 3 int main(void) There is a new file a.out that can be executed, e.g., 3 // In C99 - you can use single line comment 4 { ./a.out 4 #include /* The #include direct causes to 5 printf("I like BE5B99CPL!\n"); Alternatively the program can be run only by a.out in the case the include header file stdio.h from the C standard 6 actual working directory is set in the search path of executable files library */ 7 return 0; The program prints the argument of the function printf() 5 8 } lec01/program.c ./a.out 6 int main(void) // simplified declaration Source files are compiled by the compiler to the so-called object I like BE5B99CPL! 7 { // of the main function files usually with the suffix .o If you prefer to run the program just by a.out instead of ./a.out you need 8 printf("I like BE5B99CPL!\n"); /* calling printf() Object code contains relative addresses and function calls or just ref- to add your actual working directory to the search paths defined by the function from the stdio.h library to print string erences to function without known implementations. environment variable PATH to the standard output. \n denotes a new line */ The final executable program is created from the object files by export PATH="$PATH:‘pwd‘" 9 return 0; /* termination of the function. Return the linker Notice, this is not recommended, because of potentially many working directories. value 0 to the operating system */ The command pwd prints the actual working directory, see man pwd 10 } Jan Faigl, 2017 BE5B99CPL – Lecture 01: Introduction to C Programming 29 / 77 Jan Faigl, 2017 BE5B99CPL – Lecture 01: Introduction to C Programming 30 / 77 Jan Faigl, 2017 BE5B99CPL – Lecture 01: Introduction to C Programming 31 / 77 Program in C Values and Variables Expressions Standard Input/Output Program in C Values and Variables Expressions Standard Input/Output Program in C Values and Variables Expressions Standard Input/Output Program Building: Compiling and Linking Compilation and Linking Programs Steps of Compiling and Linking Program development is editing of the source code (files with suf- The previous example combines three steps of building the program fixes .c and .h); Human readable Preprocessor – allows to define macros and adjust compilation into a single call of the command (clang or gcc). The particular Compilation of the particular source files (.c) into object files (.o or according to the particular compilation environment steps can be performed individually .obj) ; Machine readable The output is text (“source”) file. 1. Text preprocessing by the preprocessor, which utilizes its own Linking the compiled files into executable binary file; Compiler – Translates source (text) file into machine readable form macro language (commands with the prefix #) Execution and debugging of the application and repeated editing of Native (machine) code of the platform, bytecode, or assembler alternatively All referenced header files are included into a single source file the source code. Linker – links the final application from the object files 2. Compilation of the source file into the object file .c .h .a/.lib Under OS, it can still reference library functions (dynamic libraries linked Names of the object files usually have the suffix .o Source file Header files Lib files during the program execution), it can also contains OS calls (libraries). clang -c program.c -o program.o Preprocesor Particular steps preprocessor, compiler, and linker are usually The command combines preprocessor and compiler. Linker a.out implemented by a “single” program that is called with appropriate 3. Executable file is linked from the particular object files and Compiler Executable binary file arguments. referenced libraries by the linker (linking), e.g., Object Object files E.g., clang or gcc clang program.o -o program File .o/.obj .o/.obj Jan Faigl, 2017 BE5B99CPL – Lecture 01: Introduction to C Programming 32 / 77 Jan Faigl, 2017 BE5B99CPL – Lecture 01: Introduction to C Programming 33 / 77 Jan Faigl, 2017 BE5B99CPL – Lecture 01: Introduction to C Programming 34 / 77 Program in C Values and Variables Expressions Standard Input/Output Program in C Values and Variables Expressions Standard Input/Output Program in C Values and Variables Expressions Standard Input/Output Compilers of C Program Language Functions, Modules, and Compiling and Linking Functions in C Function definition inside other function is not allowed in C. In CPL, we mostly use compilers from the families of compilers: Function is the fundamental building block of the modular Function names can be exported to other modules gcc – GNU Compiler Collection programming language Module is an independent file (compiled independently) https://gcc.gnu.org Modular program is composed of several modules/source files Function are implicitly declared as extern, i.e., visible clang – C language family frontend for LLVM Function definition consists of the Using the static specifier, the visibility of the function can be http://clang.llvm.org Function header limited to the particular module Local module function Under Win, two derived environments can be utilized: cygwin https://www.cygwin.com/ or Function body Definition is the function implementation. MinGWhttp://www.mingw.org/ Function arguments are local variables initialized by the values Function prototype (declaration) is the function header to passed to the function Arguments are passed by value (call by value) Basic usage (flags and arguments) are identical for both compilers provide information how the function can be called clang is compatible with gcc It allows to use the function prior its definition, i.e., it allows to compile C allows recursions – local variables are automatically allocated Example the code without the function implementation, which may be located in at the stack Further details about storage classes in next lectures. compile: gcc -c main.c -o main.o other place of the source code, or in other module. Declaration is the function header and it has the form Arguments of the function are not mandatory – void arguments link: gcc main.o -o main fnc(void) type function_name(arguments); The return type of the function can be void, i.e., a function without return value – void fnc(void); Jan Faigl, 2017 BE5B99CPL – Lecture 01: Introduction to C Programming 35 / 77 Jan Faigl, 2017 BE5B99CPL – Lecture 01: Introduction to C Programming 36 / 77 Jan Faigl, 2017 BE5B99CPL – Lecture 01: Introduction to C Programming 37 / 77 Program in C Values and Variables Expressions Standard Input/Output Program in C Values and Variables Expressions Standard Input/Output Program in C Values and Variables Expressions Standard Input/Output Example of Program / Module Program Starting Point – main() Arguments of the main() Function Each executable program must contain at least one definition of During the program execution, the OS passes to the program the 1 #include /* header file */ the function and that function must be the main() number of arguments (argc) and the arguments (argv) 2 #define NUMBER 5 /* symbolic constatnt */ The main() function is the starting point of the program In the case we are using OS 3 The main() has two basic forms 4 int compute(int a); /* function header/prototype */ The first argument is the name of the program 5 1. Full variant for programs running under an Operating System (OS) 6 int main(int argc, char *argv[]) int main(int argc, char *argv[]) 1 int main(int argc, char *argv[]) 7 { /* main function */ { 2 { 8 int v = 10; /* variable declaration */ ... 9 int r; 3 int v; 10 r = compute(v); /* function call */ } 11 return 0; /* termination of the main function */ It can be alternatively written as 4 v = 10; 12 } int main(int argc, char **argv) 5 v = v + 1; 13 { 14 int compute(int a) ... 6 return argc; 15 { /* definition of the function */ 7 } 16 int b = 10 + a; /* function body */ } lec01/var.c 17 return b; /* function return value */ 2. For embedded systems without OS The program is terminated by the return in the main() function 18 } int main(void) { The returned value is passed back to the OS and it can be further ... use, e.g., to control the program execution. Jan Faigl, 2017 BE5B99CPL – Lecture 01: Introduction to C Programming 38 / 77 Jan Faigl, 2017 } BE5B99CPL – Lecture 01: Introduction to C Programming 39 / 77 Jan Faigl, 2017 BE5B99CPL – Lecture 01: Introduction to C Programming 40 / 77
no reviews yet
Please Login to review.