Functions, type conversions and visibility Flashcards

1
Q

What are function prototypes?

A

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.

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

What is a header file?

A

A header file is a file that is designed to be used in multiple files and contains function prototypes as well as struct definitions.

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

What is the use of type def?

A

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

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

what is casting or type casting?

A

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)

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

What should you need to watch out for when casting?

A

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,

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

What is declaration and definition in the case of a function and variables?

A

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
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

What are the different ways to define a variable so that they work in multiple files?

A

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.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

What happens when a static variable is defined and used in a function

A

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++;

}

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

what does the following do?

  1. clang -c prog.c
  2. clang prog.o -o prog
  3. clang -c prog.c lib.c
  4. clang prog.o lib.o -o myfile
A
  1. creates an object file prog.o
  2. links the object file prog.o into an executable file prog
  3. compiles source into prog.o and lib.o
  4. links prog.o and lib.o into executable myfile
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

What are make files?

A

Make files contains rules that are used to make a program.

A make file only recompiles any new changes

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

What to watch out for when using make?

A

Use of tabs instead of spaces and also the default target for make is the first target.

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

Which 2 data types can switch statements take in?

Can the switch cases be variables?

A

Integers and enums

Switch statements should be constant

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

What to watch out for when using arrays in C?

A
  • 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.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

What happens when you assign an a value to an array that is not within the bounds?

A

When an array is assigned out of bounds then it leads to a undefined behaviour.

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

What is the null character and what is it used for?

A

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.

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

What is the first command line argument arg[0] given to a program

A

The first argument given to a program, is the program name.

arg[0] is the name of the program.

17
Q

What is #define

A

It is a preprocessor directive and it is used to give aliases. The aliases are replaced by the actual code before the compilation of the code.

18
Q

What are macro functions?

A

define double(X) X*X

Macro functions use simple text based substitution

19
Q

What are some of the uses of macro functions?

A
  • Don’t need to worry about types
  • Useful for things that aren’t valid code, like using anything apart from ints and enums in switch statements.
20
Q

What are header guards?

A

Header guards are macro guards that helps avoid double inclusion when working with the #include preprocessor directive