Chapter 2 Questions Flashcards
What is the meaning of the #include statement?
it tells the compiler what libraries need to be linked
What is the difference in a library (.h) that is between <> verses between “”?
<> is a prewritten library
“” is a user defined library
What is the library stdio.h for?
contains standard input and output functions such as scanf and printf
What is the library math.h for?
contains mathematical functions such as sqrt and pow
What are the four (4) valid options to define the main() function in C?
main(){…}
void main(){…}
int main(){… return 0;}
main(int argc, char *argv[]){…}
When you have a main() function defined with a type int, what does the statement “return 0;” mean?
the program ran as expected/ no errors or exceptions
In the statement “scanf(%d, &i)”, why do we use “&” in the second parameter?
to store the scanned value at the address?
What is the purpose of the %d, %f, %c, %s, and %p in the printf() function?
they are
What are the two (2) types of variables, scope-wise, that we have in C?
local and global
If C does not have boolean type variables, how are relational operations (needed in conditions and loops) handled?
if it returns a 0 it is false, if it returns any other number it is true
How many basic data types are there in C?
5 (int, double, float, char, void)
How is the scope of a variable defined in C?
it starts at the declaration and goes until the end of the block defined by {}
When declaring a variable/function in C, what does the rule “declaration-before-use” mean?
you cannot use a variable ahead of where it is declared (scope)
What is a forward declaration and why do we want to use it?
What are long, short, signed, and unsigned?
modifiers
What is the initial default value of an integer variable when it is defined as int x?
What is the initial default value of an character variable when it is defined as char a?
What is the initial default value of an float variable when it is defined as float x?
C does not have a byte data type, is there an equivalent type for it?
yes, char
What is the issue with trying to define an array as int a[] or char []?
the length of the array isn’t given so memory cannot be allocated?
What is the function of the null character (‘\0’) when working with strings in C?
it represents the end of the string
While working with strings in C, if we have char s1[3], s2[]; can we do s1 = s2? Yes or no and why?
How would you describe a pointer in terms of what is stored in them?
a pointer stores an address
How would you describe a pointer in terms of the type of memory they use?
Is it possible to have pointers to any type in C?
yes
What is the result of combining these two operands?
(&y)
&(y)
There are two operators for pointers, * and &. What is the purpose of each of them?
- returns the value stored in the variable to which the attached variable points to
& returns the address of the variable to which it is attached
Why is it said that an array can be seen as a pointer? Justify in terms of the information stored in the pointer and array.
In C, there is no string type, however, there are ways to implement strings? What are they?
an array of char
While defining a pointer to a character and initializing it with a string literal, it is said that the string is immutable. What does that mean and would the pointer also be immutable?
char *str = “Hello World”;
when a string is immutable, it cannot be changed during runtime, with the exception of using pointers
no the pointer would be mutable in this example
Given the following definition of an array, give the same array using pointers.
int array[] = {1, 2, 3, 4, 5};
Provide a visual representation of the above array and pointers.
*(array + 0) = 1;
*(array + 1) = 2;
*(array + 2) = 3;
*(array + 3) = 4;
*(array + 4) = 5;