C Flashcards

1
Q

How do you define a const?

A

const int = 4;

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q
  1. How do you define a typedef?
  2. What is the advantage of using typedef?
A
  1. typedef float dollars;
    • now float and dollars are interchangable
  2. It makes the code more readable and it’s easy to change a number type later, it just needs to be changed in one place
    • It makes the program more portable (different machines with different int values etc)
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

how do you use malloc?

A

int a[] = (int*)(malloc(sizeof(int);

struct * Node newNode = malloc(sizeof(struct * Node);

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

Why/When would you use a structure?

A

Structures are for:

  • limited number of elements
  • of varying type
  • which need to be kept together in one place
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

How do you declare a structure?

How do you access a member of a structure?

A

struct friendStr(name of type) sarah(name of variable);

members are accessed with the ‘.’ operator

sarah.name (would give access to the value of sara’s name).

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q
  1. How do you initialize a structure?
  2. How do you access a structure?
A

struct friendStr{

char name[MAXNAME];

long phoneNumber;

};

struct friendStr sarah;

scanf(“%s”, sarah.name);

scanf(“%ld”,sarah.phoneNumber);

printf(“Name is %s\n”,sarah.name);

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

How do you use typedef with a struct? Why would you implement this?

A

Two ways:

  1. typdef struct friendStr friend; (standalone)
  2. typedef struct freindStr{… } friend;(in the declaration of struct)

Implementing this reduces the ackwardness of always having to type struct friendStr friend; for each new declaration.

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

How do you copy the contents of one struct into another?

A

Easy!

struct StudentRec studA, studB;

studA = stubB;

It’s underlying library function copies all the elements from one struct into another. This is the ONLY instance this = operator works like this.

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

How would you take advantage of a structs ‘=’ ability to copy contents of arrays?

A

struct { int a[]; } a1, a2;

a1 = a2;

/* legal, since a1 and a2 are structures/*

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

How do you pass a struct to a function by value?

A

void printRecord(Student item){

}

int main(){

Student stundetA = {“Guass”, 99.0);

printRecord(stundentA);

return 0;

]

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

How do you pass a struct to a function as a pointer?

A

void readStudent(Stundet* s){

printf(“enter name and id”);

scanf(“%s”, (*s).lastname)

scanf(“%f”, &((*s).mark);

}

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

How do you parse(tokenize) a string?

A

char text[] = “123,abc,def”;

char * token;

token=strtok(text,”,”);

while(token != NULL){

printf(“%s\n”,token);

token=strtok(NULL,”,”);

}

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

How does tokenizing a string work?

A

It cotinues to work on the original string. It puts a terminating character at the delimiter, it then starts at the next character after the terminating character.

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

What are enumerations and how do you use them?

A

Enumerations permit the declaration of named constants in a more convenient and structured fashion than does #define; they are visible in the debugger, obey scope rules, and participate in the type system.

enum Suit{HEARTS, DIAMONDS, CLUBS, SPADES};

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