🎧 New: AI-Generated Podcasts Turn your study notes into engaging audio conversations. Learn more

Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...

Full Transcript

Introducing C & Unix 1 Why Unix? Dominant server operating system Uses open standards (e.g., POSIX threads) Free open-source versions available (FreeBSD, OpenBSD, Linux) Many free software development tools2 (e.g., gcc, emacs, gdb etc.) We will be...

Introducing C & Unix 1 Why Unix? Dominant server operating system Uses open standards (e.g., POSIX threads) Free open-source versions available (FreeBSD, OpenBSD, Linux) Many free software development tools2 (e.g., gcc, emacs, gdb etc.) We will be using Linux in the labs Why C & What is C? History of C - A byproduct of the UNIX operating system - A higher-level language than assembly - Mostly done in the 1960s and 1970s - Different language standards that evolved over time C influences many modern programming 3 languages - C++, Java, C#, Perl Understanding how memory works helps you understand other programming languages as well Strengths/Weaknesses of C Strengths: - Efficiency, portability, power, flexibility, standard libraries - Low-level, access to machine-level concepts - Small, limited features - Permissive; you need to know what you are doing 4 Weaknesses: - Error-prone, difficult to understand, difficult to modify Relationship to Other Courses 5 In 201, we take a look at using the operating system UNIX & the high- level programming language C Relationship to Other Software Engineering Courses 201: small-scale programming 301: team work, object-oriented design 401: large-scale programming 402: software quality 6 Relationship to the “Outside World” 7 Learning C also allows you to understand & appreciate higher-level programming languages, because you know what goes on “behind the scenes” The UNIX/Linux Shell The machines in the CMPUT 201 lab are ugXX.cs.ualberta.ca, where XX ranges from 00 to 34 If you are using a UNIX-based OS (e.g., Ubuntu or MacOS), just go to your terminal and use ssh to connect to the lab computer 8 If you are using Windows, you will need an ssh client such as PuTTY Example of Connecting to Lab Computer address of lab the ssh command your ccid machine Shell/ terminal 9 demo Shell Commands ls, mkdir, cd, cat, vi, gcc, cp, rm, mv, … man (manual pages are extremely useful) We will cover these commands throughout the course, especially in the Labs. 10 You should also get used to searching for certain commands yourself. demo Sample C program: Only a few things are included by default in C. You need to include the #include libraries you will use. int main(void) { The main function is the This is a variable main entry point to your that can hold int classNumber; program only integer values printf("What is your class number? "); scanf("%d", &classNumber); scanf is a library 11 function that printf("Hello CMPUT %d!\n”, classNumber); reads keyboard input return 0; printf is a library function that prints main must return 0 output to the } if it is successful terminal Compiling a C Program Wait, what does “compile” mean? - C is a compiled language, different from Python which is an interpreted language - With Python, you used an interpreter that translates and executes one statement at a time. It would stop at the first error it meets. - With C, you use a compiler that translates the whole program into object code. After linking this object code, the program can be executed. We will use gcc with the following options to compile: -gcc -Wall -std=c99 hello.c -o hello 12 --o specifies the executable name, otherwise it’s a.out --Wall enables all warnings and -std=c99 specifies the C standard we will compile with demo Compiling a C Program (Behind the Scenes) Source Code Preprocessing (hello.c) Compiling Assembly code Assembling 13 Object code (hello.o) + Libraries Linking Executable (hello) General Form of a C Program Directives start with # Examples include: // headers #include // macros int main(void) { #define PI 3.14 14 } General Form of a C Program Global variables are shared variables and can be accessed by any function in the file int main(void) { Example: int counter = 1; 15 } General Form of a C Program Your program must have a main function This is the entry point to your program, i.e., the main function starts your program Upon successful completion, your main function should int main(void) { return a value of 0. This is called the status code. For now, our main function will 16 not take any parameters (hence the void between parentheses) } General Form of a C Program A function consists of a list of statements Examples: int classNumber, numberOfStudents; int main(void) { numberOfStudents = 30; 17 scanf(“%d”, } &classNumber) In C, every variable must kind of data it will hold return 0; Documenting & Formatting Use indentation to construct blocks Use blank lines to separate logical blocks of code Use or // to add comments that explain the semantics of your code 18 Identifiers Names for variables, functions, macros, etc. Must start with a letter or underscore Case sensitive Some keywords, such as int or union are reserved and cannot be used as identifiers (see Table 2.1 on page 26)19 Try to use names that are self-explanatory and descriptive of the purpose of the variable, function, macro, etc.

Use Quizgecko on...
Browser
Browser