CSE 220 - Exam 1 - Study Guide
CSE 220 - Exam 1 - Study Guide CSE 220
Popular in Programming for Computer Engr
Popular in Computer Science and Engineering
This 8 page Study Guide was uploaded by RianMartins on Sunday September 20, 2015. The Study Guide belongs to CSE 220 at Arizona State University taught by Tadayon-Navabi in Fall 2015. Since its upload, it has received 120 views. For similar materials see Programming for Computer Engr in Computer Science and Engineering at Arizona State University.
Reviews for CSE 220 - Exam 1 - Study Guide
Report this Material
What is Karma?
Karma is the currency of StudySoup.
You can buy or earn more Karma at anytime and redeem it for class notes, study guides, flashcards, and more!
Date Created: 09/20/15
CSE 220 EXAM 1 STUDY GUIDE GNULINUX LAB MANUAL 0 Current Working Directory I An easy way to check what is your current working directory is to use the command quotpwdquot it will show you the absolute path of the directory you are I The dot is used to refer to your current working directory and the notation dot dot is used to refer to the parent directory of your current working directory 0 Ex Suppose I type quotpwdquot and see that I am at afsasueduusers whenever I use dot it refer to the directory quotusersquot and the dot dot refers to quotasuedu directory 0 Relative vs Absolut Path I Relative Path A relative path is defined as path related to the present working directory pwd 0 Ex Suppose I am located in varlog and I want to change directory to varlogkernel I can use relative path concept to change directory to kernel just type quotcd kernel I Absolut Path An absolute path is defined as the specifying the location of a file or directory from the root directory In other words we can say absolute path is a complete path from start of actual filesystem from directory 0 Ex Using the same example above using absolute path I would type quotcd varlogkernel o Create Directory Use the quotmkdir command where is the name of your new directory 0 Create files Use the text editors to create files Just type the name of the text editor and the name of your file remember to specify the format I Pico 0 Ex quotpico testtxt will open the editor pico and after you type the text you want exit quotX and save your file named testtxt I Vi 0 Ex quotvi testtxt does the same of the example above 0 Remove files To remove file just type quotrm where is the name and format of the file that you want to remove 0 Remove Directories I Empty Directory Use the command quotrmdirquot followed by the name of the directory I Full Directory If the directory contain any element you will not able to remove by using quotrmdirquot command In that case you will need to use quotrm rf and the file name It will remove the directory and all its content 0 Globing Wild Cards I star Some cases you need to use globing patterns to remove more than one file for example The means anything 0 Ex f have file01h file02h file11h file111h file3txt and templatec in my current working directory If I type rm f I will remove all the files starting with lowercase F Using rm fh I will remove all the files starting with lowercase F it could have anything after it in its name and have extension h I question mark The is used to replace only one character 0 Ex Suppose I have the same files described above f type quotrm file1h I will remove the filesfile01h and file11h f use quotrm file1h I will remove the files file01hfile11h and file111h 0 Copying Moving Renaming files I Copying To copy files use quotcpquot command 0 cp testtxt This command will copy the file testtxt present in the current working directory and paste in its parent directory 0 cp testtxtusersfred This command will copy the file testtxt present in the current working directory and paste in usersfred directory 0 cp usersbarneydocumentsfootxt This command will copy the file footxt present in under the folder documents of Barney and paste in your current working directory I Moving To move files use quotmvquot command 0 mv testtxt This command will move the file testtxt present in the current working directory to its parent directory 0 mv testtxtusersfred This command will move the file testtxt present in the current working directory to usersfred directory 0 mv usersbarneydocumentsfootxt This command will mve the file footxt present in under the folder documents of Barney to your current working directory I Renaming files To rename a file use the quotmvquot command as well 0 mv testtxt testCompetetxt This command will rename the existing file test txt to testCompete txt Keep in Mind LinuxUnix are case sensitive so the command quotls L is different from quotls l GNULINUX LAB MANUAL Chapter 8 o Redirections to inputoutput I To execute a program and use another file as input use the lt less than sign 0 Ex f00 lt input I To execute a program using another file as input and redirect its output to another file use the gt greater than sign 0 Ex f00 lt input txt gt output txt I You can even redirect the output of a program to be an input of another program using the pipe sign 0 Ex f00 lt inputtxt foo GNULINUX LAB MANUAL Chapter 12 o Compiling programs I To compile a C program type gcc cfilenamec it will produce only the filenameo Compilation only I You can also compile the C program using gccfilenamec it will produce a file aout To run the program type aout I Another form to compile a C program is using gccfilenamec o progl It will compile the file and create an executable progl Type prog1 to run the program C PROGRAMMING BASICS o HelloWord Program Hello World program includeltstdiohgt main printfquotHello Worldquot 0 Preprocessor I Definition The C preprocessor is a macro processor that is used automatically by the C compiler to transform your program before actual compilation It is called a macro processor because it allows you to define macros which are brief abbreviations for longer constructs I The C preprocessor provides some separate facilities one of them is the Inclusion of header files These are files of declarations that can be substituted into your program 0 Header Files A header file is a file with extension h which contains C function declarations and macro definitions and to be shared between several source files There are two types of header files the files that the programmer writes and the files that come with your compiler You request the use of a header file in your program by including it with the C preprocessing directive include like you have seen inclusion of stdioh header file which comes along with your compiler o printf method I The printf function is not part of the C language because there is no input or output defined in C language itself The printf function is just a useful function from the standard library of functions that are accessible by C programs The behavior of printf is defined in the ANSI standard fthe compiler that you are using conforms to this standard then all the features and properties should be available for you int i or d char c float f string 5 0 Assume that we have intintl intint2 charchn float flt string strl string str2 0 Ex printfquotPrint int d intl 0 Ex printfquotPrint char c chr 0 Ex printfquotPrint float f fit 0 Ex printfquotPrint string s str1 o scanf method The scanf function allows you to accept input from standard in which for us is generally the keyboard The scanf function can do a lot of different things but it is generally unreliable unless used in the simplest ways It is unreliable because it does not handle human errors very well But for simple programs it is good enough and easytouse Ex Ex The same table above is applicable for scanf scanf d ampint2 sca nf s ampstr2 Keep in Mind You need to include ltstdiohgt in the top of your c file to be able to use printf and scanf functions C PROGRAMMING PREPROCESSOR 0 Define a Preprocessor macro using define The C preprocessor modifies a source code file before handing it over to the compiler You39re most likely used to using the preprocessor to include files directly into other files or define constants but the preprocessor can also be used to create quotinlinedquot code using macros expanded at compile time and to prevent code from being compiled twice There are essentially three uses of the preprocessordirectives constants and macros Directives are commands that tell the preprocessor to skip part of a file include another file or define a constant or macro Directives always begin with a sharp sign and for readability should be placed flush to the left of the page All other uses of the preprocessor involve processing define39d constants or macros Typically constants and macros are written in ALL CAPS to indicate they are special as we will see 0 Example define PIPLUSONE 314 1 t define a constant PPLUSONE with value 314 1 o Conditional Compilation There are a whole set of options that can be used to determine whether the preprocessor will remove lines of code before handing the file to the compiler They include if elif else ifdef and ifndef An if or ifelifelse block or a ifdef or ifndef block must be terminated with a closing endif The if directive takes a numerical argument that evaluates to true if it39s non zero If its argument is false then code until the closing else elif of endif will be excluded 0 Example ifndef FILENAMEI I define FILENAMEI I code endif ifndef FILENAMEI I 0 Data Types int vs unsigned int 0 Unsigned can hold a larger positive value and no negative value 0 Unsigned uses the leading bit as a part ofthe value while the signed version uses the leftmostbit to identify ifthe number is positive or negative 0 signed integers can hold both positive and negative numbers char vs unsigned char 0 There39s no dedicated quotcharacter typequot in C language char is an integer type same in that regard as int short and other integer types char just happens to be the smallest integer type So just like any other integer type it can be signed or unsigned For variable of type char only 7 bits are available and its range is 127 to 127 2 7 1 For variable of type unsigned char all 8 bits are available and the range is O to 255 2quot8 1 o If you use char as character unsigned key word is completely ignored by the compiler just as comments are removed from your program 0 Scope amp Lifetime of variables see Practice Scope handout for examples available in BB I Lifetime Lifetime of any variable is the time for which the particular variable outlives in memory during running of the program I Scope The scope of any variable is actually a subset of lifetime A variable may be in the memory but may not be accessible though So the area of our program where we can actually access our entity variable in this case is the scope of that variable The scope of any variable can be broadly categorized into three categories 0 Global scope When variable is defined outside all functions It is then available to all the functions ofthe program and all the blocks program contains 0 Local scope When variable is defined inside a function or a block then it is locally accessible within the block and hence it is a local variable 0 Function scope When variable is passed as formal arguments it is said to have function scope C PROGRAMMING C FUNCTIONS amp SCOPE 0 Definitions and Syntaxes I Study chapter 3 of the book C How to Program 7th edition 0 Recursion I A recursive method is a method who calls itself I It should contain a base case and a recursive case 0 Base Case Is the part that check whenever it should get out the loop and return the expected value 0 Recursive case If the result is not right yet this part is responsible to call itself again I Please keep in mind that you must have those two cases and the Base case should be the first condition in your method I Example include ltstdiohgt int factorialint i ifi lt 1 base case always first return 1 return i factoriali 1 recursive case C PROGRAMMING C POINTERS O O Pointer operators I amp refers to memory address I refers to corresponding values Parsing arguments to functions by reference I In order to parse a pointer as argument you have to inform the type of the pointer in the function prototype and function header 0 Ex void cubeByReference int nPtr Relationship between arrays and pointers quotArrays and pointers are intimately related in C and often may be used interchangeably An array name can be thought of as a constant pointer Pointers can be used to do any operation involving array subscripting Assume that integer array b5 and integer pointer variable thr have been defined Because the array name without a subscript is a pointer to the first element of the array we can set thr equal to the address of the first element in array b with the statement thr b This statement is equivalent to taking the address of the array s first element as follows thr ampb 0 Array element b3 can alternatively be referenced with the pointer expression thr 3 The 3 in the expression is the offset to the pointer When the pointer points to the array s first element the offset indicates which array element should be referenced and the offset value is identical to the array subscript This notation is referred to as pointeroffset notation The parentheses are necessary because the precedence of is higher than the precedence of Without the parentheses the above expression would add 3 to the value of the expression thr ie 3 would be added to bO assuming thr points to the beginning of the array Just as the array element can be referenced with a pointer expression the address ampb 3 can be written with the pointer expression thr 3 The array itself can be treated as a pointer and used in pointer arithmetic For example the expression b 3 also refers to the array element b3 In general all subscripted array expressions can be written with a pointer and an offset In this case pointeroffset notation was used with the name of the array as a pointer The preceding statement does not modify the array name in any way b still points to the first element in the array Pointers can be subscripted like arrays If thr has the value b the expression thr 1 refers to the array element b1 This is referred to as pointersubscript notation C How to Program 7th edition Chapter 7 Section 9
Are you sure you want to buy this material for
You're already Subscribed!
Looks like you've already subscribed to StudySoup, you won't need to purchase another subscription to get this material. To access this material simply click 'View Full Document'