Functions, type conversions and visibility Flashcards
What are function prototypes?
A function should be declared ( need not be defined ) before it is used.
(If not declared, then C is will assume the prototype)
Function prototypes are used for this. They are a line of code that can be used to declare a function without the need of the body of the function.
What is a header file?
A header file is a file that is designed to be used in multiple files and contains function prototypes as well as struct definitions.
What is the use of type def?
Type def is used to give new names to types
eg: typedef unsigned int day, month
day or month can be used for the unsigned int type
what is casting or type casting?
Type casting converts values from one type to another type implicitly or explicitly
Storing a float into an int is implicit casting
you can also explicitly type casting to an int by using (int)
What should you need to watch out for when casting?
There is no error checking in casting
unsigned int i = -1
since unsigned int cannot store negative values, in this case, i will be 2^32 - 1
unsigned char c = 300,
What is declaration and definition in the case of a function and variables?
Variables:
- Declaration is specifying the type of the variable without defining it
- Variables can be declared several times but it can only be defined once
Functions:
- Declaration is the prototype
- Prototypes can be specified in different files but the function can be defined in only place
What are the different ways to define a variable so that they work in multiple files?
There are 2 ways to define a variable in multiple files
- Static: The variable is unique to this file
- extern: The variable is defined in some other file, but it is going to be used in this file that defines it.
What happens when a static variable is defined and used in a function
When a static variable is defined inside a function then it is effectively global as in the same value persists when the function is called again.
inf get_number(){
static int i = 0;
i++;
}
what does the following do?
- clang -c prog.c
- clang prog.o -o prog
- clang -c prog.c lib.c
- clang prog.o lib.o -o myfile
- creates an object file prog.o
- links the object file prog.o into an executable file prog
- compiles source into prog.o and lib.o
- links prog.o and lib.o into executable myfile
What are make files?
Make files contains rules that are used to make a program.
A make file only recompiles any new changes
What to watch out for when using make?
Use of tabs instead of spaces and also the default target for make is the first target.
Which 2 data types can switch statements take in?
Can the switch cases be variables?
Integers and enums
Switch statements should be constant
What to watch out for when using arrays in C?
- Arrays are not bounds checked
- No way to get the length of the array
- You cannot return an array from functions
- Cannot assign one array to another array
- Equality check would only check to see if the arrays are the same object.
What happens when you assign an a value to an array that is not within the bounds?
When an array is assigned out of bounds then it leads to a undefined behaviour.
What is the null character and what is it used for?
The null character ‘\0’ is just the 0 of the char type, it denotes the end of a string that is represented by a character array.