C Beginnings Flashcards

(42 cards)

1
Q

How would you get input from the user in C?

A

scanf(“%d”, &age);

The %d specifies what data type you want
The &age is the location in memory of where you will store your data

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

Issue with scanf()?

A

It doesn’t get the whole line, you need to use fgets.

Scanf only reads until the first whitespace

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

How to properly use fgets()?

A

fgets(variable, size of variable, stdin);

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

Issue with fgets()?

A

It automatically includes a \n

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

How to get rid of the fgets() \n?

A

import <string.h></string.h>

name[strlen(name)-1] = ‘\0’

Need to
1. import the string
2. make the last digit equal to ‘\0’

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

When would you want to use fgets over scanf?

A

When you want to accept whitespaces

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

How to get math functions?

A

include <math.h></math.h>

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

How would you make a switch statement?

A

switch(grade){
case ‘A’:
printf(“Nice”);
break;
default:
printf(“LOL);
}

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

What do you have to have at the end of every switch case?

A

A break line.

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

What do you have to add if none of your cases work just in case?

A

a default: case

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

Show me a ternary operator

A

(condition) ? value if true : value if false

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

What is a function prototype?

A

The function’s call method with the required arguments.

  • makes sure when a function is called, it has the correct arguments
  • C doesn’t check valid arguments were entered so you need these prototypes for it to work
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

strlwr(string)

A

Converts a string to lowercase

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

strupr(string)

A

Converst a string to uppercase

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

strcat(String1, String2);

A

Appends string 2 to string 1

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

strncat(string1, string2, 1);

A

Appends n characters from string2 to string 1

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

strcpy(string1, string2)

What would strcpy(Nick, Heath) do?

A

Copy string2 to string1

Make Nick = Heath

18
Q

strncpy(string1, string2, 4);

A

copy n characters of string2 to string1

19
Q

What is something interesting about these string functions?

A

They are pointers that change the original variable

20
Q

strset(string1, ‘?’);

A

sets all characters of a string to a given character

21
Q

strnset(string1, ‘x’, 1);

A

Sets first n chracters to a string to a given character

22
Q

strrev(string1)

A

reverses a string

23
Q

strlen(string1);

A

returns length of string

24
Q

strcmp(string1, string2);

A

string compare all characters

Return 0 if the same
Return something else if not the same

25
strncmp(string1, string2, 1);
string compare n characters
26
strcmpi(string1, string1);
string compare all (ignore case)
27
strnicmp(string1, string1, 1);
string compare n characters (ignore case)
28
How do we get a array of strings in C?
We need a 2D array
29
char cars[][10] Explain the 10
Each element is 10 chars wide
30
How to iterate a string of arrays?
Use a for loop, for(int i = 0, i < sizeof(cars)/sizeof(cars[0]); i++) printf("%s\n", fruits[i]);
31
How to update a string value in a string array?
Use the strcpy function
32
for(int i = 0, i < sizeof(cars)/sizeof(cars[0]); i++) printf("%s\n", cars[i]); explain this
The sizeof(cars)/sizeof(cars[0]) The first sizeof gets the total bit length of the cars array. For example, if cars[][10] = {4, 3, 2, 1} then each string is 10 bits long and since we have four of them we have 40 bits. The Second sizeof, gets the length of the first one which is 10, which is also true for all of them. Thus when divided we have 4 cars to loop through The printf() then prints out each car since it is a pointer. Note* When it prints out it prints out until it reaches the '\0' which is automatically added at initialization.
33
I have a variable age, how do you declare a pointer to point to age?
int *ptr = &age;
34
I have a ptr to age, how do I print out the age with a ptr?
printf("%d", *ptr);
35
How accept a pointer for a funciton's argument
void accept(int *ptr);
36
If you don't assign a pointer, what is good practice for this?
Assign it to a null
37
How to open a file?
FILE *file = fopen("text", "Mode"):
38
What should you do after done with a file and how to do it?
Close! fclose(fileName);
39
How to print to a file?
fprintf(fileName, "text");
40
How to read from a file?
fgets(whereToReadTo, size, fileName);
41
What is Array Decay?
When we send an array to a function, the array becomes a pointer and could end up messing up whats inside our array since we are changing the values in the array
42