C programming 1 Flashcards

this takes material from "C programming a modern approach" book , and sololearn app

1
Q

which system is C language a product of?

A

UNIX operating system

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

what was UNIX programmed in originally? explain the history behind the changes

A

it was originally programmed in Assembly like the other operating systems of that time.
then thompson decided that a higher level language is required for further development of UNIX so he designed a small language named B which was based on BCPL.

after Ritchie joined the UNIX project he started working on an extended version of B and named it “NB” or new B , later as the language diverged more from B he changed the name to “C” , they rewrote the UNIX in C .

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

what language was B based on ?

A

it was based on BCPL

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

what was the old name for C language?

A

it was “NB” or New B ,at first

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

who made the C language?

A

Dennis Ritchie

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

where was UNIX made and by whom ?

A

at Bell Laboratories by Ken Thompson , Dennis Ritchie and others.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

name some 4 C based programming languages and how they relate to C.

A

C++ : includes all C features of C but adds others like classes and more features to support OOP .

Java : is based on C++ and therefore inherits many of C features.

C# :is a more recent language, derived from C++ and Java .

Perl : was a fairly simple language but as it has grown it adopted many features from C language.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

name 3 philosophies of the C language design.

A

1: it is a low level language.
2: it is a small language.
3: it is a permissive language.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

what is meant that C language is low level? how is that important for systems programming?

A

it provides access to low level concepts like addresses and bytes which other languages try to hide.

it also provides operations that correspond closely to machines built-in instructions. so it can be fast as systems cannot afford to be slow.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

how was C languages features kept small?

A

it was kept small by relying on a library of standard functions.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

name 6 strengths of C language.

A

1: its efficient , regarding speed and running with limited memory.
2: its portable .
3: its powerful , it has a large collections of datatypes and operators which gives it power and it can do more with fewer lines.
4: its flexible , as it imposes few restrictions on the use of its features.

5 :has a standard library which has many useful functions.

6: integrated with UNIX and UNIX-like operating systems.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

how did C become portable?

A

1 - it was associated with UNIX and ISO/ANSI early which prevented it from splintering into numerous dialects.

2 - its compliers are small and easily written which makes them widely available.

3- has many features that support portability.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

name 3 weaknesses for C langauge

A

1 : its programs can be error prone.

2 : its programs can be hard to understand.

3 : its programs can be hard to modify.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

name one reason C programs can be error prone.

A

its flexibility can make it error prone

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

name 6 ways you can ensure more effective use of C language .

A

1 : learn its pitfalls and avoid them.

2 : use software tools that make programs more reliable , like Lint for error analysis.

3 : take advantage of existing code libraries.

4 : adopt a sensible set of code conventions.

5 : avoid tricks and overly complex code.

6 : stick to standard.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

what does Lint do?

A

Lint is a tool to check for potential errors .

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
17
Q

how to get “Lint” tool ?

A

it is included as Splint in many linux distributions “secure programming lint” , and it can be download from splint.org for free.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
18
Q

what steps the C program takes to become executable ?

A

preprocessing : the program is given first to the preprocessor which obeys commands preceded by “#” which are known as directives , and makes modifications.

compiling ; the compiler translates to the machine instructions making “object code” .

linking : .the linker includes additional code to make the program executable .

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
19
Q

in gnu/linux how to make the executable version of the file “pun.c” get named “pun” ?

A

we enter the following command in the command line while in the same directory as the file:

gcc -o pun pun.c

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
20
Q

what is an IDE and what can it do?

A

IDE = integrated development environment.

it can edit , compile , link ,execute and debug a program without leaving the environment .

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
21
Q

what are functions in C language , generally?

A

they are like building blocks of programs , known in other languages as subroutines and procedures .

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
22
Q

what are the two types of functions in C based on their source?

A

there are two :

those written by the programmer

and

those provided as a part of C implementation which are known as library functions.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
23
Q

what is a statement in C ?

A

its a command to be run when the program is run .

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
24
Q

what are storage locations called in C ?

A

Variables

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
25
Q

what is the difference between different variable types in general ?

A

they are stored differently.

they have different operations that can be done on them depending on the type.

they can store different sizes or lengths depending on the variable type .

they can be slower or faster to work with , and appropriate or not .

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
26
Q

where to put declarations and statements in main function in different C versions?

A

declarations should come first before statements inside main function.

but in C99 its allowed to not declare until before its needed , just like in C++ and Java .

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
27
Q

what is it called when a variable is given a value?

A

assignment .

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
28
Q

what happens when you divide two integers in C if the result has a number after the decimal point ?

A

the result / output gets truncated or “ rounded down” , it means any numbers after decimal point is lost.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
29
Q

what is an uninitialized variable in C and what happens if you try to access it ?

A

an uninitialized variable is a one that is not given an initial value yet

if you try accessing it you may get a random meaningless number or a program crash.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
30
Q

how to give a constant a name before main function?

A

by using macro definition through the directive “#define” .

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
31
Q

what conventions are there for macro definitions in C?

A

it must be all in CAPS , and should be inside parentheses if it contains an operator in its value.

just for conventional purposes .

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
32
Q

what is meant by identifiers in C ?

A

they are names given to Variables , Functions , Macros and other entities.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
33
Q

what are the rules for identifiers names in C ?

A

they can only contain letters ,numbers and underscores , and should start with a letter or an underscore but not a number.

they are case sensitive.

in C99 they can contain certain “universal characters names” as well .

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
34
Q

what are some conventions for identifiers in C language ?

A

even if C language is case sensitive , variables should be different unless related.

one should either use all small letters and separate words with underscores , or use no underscores and capitalize words after the first word in .

unless they are macros then use all in Caps.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
35
Q

can keywords be used as identifiers ?

A

no , they are reserved .

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
36
Q

what is meant by tokens in C , and what are some of them ?

A

they are basic units that cant be split inside them (using spaces or whitespace characters like tabs and newlines ) without changing their meanings , but you can usually insert whitespace characters between them safely and without limits.

some of them are :

keywords , identifiers , punctuation marks , string literals .

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
37
Q

is splitting a string into lines without tricks legal in C ?

A

no , its illegal .

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
38
Q

what does GCC stand for ?

A

it used to stand for GNU C compiler . but it was extended to include many other languages and now means : (GNU compilers collection )

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
39
Q

what does GNU stand for ?

A

it stands for “GNU is Not UNIX” .

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
40
Q

are : “exit (0) ;” and ‘return 0 ;” equivalents when they are inside main ?

A

yes

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
41
Q

does the compiler remove the comment entirely or replace it with a blank space ?

A

some old compliers did remove the characters entirely but the current standards replace each comment with a single space characters .

42
Q

how to nest a comment inside another in C ?

A

it can only be done since C99 , by nesting // comments inside old style comments /* */

43
Q

why do floating point constants need to end with f letter ?

A

because otherwise they would be of type double which can be larger than float ,and therefore may sometimes end up exceeding the float capacity .

44
Q

explain the length limits for identifiers in C language .

A

they can be arbitrarily long but compilers are only required to read the 31 first characters ( 63 in C99).

BUT if they have external linkage they may only be required to check the first six characters (31 in C99) .

most compilers and linkers are more generous than the standard .

45
Q

how to spot a format specifier inside printf and what does it do ?

A

it starts with “%” and is used to specify how the value is converted from its internal form “binary” to its printed form such as strings of a decimal digits .

46
Q

explain the requirements to write a format specifier inside printf ?

A

it should start with “%” .

should contain a letter after “%” either directly or after following % with an optional minus for left justification then the optional values of : minimum field width , or a dot then the value of precision , or both of them (separated by a dot) .

47
Q

what is minimum field width in format specifiers in printf ?

A

its the minimum number of characters to print , if more is required its automatically expanded , if less were to be printed then blank spaces will precede the field , unless its left justified then spaces will follow it .

48
Q

what is meant by precision in format specifier : %d ?

A

its minimum number of digits to be printed , if there is less , zeroes will precede the value .

49
Q

what is the default value of precision in format specifier %d ?

A

its 1 .

50
Q

what does the format specifier %d do ?

A

it displays an integer in decimal format

(base 10 ) .

51
Q

what does the format specifier %e do ?

A

it displays a floating point number in exponential format .

52
Q

what does precision mean in the format specifier %e ?

A

it specifies how many digits to be displayed after decimal point .

53
Q

what is the default precision value for the format specifier %e?

A

its 6 .

54
Q

what happens if you set precision to 0 for the format specifier %e ?

A

it displays no decimal point.

55
Q

what does the format specifier %f do ?

A

it displays a floating point number with a fixed decimal format , without an exponent .

56
Q

what does precision mean in the format specifier %f ?

A

it means the number of digits to display after the decimal point .

57
Q

what is the default value of precision for the format specifier %f ?

A

its 6 .

58
Q

what does the format specifier % g mean ?

A

displays a floating number in either fixed decimal format or exponential format depending on the numbers size , and g will not show trailing zeroes.

59
Q

what is the meaning of precision for the format specifier %g ?

A

it means maximum number of significant digits .

60
Q

name 4 numerical format specifiers .

A

%d
%e
%f
%g

61
Q

what is used to mark an end of a statement in C ?

A

we use the statement terminator which is a semicolon “ ; “ .

62
Q

why we need to terminate statements in C ?

A

to help the parsers identifying the end of each statement since C statements can be multilinear .

but preprocessing directives do not end with semicolons.

63
Q

how to separate variables if you declare them in one line ?

A

with a comma ,and the last one ends with a semicolon .

64
Q

what does an escape sequence do?

A

an escape sequence enables strings to contain characters that would otherwise cause an error for the compiler.

65
Q

name 7 escape sequences .

A
alert (bell) :  \a
backspace :  \b
newline : \n
horizontal tab : \t
\\
\"
\'
66
Q

what is “&” used for in scanf ?

A

its used to create a pointer to a variable .

which evaluates to the address of the variable that is its operand.

67
Q

what should the programmer check for when using scanf?

A

the programmer must check that the number of conversion specifications matches the number of input variables.

and should check that each specification is appropriate for the corresponding input variable , as the compiler is not required to check for possible mismatches.

68
Q

what does scanf do when it encounters a character in the input that cannot be a part of the current item ?

A

it puts it back to be read again for the next call or the scanning of the next input item

69
Q

what does scanf do if it encounters one or more whitespace characters in the input.

A

it repeatedly reads whitespace characters until it reaches non white space character which is then put back .

70
Q

how does scanf handle the format string?

A

it tries to locate items of the appropriate type in the input for each conversion specifier in the format string.

it will skip blank spaces if necessary and reads the items until it reaches a character that cannot possibly belong to the item then if its reads successfully it will continue processing the rest of the format string , if not successfully then it will return immediately without looking at the rest of the format string or the input data ..

71
Q

what does scanf do if it encounters one or more ordinary characters or whitespace characters in the format string?

A

if the non-whitespace character matches that in the input it discards it from input and continues processing the format string , if the two do not match , it puts the offending character back into the input and aborts without further processing format string or reading more input .

If it’s a whitespace it will match any number of whitespace characters in input.

72
Q

what is the bad practice in this code :

scanf(“%d , %d” , &i , &y) ;

A

it uses a comma inside the format string which is usually a bad idea.

73
Q

what is the difference between %i and %d ?

A

in printf format string there is no difference ,however in scanf format string %d will match integers written in decimal only , while %i will match decimal ,octal or hexadecimal integers.

if a number has 0 as prefix , %i will treat it as an octal ,if it has 0X or 0x prefix it will be treated as a hexadecimal .

74
Q

how to print the % character inside the printf format string ?

A

by using two consecutive % characters “%%”

75
Q

how do we know how far apart tab stops are in C language ?

A

its not possible , it depends on the operating system ,while its typically 8 characters long , C makes no guarantee .

76
Q

what does scanf do if it is asked to scan for a numeric input but the user enters nonnumeric characters ?

A

if the nonnumeric characters appear after the numeric as in “29foo” it will store the numbers in the variable and the nonnumeric “foo” will be left to be read in the next call of scanf .

if the nonnumeric is at the start as in “foo29” , it will quits without storing anything in the variable , and will read “foo’ in the next call .

77
Q

how does scanf “put back” characters and read them again later ?

A

characters are not read as they are written ,instead they are stored in a hidden buffer , to which scanf has access .

78
Q

is C language more known for its emphasis on expressions or statements ?

A

expressions

79
Q

what is meant by expressions in C language ?

A

expressions are formulas that show how to compute a value , they are legal combinations of symbols ., they consist of one operand at least and may contain operators too .

80
Q

what are the simplest expressions in C and how to link them ?

A

the simplest are variables and constants , variables are values that are computed as the program runs , constants are values that do not change .

we use operators to link the basic expressions .

81
Q

what happens if the % operator is used with non integer numbers ?

A

the program wouldn’t compile .

82
Q

what happens if zero is used as the right operand of % or / ?

A

it would cause undefined behaviour .

83
Q

describe the result when / or % are used with a negative operand in C language .

A

if one of two operands used with / is negative they will either be rounded up or down in C89 but in C99 they will always be truncated towards zero .

for % the sign of the result depends on the implementation in C89 , but in C99 it will have the sign of the first operand .

84
Q

how does C deal with implementation defined behaviour ?

A

it is left out for the “implementation” , the software used to compile , link and execute the program on a particular platform to fill in the unspecified parts of behaviour .

85
Q

what results from the existence of implementation defined behaviours in C ?

A

the behaviour of the program may vary somewhat between platforms .

86
Q

why does C have implementation defined behaviours ?

A

for efficiency ,it will need to match the way the hardware behaves .

87
Q

how to take implementation defined behaviour into consideration ?

A

we need to try to avoid creating programs that rely on expecting a certain behaviour from implementation defined behaviours .
if its not possible then we need to check the manual .

88
Q

what are associativity and precedence rules used for in C programming ?

A

precedence rules are used to resolve ambiguity in expressions ,
associativity rules have the same purpose when two operators have the same level of precedence , they either have left associativity or right associativity .

89
Q

what is the order of precedence for unary “+” , “-“ , and binary “+” , “-“ ,”/” , “*” , “%” ?

A

from high level to low level :
1- unary + and - .
2 - * and / and % .
3- binary + and - .

90
Q

how is e treated in the expression v = e if they have different types?

A

the value of e is converted to the type of v and then its assigned to it .

91
Q

do operators have side effect ?

A

some do like the assignment operator , but most do not.

92
Q

how can several assignments be chained together ?

A

a = b = c = 0 ;

93
Q

is the assignment operator right associative or left associative ?
rewrite the following example to illustrate that :
a = b = c = 0;

A

right associative

a = (b = (c = 0))

94
Q

what does ++i and i++ do ? and what is the difference between them?

A

++i increments i by 1 immediately , i++ increments later and will use the old value by now .

95
Q

what are the final values for i , j and k after this code runs:
int i = 1 , j = 2 ;
int k = 0 ;

k = ++i + j++ ;

A

they are equivalent to
i += 1 ;
k = i + j ;
j = j + 1 ;

which will make i , j and k values 2 , 3 and 4 respectively

96
Q

what is the precedence level and associativity for prefix and postfix ++ and - - ?

A

for postfix they have higher precedence than unary minus and plus and are left associative , for prefix version they have same precedence as unary plus and minus and are right associative .

97
Q

order the following operators according to precedence and mention their associativity
: unary + and minus , additive plus and minus , increment and decrement postfixes and prefixes , assignment operators = , += , *= , /= , -= , %= , and multiplicative operators * , / , % .

A

1 : increment and decrement postfixes ++ , - - , and they are left associative .

2 : increment and decrement prefixes and unary plus and minus , ++ , - - , + , - , and they all are right associative .

3 : multiplicative operators , * , / , % , and they all are left associative .

4 : additive plus and minus , + , - , and they are left associative .

5 : assignment operators , = , *= , -= , /= , %= , += , and they are right associative .

98
Q

what does knowing order of precedence and associativity allow us to know about the expressions and what it doesn’t allow us to know ?

A

it allows us to determine uniquely where the parenthesise would go in the expressions if they were all parenthesized .

but they do not determine that we know what the values will be which may depend on what order the values will be evaluated in some cases .

C does not define in what order the subexpressions will be evaluated , except for some involving logical and , logical or , conditional and comma operators .

most expressions have the same value regardless of what order the subexpressions are evaluated in , but not always when a subexpression modifies one of it’s operands .

99
Q

why doesn’t knowing the precedence order and association determine the values of all expressions ?

A

because C does not determine in what order the subexpressions are evaluated in , if there is some subexpression that modifies one of it’s operands , the values may differ depending on the order of evaluation.

100
Q

does C determine in what order subexpressions are evaluated ?

A

no , except for some operators like logical or ,logical and , conditional and comma .