C Flashcards

1
Q

What is the main method in C?

A

-Starting point of your C application
-Returns an int
-If returned value is not provided, defaults to return 0

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

What is 0 indicate by convention?

A

Valid exit

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

What does a non-zero indicated by convention

A

Some kind of error to the calling program

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

What are C programs complied into?

A

Object code by a compiler

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

What does the pre-processor cpp do ?

A

It examines the source code before the compiler:
-it identifies any special cpp directives
-it re-writes source code into an intermediate source file
-it passes the new source file to the compiler

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

What are the 3 forms of cpp directives?

A

1.File inclusion:
-#include <file>: paste a file directly into your source code
2.Macros:
-#define <content>: these are pure textual substitutions
3.Conditional inclusions:
-#ifndel <label></label></content></file>

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

What is important to remember about cpp directives?

A

They are not part of the C language. Gcc never knows anything about them, because it only sees the extended output created by cpp.

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

What is an example of a standard header file ?

A

stdio.h

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

Why is it important to have header files?

A

They contain declarations for various functions–> declarations provide basic type information for the compiler.
Since the compiler compiles each source file independently, it does not know that this function is properly defined in another source file.

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

What does #include <stdio.h> do?</stdio.h>

A

It includes a declaration for the printf() function

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

What is the error “Symbol could not be resolved” mean?

A

You have probably forgotten the associated header file.

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

What is the basic form use of printf()?

A

printf(formatting string, variable list);

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

What are the 5 main C data types?

A

d :decimal integer
f :floating point value
s :character string
c :individual character
p :pointer address value

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

C is a … language?

A

An imperative, procedural language

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

What are imperative languages?

A

They use a series of statements, each of which can modify the state of the program.

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

What is the difference between imperative and declarative languages?

A

Imperative –> HOW to produce its output
Declarative(SQL)–> WHAT output you would like to see

17
Q

What is a procedural language?

A

It indicates that programming logic can be encoded as procedures or sub-routines that can be called from other locations in the code.
MOST BASIC FORM OF STRUCTURED PROGRAMMING

18
Q

What do OO languages do that imperative procedural languages don’t?

A

OO languages –>use methods to provide sub-routines + couple data with the methods that operate on them.
C IS NOT OBJECT ORIENTED LANGUAGE

19
Q

What is the gcc invocation used for?

A

To compile several source files into a single executable

20
Q

What is a header guard?

A

They ensure that only one copy of this header file gets included in your source file.

21
Q

What is the -Wall flag used for?

A

It prints all warnings

22
Q

What is different about C methods from Java methods?

A

There are no public/private methods. –> all functions are externally visible
We can use static to tell the compiler that this function can only be referenced/invoked from the source file in which it is written.

23
Q

What is the main difference between C and Java(architecture)?

A

Java is standardized across all CPU architectures i.e int is always 4 bytes
C isn’t i.e int may be 2,4 or 8 bytes

24
Q

How do we determine the byte count of a given primitive type?

A

Using sizeof()

25
Q

What is the main difference between C and Java(arrays)?

A

Java –> bounds checking to make sure you don’t index outside of the array. RUN-TIME ERROR
C–> no bounds checking on arrays

26
Q

How does C compensate for having no classes/data?

A

Using struct:
-its like a class definition without any methods
* usually included in header files so other functions can use them

27
Q

What are the 3 ways that memory can be set aside for data in C?

A

1.Static
2.Automatic
3.Dynamic

28
Q

What is static allocation?

A

-It occurs when the compiler knows that a variable will exist for the lifetime of the program. –> because variable is declared outside of any function
* THERE IS ONLY 1 COPY of this variable so the compiler can set aside memory for this at compile time

29
Q

What is automatic allocation?

A

-It occurs automatically at run-time
-Used for variables that are defined inside a function
-Exist only while the function is executing
How it works: a run-tike component will set up memory for these variables when the function is called and when the function finishes, all memory is released.

30
Q

What happens if the memory isn’t released when the function finishes in automatic allocation?

A

Memory leaks:
-if they are produced each time a function is called, a long running program will eventually run out of memory and crash

31
Q

Where is the function memory?

A

On the process STACK:
-each function has its own stack frame
-in each frame are each of the local variables in the function
-STACK grows and shrinks as the program runs

32
Q

What is dynamic allocation?

A

-Memory is allocated at run-time only as you ask for it.
-Memory will be taken from a pool of memory called the heap that is set aside for each process.
-The heap grows as required

33
Q

What are the 3 basic basic memory allocation functions?

A

1.malloc(MOST COMMON) : allocate some number of bytes of uninitialized memory
2.calloc: similar to malloc except it initializes the memory to 0
3.realloc: re-allocate a previous memory block to a new size* used for re-sizing arrays

34
Q

What does * refer to in C?

A

It refers to a pointer value which is a reference to a memory allocation.

35
Q

What is important to remember about the addresses generated by malloc?

A

They are logical addresses:
-they represent the relative position of a data element in the current process space.
-not an actual physical address in memory

36
Q

What are the memory regions for all 3 types of allocations?

A

Static:
-in a fixed location near the bottom of the space
Automatic:
- STACK is at the top of the space and grows downward
Dynamic:
-HEAP is in the lower region of the space and grows upward

37
Q

What happens when you try to access an invalid memory location?

A

The OS will abort your application and print a segmentation fault error.

38
Q

What is special about manipulation of character strings in C?

A

Strings are just arrays of characters accessed with a char*
Strings end with a terminating character :
-NULL OR \0
-not part of the string length