SYSC 2006 Lecture 5: Imperative Programming PDF
Document Details
Uploaded by AstonishedIndium8494
Carleton University
2024
null
null
Tags
Related
- Fall 2020 Concepts of Programming Languages PDF
- Programming Paradigm and Intro to JAVA Lecture
- EE219 Recursion in C/C++ PDF
- Introduction to Programming, Chapter 1, PDF
- SYSC 2006: Imperative Programming Lecture 3 - Flow of Control (Carleton University)
- SYSC 2006 Lecture 2: Variables, Data Types, and Expressions PDF
Summary
This document is a lecture on organizing code and global variables in imperative programming, specifically using C. It covers the compilation process, including preprocessing and the use of header files in C. It aims at helping students learn the basic concepts of programming.
Full Transcript
Copyright © 2024 1 Department of Systems and Computer Engineering, Carleton University SYSC 2006: Foundations of Imperative Programming Lecture 5: Organizing your Code Global Variables 2...
Copyright © 2024 1 Department of Systems and Computer Engineering, Carleton University SYSC 2006: Foundations of Imperative Programming Lecture 5: Organizing your Code Global Variables 2 Previous Lecture Summary ❑Use functions to create modular programs ❑Draw memory diagrams to show how parameters are passed to functions ❑Explain the concept of recursion ❑Implement recursive functions in C ❑Draw memory diagrams to trace the execution of recursive function 2 3 Objectives Learn how to organize your code Understand the basics of compilation Compile code from the terminal Introduce global variables 4 How to Organize your Code 5 Code Divided in Functions Thinking about organizing your code only makes sense when you have your code organized into functions. What does the compiler need? Function's return type Name of the function Number of parameters the function expects and their types Reminder: Implicit declarations are dangerous! 6 Code Divided in Functions What does a developer need to use a function? Name of the function Number of parameters the function expects and their types Function's return type The Compiler and the Developer need the same information Can we separate this information into different files? 7 How to Organize your Code Good practice: If the complexity of your code starts growing OR if you are planning to share your work results with other programmers, separate your code into modules. Each module contains two files: Header file (.h): The file that contains the declarations (not definitions!) of all the entities (symbols, types, variables, functions) intended to be shared with others. Source file (.c): The file that contains the source code (definitions) 8 How to Organize your Code main.c Example: #include power.h #include #include "power.h" int power(int m, int n); int main(void){ power.c int i = 0; int power(int base, unsigned int n) { while (i < 10) { printf("%d %d %d\n", i, if (n == 0){ power(2,i),power(-3,i)); return 1; i++; } } return n*power(base, n-1); return EXIT_SUCCESS; } } 9 How to Organize your Code #include Purpose of a header file: #include Compiler finds out how to compile the external function invocation. #include "power.h" Programmer can learn how to use the functions in the program. int main(void){ int i = 0; File name surrounded by angle brackets < >: while (i < 10) { preprocessor should look for the included printf("%d %d %d\n", i, files in the standard locations. power(2,i),power(-3,i)); File name surrounded by quotes " ": i++; preprocessor should look for the included file } in the same directory as the current file return EXIT_SUCCESS; being processed is located. } 10 MORE PRACTICE (Home) Reorganize the code you created in lecture 4 for the factorial and power functions into.h and.c files. 11 Compilation 12 Introduction From High-level programming languages (HLLs) to Machine Language HLLs are somewhat similar to a natural language They use symbols, words, and conventions readable to humans Enable humans to express complex commands for computers Programmers can write programs that are more or less independent of a particular type of computer 13 Introduction A “translator” is used to translate the program into Machine Language Programs written in high-level languages could be translated into any number of different machine languages → making them usable on many different computers “Portability” 14 Introduction How do we translate from HLL to Machine Language? We use compilers and the process is called compilation (e.g., C language) We can also use interpreters based on the HLL (e.g., Python) 15 How to Create a Program in C? Step 1: Write a program following the rules of the programming language Source Code: stored in file/s You just need a text editor The file’s extension gives you an idea of the programming language (e.g., myprog.c, myprogram.cpp, myprogram.py, etc.) 16 How to Create a Program in C? Step 2: Compile the Source Code The compiler determines if there are any errors in the coding. Do not expect the compiler to think for you. If there are no errors, it transforms the Source Code into a file with the program in machine language (Linux.out, MS Windows.exe) 17 The Compilation Process Source Code Preprocessor Preprocessed Code (*.h, *.c) Compiler Assembly Code Assembler Object File (*.obj) Static Library Files Linker Executable or Library There are multiple C compilers: gcc, clang, etc. 18 Preprocessing Source Code Preprocessed (*.h, *.c) Removal of comments Code Preprocessor Replacing preprocessing directives (i.e., statements that start with #) Expansion of include files Expansions of macros Conditional compilation In general, the output is not visible to the programmer Programmer may want to look at it if an error occurs 19 Preprocessing Let’s assume we use the gcc compiler in Ubuntu or MacOS terminal. If you want to know what your source code looks like after preprocessing: use –E option Example: gcc –E prog.c (assuming the source code is stored in prog.c). This option causes the compiler to stop as soon as the preprocessor completes its function. The preprocessed source code will be sent to the standard output, and all modifications made by the preprocessor will be marked in a specific way. If you want to preserve the preprocessed text for further analysis, you can redirect the standard output to a file: gcc –E prog.c > outfilename 20 Preprocessing: Important Rules Preprocessor directives always begin with # (hash) It must be the first visible character in the line If the preprocessor directive does not fit on one line and needs to be split, you must put the \ character in the place where the directive is broken #include \ Any preprocessor directive placed in a particular file acts only inside that file and nowhere else. 21 #include #include “filename.h” #include Preprocessor looks for a file named filename.h File name inside quotes (“”): searches for that file in the same directory as the file containing the directive; File name inside triangular brackets (): searches for the file in the compiler's default directory 22 #include File is not found → error File found → #include directive is replaced with the content of the included file; The included file may contain #include directives A particular compiler may impose a certain limit on the number of nested inclusions #include may be used anywhere in the source file, not just at the beginning. 23 # include Two source files Output of the preprocessor Lines with # are src1.c internal and ignored. int main(void) { # 1 "src1.c" User should read: #include "src2.c" # 1 "" } # 1 "" int main(void) { src2.c # 1 "src1.c“ int i = 0; int i = 0; int main(void) { return i; return i; # 1 "src2.c" 1 } int i = 0; gcc -E src1.c return i; # 4 "src1.c" 2 } 24 #define identifier text #define identifier text Preprocessor remembers the identifier and the text associated with it; From this moment on, the preprocessor analyses the source code, replacing any occurrence of the identifier with the associated text; The directive itself will not appear in the preprocessed source code. 25 #define identifier text The directive itself will not appear in the preprocessed source code. Source code: Preprocessed code: #define PI 3.1415 int main(void) { int main(void) { float s = 2 * PI * 10.0; float s = 2 * 3.1415 * 10.0; return 0; return 0; } } 26 #define identifier text Common MISTAKE #define SIZE 4+4 What will be the output? int main(void) { int i; Do not forget that the i = 2 * SIZE; preprocessor does not use the value of the identifier, printf("%d\n",i); but only replaces it with return 0; the associated text. } 27 Conditional compilation Expressions placed after the #if and #elif are used to #if expression0 decide which text is passed for compilation. text0 Expressions are evaluated by the preprocessor (not by #elif expression1 the compiler) so they must be evaluated exclusively by text1 means of: #elif expression2 Preprocessor symbols that are assigned numeric values text2 (using #define directives); :: Operators: +, -, * (multiplication), /, >>,