C Beginnings Flashcards
(42 cards)
How would you get input from the user in C?
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
Issue with scanf()?
It doesn’t get the whole line, you need to use fgets.
Scanf only reads until the first whitespace
How to properly use fgets()?
fgets(variable, size of variable, stdin);
Issue with fgets()?
It automatically includes a \n
How to get rid of the fgets() \n?
import <string.h></string.h>
name[strlen(name)-1] = ‘\0’
Need to
1. import the string
2. make the last digit equal to ‘\0’
When would you want to use fgets over scanf?
When you want to accept whitespaces
How to get math functions?
include <math.h></math.h>
How would you make a switch statement?
switch(grade){
case ‘A’:
printf(“Nice”);
break;
default:
printf(“LOL);
}
What do you have to have at the end of every switch case?
A break line.
What do you have to add if none of your cases work just in case?
a default: case
Show me a ternary operator
(condition) ? value if true : value if false
What is a function prototype?
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
strlwr(string)
Converts a string to lowercase
strupr(string)
Converst a string to uppercase
strcat(String1, String2);
Appends string 2 to string 1
strncat(string1, string2, 1);
Appends n characters from string2 to string 1
strcpy(string1, string2)
What would strcpy(Nick, Heath) do?
Copy string2 to string1
Make Nick = Heath
strncpy(string1, string2, 4);
copy n characters of string2 to string1
What is something interesting about these string functions?
They are pointers that change the original variable
strset(string1, ‘?’);
sets all characters of a string to a given character
strnset(string1, ‘x’, 1);
Sets first n chracters to a string to a given character
strrev(string1)
reverses a string
strlen(string1);
returns length of string
strcmp(string1, string2);
string compare all characters
Return 0 if the same
Return something else if not the same