Week 2 Flashcards Preview

CLA - C Progr Lang Cert Assoc > Week 2 > Flashcards

Flashcards in Week 2 Deck (91):

An operator is ...

~ a symbol of the programming language which is able to operate on the values, such as e.g. + or =


What is the relationship between dividend and divisor?

5 / 2 = 2.5
5 is the dividend
2 is the divisor


What is going to happen if your function tried to divide by 0?

- it is strictly forbidden
- you will get a compilation error and won't be able to run the program
- the error might not be shown to you, your program might just terminate abnormally


The '-' operator can be both, a ... and a ... operator

unary, binary


What is a unary operator?

A unary operator expects only one argument. For example, a '-' can denote a negative number. In this case, it is unary. In an arithmetic operation involving subtraction it might be binary.


10 - 8 = 2
10 is the ...
8 is the ...

minuend, subtrahend


To use a '+' sign as a unary operator is possible, that is ... You could use it to ... But it is in most cases ...

syntactically correct, preserve the sign, redundant


Multiplication ... addition in mathematics. C preserves this hierarchy of ...

precedes, priority


What determines the order of computations performed by operators with equal priority?



If we have several operators with equal order of priority which order of computations does C use?

left-sided binding (from left to right)


How do you calculate the remainder of a division in C?

- use the modulus operator:

int i,j,k


-> the remainder of i / j is k, that is 13 / 5 R 3, k = 3


What does the plus plus operator in C do?

- it increments by 1
- it would e.g. replace x = x + 1 by x++


How does C decrement a variable by 1?

- you can decrement the traditional way: DaysLeft = DaysLeft - 1
- or use C's own way: DaysLeft--


'Variable++' is called ...

... post-increment operator.


'--Variable' is called ...

... pre-decrement operator.


'++Variable ' is called ...

... pre-increment operator.


'Variable--' is called ...

... post-decrement operator.


What do post-increment/decrement operators do as opposed to the opposite?

- return the original (unchanged) variable's value and then increment/decrement the variable by 1
- variable is modified first and then it's value is used


What do pre-increment/decrement operators do as opposed to the opposite?

- Increment/decrement the variable by 1 and return its value already increased/reduced
- variable's current value is used and then modified


What is the value of i and j?

int i,j;




What is the value of i and j?

int i,j;




What is the order of priority for arithmetic and C-specific operators?

highest - 1): ++ -- + - (unary)
2: * / %
3: + - (binary)
4: < <= > >=
5:== !=
lowest - 6): = += -= *= /= %=


What is the binding for prefix and postfix operators?

Prefix: right to left
Postfix: left to right


What is the C shortcut operator for the following expression?

i = i + 2 * j;

i += 2 * j;


What is the C shortcut operator for the following expression?
Var = Var / 2;

Var /= 2;


What is the C shortcut operator for the following expression?

Rem = Rem % 10;

Rem %= 10;


What is the C shortcut operator for the following expression?

j = j - (i + Var + Rem);

j -= (i + Var + Rem)


What are floating numbers?

~ store numbers that have a non-empty decimal fraction


When can you omit zero?

- when it's the only digit in front or after the decimal point
- the decimal point must be there to denote a floating-point number! (4. float vs 4 int)


Very large or very small numbers can be displayed in ...

... scientific notation.


Express 240000000 in scientific notation.



Write -0.000000523 in scientific notation.



With what word are floating-point numbers declared?



What happens when we’re forced to convert integer values into float values?

- can cause a lack of accuracy
- the transformation affects the internal (machine) representation of those values, as computers use different methods for storing floats and ints in their memory


What happens when we’re forced to convert float values into integer values?

- will result in a loss of accuracy
- might not be feasible as integer variables have a limited capacity


What happens when a number conversion from float to int reaches maximum capacity?

- variable will be unable to store such a large value and there will be a loss of accuracy
- but result of assignment unclear either max. permissible value or random value or an error will occur
= implementation dependent issue (one side of portability)


A word is essentially a ...

... string of characters.


In C all strings are treated as ...

... arrays.


To store and manipulate characters, the “C” language provides a special type of data. What is this?



How do you assign string values to a variable called Character?

1) put the character/s in apostrophes:
Character = 'A';
2) use the ASCII code for A: Character = 65;


Why should you not use ASCII codes for char?

- it's illegible for someone who doesn't know ASCII
- computers that don't use ASCII can't work with it


What is ASCII?

= American Standard Code for Information Interchange
~ it's a universally accepted standard for character encoding
- provides space for 256 different characters


The difference between the ASCII code for an uppercase letter and the same letter but in lower case is ..., which is the code for ...

32, whitespace


What types of characters does the ASCII code describe?

-punctuation marks
-control characters that control input/output devices


What kind of character coding framework do many IBM mainframes use?

EBCDIC (Extended Binary Coded Decimal Interchange Code)


What is a literal?

~ a symbol which uniquely identifies its value / a literal means itself


Is "CountBirds" a literal?

No, it is a variable and by looking at it you can't guess it's value.


Is "B" a literal?

Yes, because when you look at it you realise it's value straight away.


Is "i + 235" a literal?

It is a combination of a variable and a literal, called an expression.


What is the backslash \ used for in C?

- it is an escape character: you can escape from the normal meaning that follows the slash
- e.g. Character = "\" allows you to escape from the normal meaning of the apostrophe to code the apostrophe itsself


How do you put the symbol backslash into a char variable?

Character = "\\" essentially you are escaping from the usual meaning of a backslash as an escape character


There are other escape characters. \n denotes a ... and is sometimes called an ..., as printers react to this character by pulling out the ...

- transition to a new line, LF (Line Feed)


What is \r and what does it do?

- denotes the return to the beginning of the line
- sometimes called a CR (Carriage Return)


What is \a and what does it do?

- officially called BEL (for bell)
- it is an alarm tone
- turns on a teletypes ringer


What does the escape character \0 do?

- called nul
- is a character that does not represent any character


What are octal digits?

~ digits from the range of 0 to 7 used in ASCII


A different way to escape is using an octal ...The sequence \ooo means you can specify any character in the ... character set as a three-digit octal character code. Octal escape sequences can never be longer than ... and are ... by the first character that is not an octal digit. You must use at least ... digit.

escape sequence, ASCII, 3 digits, terminated, 1


The sequence ... allows you to specify any ASCII character as a hexadecimal character code. You must use at least ... digit for a hexadecimal escape sequence, but you can omit the second and third digits. Unlike octal escape constants, the number of hexadecimal digits in an escape sequence is ... A hexadecimal escape sequence terminates at the first character that is not a ... Because hexadecimal digits include the letters a through f, care must be exercised to make sure the escape sequence terminates at the ...

\xhhh, 1, unlimited, hexadecimal digit, intended digit


What is the relationship between the char and int type? (4)

- char type is treated as a special kind of int type
- you can assign a char value to an int variable, and vice versa
- but expect a loss of value from int to a char variable if the value exceeds 255 (the top-most character code in ASCII)
-value of the char type can be subject to the same operators as the data of type int


How can we check if 2 values are equal?

- with the (equal equal) operator ==
- e.g. 2==2 true, 3==1 false


How can we ask if a value is unequal to another value?

DaysRemaining != 0


How would we code, are there more black sheep than white sheep?

- same as in mathematics:
BlackSheep > WhiteSheep


How do you use the greater than or equal operator in C?

4 >= 3


How do you memorise/store the answer from a greater or equal question?

int Answer, Value1, Value2

Answer = Value1 >= Value2

If Value2 is 5 and Value1 is 7, the statement is true.

So, Answer will be 1.

If Value2 is 3 and Value1 is 2, the statement is false.

So, Answer will be 0.


How can we give a computer an instruction to take a specific action if a condition is met and abstain from taking action if that condition is not met? (4)

- through a conditional instruction / conditional statement

if(expression that is either true or false) instruction;

- if the expression is true (non-zero) the instruction will be executed
- if the expression is false (0) the instruction is omitted


Code the following intention.

"If the temperature is below 0 degrees, we go ice skating and then we will have some mulled wine at the stalls."

if(Temperature<0) GoSkating(); DrinkMulledWine();


How do you include more than one instruction in a conditional statement?

- with a compoud statement or block

if($){(instruction1); (instruction2);}


How can you make sure your code doesn't run over the margins of the editor?

- have to apply styles
- most common is the K & R style (Kernighan and Ritchie style)


What is input?

~ sending data in the direction from human (user) to the computer program


The stream of data transferred in the opposite direction, i.e. from the computer to the human, is called ...



What is the impairment of the puts function?

- you can't use it if you want to print an integer value or a single character


What does the printf function do?

This function can easily output several values of different types and mix them with the text.


What requirements does printf have? (3)

- doesn’t specify how many arguments must be provided
- there must be at least one argument
- only the first argument has a strict type (it must be a string); all subsequent arguments may be of any type


If there’s a percent symbol inside the format, the printf considers it a ... on how to proceed with the arguments following the format. The first hint refers to the ... argument after the format, the second to the ..., etc. (but there are some exceptions to this simple rule).

hint, first, second


What is a specifier?

~ % joined with some other character


... specify that the following argument is of type int.

%d or %i


What does %x specify?

- the following argument is of type int and expressed as a hexadecimal number


What does %o specify?

- specifies that the corresponding argument is a value of type int and should be presented as a fixed-point octal number


... (as in ...) specifies that the corresponding argument is a value of type int or char and should be presented as a ...

%c, char, character


What is the specifier called that specifies that the corresponding argument is a value if type float presented as a floating-point number.



If the % sign is a specifier, how do you display a percent sign normally?

- two percent signs %% mean only one of them will be displayed


Code the following:

The text "This house was built in ... Its width is ..." is to be displayed. The two blanks contain two variables which need to be declared first, Year 1982 and Width 10.54. The computer then retrieved those variables and displays them in their proper place.


int main(void) {

int Year;
float Width;
Year = 1982;
Width = 10.54;

printf("This house was built in %d. Its width is %f.", Year, Width);



What happens when you code this snippet:

printf("%d %d %d", i, j);

- there are more specifiers in the format than corresponding arguments
- modern compilers recognise this error, older ones might not and will display a random value


What happens when you code this snippet: (3)

printf("%d", i, j);

- there are fewer format specifiers than arguments
- will not cause an error, the argument with the missing specifier will simply not be displayed
- compiler might point out missing specifier


What happens when you code this snippet:

printf("%d", i, j);

- there are fewer format specifiers than arguments
- will not cause an error, the argument with the missing specifier will simply not be displayed
- compiler might point out missing specifier


What happens when you code this snippet:

int i;
i = 100;
printf("%f", i);

- specifier provides a different type to corresponding argument
- if compiler doesn't notice mismatch, a random number will be displayed


What happens when you code this snippet: (3)

char c;
c = '?';
printf("%d", c);

- variable is declared as a char, specifier demands an int
- char is treated as int, so this won't cause an error
- printf will output the ASCII code of that character which was used as the corresponding argument


Encoding all data needed inside the source code is called ...

hard coding


What function similar to printf allows the user to enter data?



What are the differences between printf and scanf?

- scanf requires that the variable in which the user data is stored must be explicitly specified (no formulas)


How can you calculate the squareroot of a value?

value * value