COP 3223C Exam 2

¡Supera tus tareas y exámenes ahora con Quizwiz!

while loop

-Repeats a statement or group of statements while a given condition is true. -It tests the condition before executing the loop body. -Here, the key point to note is that a while loop might not execute at all. - When the condition is tested and the result is false, the loop body will be skipped and the first statement after the while loop will be executed.

return array from function

-Second point to remember is that C does not advocate to return the address of a local variable to outside of the function -Therefore you would have to define the local variable as static variable. -The scandalous truth is that C has no arrays — that they are merely cleverly disguised pointers.

passing arrays as function arguments

-Similarly, you can pass multi-dimensional arrays as formal parameters with some extra rules -One important thing for passing multidimensional arrays is, first array dimension does not have to be specified. -The second (and any subsequent) dimensions must be given

break statement

-Terminates the loop or switch statement and transfers execution to the statement immediately following the loop or switch. -When a break statement is encountered inside a loop, the loop is immediately terminated and the program control resumes at the next statement following the loop. -It can be used to terminate a case in the switch statement -If you are using nested loops, the break statement will stop the execution of the innermost loop and start executing the next line of code after the block.

The Auto Storage Classes

-The auto storage class is the default storage class for all local variables -The example below defines two variables with in the same storage class -'auto' can only be used within functions, i.e., local variables.

for loop

-The condition is now evaluated again. -If it is true, the loop executes and the process repeats itself (body of loop, then increment step, and then again condition). -After the condition becomes false, the 'for' loop terminates.

nested loops

-The syntax for a nested for loop statement in C is as follows -The syntax for a nested while loop statement in C programming language is as follows -The syntax for a nested do...while loop statement in C programming language is as follows

call by value

-This method copies the actual value of an argument into the formal parameter of the function. -In this case, changes made to the parameter inside the function have no effect on the argument. -By default, C programming uses call by value to pass arguments. -In general, it means the code within a function cannot alter the arguments used to call the function.

call by reference

-This method copies the address of an argument into the formal parameter. -Inside the function, the address is used to access the actual argument used in the call. -This means that changes made to the parameter affect the argument. -To pass a value by reference, argument pointers are passed to the functions just like any other value. -So accordingly you need to declare the function parameters as pointer types

calling a function

-To call a function, you simply need to pass the required parameters along with the function name, and if the function returns a value, then you can store the returned value.

goto statement

-Transfers control to the labeled statement. -A goto statement in C programming provides an unconditional jump from the 'goto' to a labeled statement in the same function. -NOTE •Use of goto statement is highly discouraged in any programming language •It makes difficult to trace the control flow of a program, •It makes the program hard to understand •It makes the program hard to modify •Any program that uses a goto can be rewritten to avoid them.

initializing local and global variables

-When a local variable is defined, it is not initialized by the system, you must initialize it yourself. -Global variables are initialized automatically by the system when you define them as follows -It is a good programming practice to initialize variables properly, otherwise your program may produce unexpected results, because uninitialized variables will take some garbage value already available at their memory location.

do...while loop

-A do...while loop is similar to a while loop, except the fact that it is guaranteed to execute at least one time.

function declarations

-A function declaration tells the compiler about a function name and how to call the function. -The actual body of the function can be defined separately. -A function declaration has the following parts -Parameter names are not important in function declaration only their type is required, so the following is also a valid declaration

array in detail

-Arrays are important to C and should need a lot more attention. -The following important concepts related to array should be clear to a C programmer •Multi-dimensional arrays •Passing arrays to functions •Return array from a function •Pointer to an array

Bitwise Operators

-Assume A = 60 and B = 13 in binary format, they will be as follows •A = 0011 1100 (12) •B = 0000 1101 (61) •----------------- •A&B = 0000 1100 (49) •A|B = 0011 1101 (60) •A^B = 0011 0001 (240) •~A = 1100 0011 (15)

passing arrays as function arguments

-If you want to pass a single-dimension array as an argument in a function, you would have to declare a formal parameter in one of three ways •Formal parameters as a pointer •Formal parameters as a sized array •Formal parameters as an unsized array -All three declaration methods produce similar results because each tells the compiler that an integer pointer is going to be received.

Extern Storage Classes

-The extern modifier is most commonly used when there are two or more files sharing the same global variables or functions as explained below.

declaring arrays

-To declare an array in C, a programmer specifies the type of the elements and the number of elements required by an array as follows -This is called a single-dimensional array. -The arraySize must be an integer constant greater than zero and type can be any valid C data type. -One of the most common implementations of arrays in C is used to represent a string -C has no built-in datatype for strings, instead make an array of characters

function arguments

-While calling a function, there are two ways in which arguments can be passed to a function •Call by value Call by reference

for loop code

for ( init; condition; increment) { statement(s); }

local and global variables

-A program can have same name for local and global variables but the value of local variable inside a function will take preference. -It is highly recommended not to do this as it adds confusion to the program and is difficult to maintain

passing arrays as function arguments

-When we pass arrays into functions, the compiler automatically converts the array into a pointer to the first element of the array. -When you call a function with an array as an argument, you must omit the square brackets and pass only the name of the array -If you keep the square brackets, the compiler assumes that you meant only to pass a single element and that you forgot to specify which one

calling a function

-While creating a C function, you give a definition of what the function has to do. -To use a function, you will have to call that function to perform the defined task. -When a program calls a function, the program control is transferred to the called function. -A called function performs a defined task and when its return statement is executed or when its function-ending closing brace is reached, it returns the program control back to the main program.

initializing arrays

-You can initialize an array in C either one by one or using a single statement as follows -You can initialize an array in C either one by one or using a single statement as follows -The number of values between braces { } cannot be larger than the number of elements that we declare for the array between square brackets [ ]. -If you omit the size of the array, an array just big enough to hold the initialization is created. -The last statement assigns the 5th element in the array with a value of 50.0. -All arrays have 0 as the index of their first element which is also called the base index and the last index of an array will be total size of the array minus 1

nested if statements

-You can use one if or else if statement inside another if or else if statement(s). -It is always legal in C programming to nest if-else statements, which means you can use one if or else if statement inside another if or else if statement(s). -You can nest else if...else in the similar way as you have nested ifstatements.

pointer to array

-balance is a pointer to &balance[0], which is the address of the first element of the array balance -assigns p as the address of the first element of balance -It is legal to use array names as constant pointers, and vice versa. -Therefore, *(balance + 4) is a legitimate way of accessing the data at balance[4]. -Once you store the address of the first element in 'p', you can access the array elements using *p, *(p+1), *(p+2) and so on.

initializing two dimensional arrays

-Multidimensional arrays may be initialized by specifying bracketed values for each row. Following is an array with 3 rows and each row has 4 columns -Multidimensional arrays may be initialized by specifying bracketed values for each row. Following is an array with 3 rows and each row has 4 columns -The nested braces, which indicate the intended row, are optional. The following initialization is equivalent to the previous example

for loop

-Next, the condition is evaluated. -If it is true, the body of the loop is executed. -If it is false, the body of the loop does not execute and the flow of control jumps to the next statement just after the 'for' loop.

do...while loop

-Notice that the conditional expression appears at the end of the loop, so the statement(s) in the loop executes once before the condition is tested. -If the condition is true, the flow of control jumps back up to do, and the statement(s) in the loop executes again. This process repeats until the given condition becomes false

Operator Precedence

-Operator precedence determines the grouping of terms in an expression and decides how an expression is evaluated. -Certain operators have higher precedence than others -For example, the multiplication operator has a higher precedence than the addition operator

Match the storage class to its definition.

auto- is the default register- to define static- instructs extern- to give a reference

do...while loop

do {statement(s); } while(condition);

Extern Storage Class Code

extern int count; void write_extern(void) { printf("", count); }

Extern Storage Class Code

int count ; extern void write_extern(); main() { count = 5; write_extern(); }

defining a function code

return_type function name(){ body of function }

What does a storage class define regarding variables?

visibility life-time scope

while loop code

while(condition) { statements(s); }

The Auto Storage Classes Code

{ int mount; auto int month; }

The Register Storage Class Code

{ register int miles; }

arrays

• A specific element in an array is accessed by an index. •All arrays consist of contiguous memory locations. •The lowest address corresponds to the first element and the highest address to the last element.

function

•A function declaration tells the compiler about a function's name, return type, and parameters. A function definition provides the actual body of the function. •A function can also be referred as a method, sub-routine or a procedure depending upon what type of software is being developed and the language

functions

•A function is a group of statements that together perform a task. •Every C program has at least one function, which is main(), and all the most trivial programs can define additional functions. •Divide up code into separate functions. •How to divide up code among different functions is up to the programmer •Logically the division is such that each function performs a specific task.

return type

•A function may return a value. •The return_type is the data type of the value the function returns. •Some functions perform the desired operations without returning a value. •In this case, the return_type is the keyword void.

looping

•A loop statement allows us to execute a statement or group of statements multiple times. •The general form of a loop statement in most of the programming languages

parameters

•A parameter is like a placeholder. •When a function is invoked, you pass a value to the parameter. •This value is referred to as actual parameter or argument. •The parameter list refers to the type, order, and number of the parameters of a function. •Parameters are optional; that is, a function may contain no parameters.

scope rules

•A scope in any programming is a region of the program where a defined variable can have its existence and beyond that variable it cannot be accessed. •There are three places where variables can be declared in C programming language -Inside a function or a block which is called local variables -Outside of all functions which is called global variables -In the definition of function parameters which are called formal parameters

Storage Classes

•A storage class defines the scope (visibility) and life-time of variables and/or functions within a C Program. •They precede the type that they modify. •We have four different storage classes in a C program -auto -register -static extern

Operators

•An operator is a symbol that tells the compiler to perform specific mathematical or logical functions. •C language is rich in built-in operators and provides the following types of operators -Arithmetic Operators -Relational Operators -Logical Operators -Bitwise Operators -Assignment Operators Misc Operators

arrays

•Arrays a kind of data structure that can store a fixed-size sequential collection of elements of the same type. •An array is used to store a collection of data, but it is often more useful to think of an array as a collection of variables of the same type. •Instead of declaring individual variables, such as number0, number1, ..., and number99, you declare one array variable such as numbers and use numbers[0], numbers[1], and ..., numbers[99] to represent individual variables.

Decision Making

•C programming language assumes any non-zero and non-null values as true, and if it is either zero or null, then it is assumed as false value.

looping

•C programming language provides the following types of loops to handle looping requirements. -while loop -for loop -do...while loop -nested loops

Decision Making

•Decision making structures require that the programmer specifies one or more conditions to be evaluated or tested by the program •Along with a statement or statements to be executed if the condition is determined to be true, and optionally, other statements to be executed if the condition is determined to be false.

defining a function

•Defining a Function -The general form of a function definition in C programming language is as follows - - - -A function definition in C programming consists of a function header and a function body.

while loop

•Here, statement(s) may be a single statement or a block of statements. •The condition may be any expression, and true is any nonzero value. The loop iterates while the condition is true. •When the condition becomes false, the program control passes to the line immediately following the loop.

functions

•The C standard library provides numerous built-in functions that your program can call. •For example, -strcat() to concatenate two strings -memcpy() to copy one memory location to another location -printf() outputs data to the command prompt -scanf() reads user input from the command prompt

function body

•The function body contains a collection of statements that define what the function does.

Decision Making

•The general form of a typical decision making structure found in most of the programming languages

function name

•This is the actual name of the function. •The function name and the parameter list together constitute the function signature.

nested switch statement

•You can use one switch statement inside another switchstatement(s). •It is possible to have a switch as a part of the statement sequence of an outer switch. •Even if the case constants of the inner and outer switch contain common values, no conflicts will arise.

looping

•You may encounter situations, when a block of code needs to be executed several number of times. •In general, statements are executed sequentially: -The first statement in a function is executed first, followed by the second, and so on. •Programming languages provide various control structures that allow for more complicated execution paths.

for loop

•for loop -Executes a sequence of statements multiple times and abbreviates the code that manages the loop variable. -allows you to efficiently write a loop that needs to execute a specific number of times

infinite loop

-A loop becomes an infinite loop if a condition never becomes false. -The for loop is traditionally used for this purpose. -Since none of the three expressions that form the 'for' loop are required, you can make an endless loop by leaving the conditional expression empty. -When the conditional expression is absent, it is assumed to be true. -You may have an initialization and increment expression, but C programmers more commonly use the for(;;) construct to signify an infinite loop. -NOTE − You can terminate an infinite loop by pressing Ctrl + C keys.

switch statement

-A switch statement allows a variable to be tested for equality against a list of values. -A switch statement allows a variable to be tested for equality against a list of values. -Each value is called a case, and the variable being switched on is checked for each switch case.

for loop

-After the body of the 'for' loop executes, the flow of control jumps back up to the increment statement. -This statement allows you to update any loop control variables. -This statement can be left blank, as long as a semicolon appears after the condition.

accessing two dimensional array elements

-An element in a two-dimensional array is accessed by using the subscripts, i.e., row index and column index of the array. -The below statement will take the 4th element from the 3rd row of the array. -Frequently programmers used nested for loops to access two-dimensional arrays

accessing array elements

-An element is accessed by indexing the array name. -This is done by placing the index of the element within square brackets after the name of the array. -The below statement will take the 10th element from the array and assign the value to salary variable.

If...Else If...Else Statement

-An if statement can be followed by an optional else if...else statement, which is very useful to test various conditions using single if...else if statement. -When using if...else if..else statements, there are few points to keep in mind − -An if can have zero or one else's and it must come after any else if's. -An if can have zero to many else if's and they must come before the else. -Once an else if succeeds, none of the remaining else if's or else's will be tested.

If...Else Statement

-An if statement can be followed by an optional else statement, which executes when the Boolean expression is false. -If the Boolean expression evaluates to true, then the if block will be executed, otherwise, the else block will be executed.

If Statement

-An if statement consists of a boolean expression followed by one or more statements. -If the Boolean expression evaluates to true, then the block of code inside the 'if' statement will be executed. -If the Boolean expression evaluates to false, then the first set of code after the end of the 'if' statement (after the closing curly brace) will be executed.

nested loops

-C programming allows to use one loop inside another loop. -You can use one or more loops inside any other while, for, or do..while loop. -You can put any type of loop inside any other type of loop. -For example, a 'for' loop can be inside a 'while' loop or vice versa.

return array from function

-C programming does not allow to return an entire array as an argument to a function. -However, return a pointer to an array by specifying the array's name without an index. -To return a single-dimension array from a function, you would have to declare a function returning a pointer

continue statement

-Causes the loop to skip the remainder of its body and immediately retest its condition prior to reiterating. -The continue statement in C programming works somewhat like the break statement. -Instead of forcing termination, it forces the next iteration of the loop to take place, skipping any code in between. -For the for loop, continue statement causes the conditional test and increment portions of the loop to execute. -For the while and do...while loops, continue statement causes the program control to pass to the conditional tests.

formal parameters

-Formal parameters, are treated as local variables with-in a function and they take precedence over global variables.

function declarations

-Function declaration is required when you define a function in one source file and you call that function in another file. -In such case, you should declare the function at the top of the file calling the function.

global variables

-Global variables are defined outside a function, usually on top of the program. -Global variables hold their values throughout the lifetime of your program and they can be accessed inside any of the functions defined for the program. -A global variable can be accessed by any function. -That is, a global variable is available for use throughout your entire program after its declaration.

goto statement

-Here label can be any plain text except C keyword and it can be set anywhere in the C program above or below to goto statement.

function arguments

-If a function is to use arguments, it must declare variables that accept the values of the arguments. -These variables are called the formal parameters of the function. -Formal parameters behave like other local variables inside the function and are created upon entry into the function and destroyed upon exit.

Static Storage Class

-In C programming, when static is used on a global variable, it causes only one copy of that member to be shared by all the objects of its class.

do...while loop

-It is more like a while statement, except that it tests the condition at the end of the loop body. -Unlike for and while loops, which test the loop condition at the top of the loop, the do...while loop in C programming checks its condition at the bottom of the loop.

The Register Storage Class

-It should also be noted that defining 'register' does not mean that the variable will be stored in a register. -It means that it MIGHT be stored in a register depending on hardware and implementation restrictions. [

loop control statements

-Loop control statements change execution from its normal sequence. -When execution leaves a scope, all automatic objects that were created in that scope are destroyed. -C supports the following control statements. •break •continue goto

Extern Storage Class

-The extern storage class is used to give a reference of a global variable that is visible to ALL the program files. -When you use 'extern', the variable cannot be initialized however, it points the variable name at a storage location that has been previously defined. -When you have multiple files and you define a global variable or function, which will also be used in other files, then extern will be used in another file to provide the reference of defined variable or function. -Just for understanding, extern is used to declare a global variable or function in another file.

Arithmetic Operators

-The following arithmetic operators supported by the C language •+ - adds two operands •- - subtracts second operand from first •* - multiples two operands •/ - divides numerator by denominator •% - remainder after integer division •++ - increments integer by one •-- - decrements integer by one

Assignment Operators

-The following assignment operators supported by the C language •*= - Multiply AND assignment operator; it multiplies the right operand with the left operand and assigns the result to the left operand. •/= - Divide AND assignment operator; it divides the left operand with the right operand and assigns the result to the left operand. •%= - Modulus AND assignment operator; it takes modulus using two operands and assigns the result to the left operand.

Assignment Operator

-The following assignment operators supported by the C language •<<= - Left shift AND assignment operator. •>>= - Right shift AND assignment operator. •&= - Bitwise AND assignment operator. •^= - Bitwise exclusive OR and assignment operator. •!= - Bitwise inclusive OR and assignment operator

Assignment Operators

-The following assignment operators supported by the C language •= - Simple assignment operator; assigns values from right side operands to left side operand •+= - Add AND assignment operator; it adds the right operand to the left operand and assign the result to the left operand •-= - Subtract AND assignment operator; it subtracts the right operand from the left operand and assigns the result to the left operand.

Bitwise Operators

-The following bitwise operators supported by the C language •Bitwise operator works on bits and perform bit-by-bit operation. •The truth tables for &, |, and ^ is as follows

Logical Operators

-The following logical operators supported by the C language •&& - called Logical AND operator, if both the operands are non-zero, then the condition becomes true •|| - called Logical OR operator; if any of the two operands is non-zero, then the condition becomes true •! - called Logical NOT operator; used to reverse the logical state of its operand; If a condition is true, then Logical NOT operator will make it false

Miscellaneous Operators

-The following miscellaneous operators supported by the C language •sizeof() - Returns the size of a variable.. •& - Right shift AND assignment operator. •* - Pointer to a variable. •?: - Ternary operator; conditional expression; If Condition is true ? then value X : otherwise value Y

Relational Operators

-The following relational operators supported by the C language •== - checks if two operands are equal •!= - checks if two operands are not equal •> - checks if left operand is greater than right operand •< - checks if left operand is less than right operand •>= - checks if left operand is greater or equal to than right operand •<= - checks if left operand is less than or equal to right operand

switch statement

-The following rules apply to a switch statement − •Not every case needs to contain a break. If no break appears, the flow of control will fall through to subsequent cases until a break is reached. •A switch statement can have an optional default case, which must appear at the end of the switch. The default case can be used for performing a task when none of the cases is true. No break is needed in the default case.

switch statement

-The following rules apply to a switch statement − •The expression used in a switch statement must have an integral or enumerated type, or be of a class type in which the class has a single conversion function to an integral or enumerated type. •You can have any number of case statements within a switch. Each case is followed by the value to be compared to and a colon. •The constant-expression for a case must be the same data type as the variable in the switch, and it must be a constant or a literal.

switch statement

-The following rules apply to a switch statement − •When the variable being switched on is equal to a case, the statements following that case will execute until a break statement is reached. •When a break statement is reached, the switch terminates, and the flow of control jumps to the next line following the switch statement.

for loop

-The init step is executed first, and only once. -This step allows you to declare and initialize any loop control variables. -You are not required to put a statement here, as long as a semicolon appears. •

Register Storage Classes

-The register storage class is used to define local variables that should be stored in a register instead of RAM. -This means that the variable has a maximum size equal to the register size (usually one word) and can't have the unary '&' operator applied to it (as it does not have a memory location). -The register should only be used for variables that require quick access such as counters.

two dimensional arrays

-The simplest form of multidimensional array is the two-dimensional array. -A two-dimensional array is, in essence, a list of one-dimensional arrays. -To declare a two-dimensional integer array of size [x][y], you would write something as follows -A two-dimensional array can be considered as a table which will have x number of rows and y number of columns. -A two-dimensional array a, which contains three rows and four columns can be shown as follows

multidimensional arrays

-The simplest form of the multidimensional array is the two-dimensional array. -However, the number of dimensions has no limit

Static Storage Class

-The static storage class instructs the compiler to keep a local variable in existence during the life-time of the program instead of creating and destroying it each time it comes into and goes out of scope. -Therefore, making local variables static allows them to maintain their values between function calls. -The static modifier may also be applied to global variables. -When this is done, it causes that variable's scope to be restricted to the file in which it is declared.


Conjuntos de estudio relacionados

Python and Socket Programming Vocab

View Set

Chapter 10: Paired Samples t Test

View Set

Care of the Client with Impaired Renal Function

View Set

CHM/lab operation practice questions

View Set

The Scientific Revolution & Enlightenment

View Set