0 - Essential C Flashcards
What can printf do on standard out?
If you just have a string, you can just put it inside the parentheses: #include printf("Hello World");
If you want many arguments, you need to use the conversion sequence: %d or %i - int %s - char* %x - int (hexadecimal) %p - void * (prints as a pointer)
For example,
int i;
for (i = 0; i
How can you use printf to print to another place, such as standard error?
Use fprintf(), which takes as its first argument the stream you’re printing on:
- fprintf(stderr, “Fatal Error #2212. We’re hosed”);
- fprintf(stderr, “Fatal Error in foo(): value of bar is %p\n”, bar);
Notice that cin becomes stdin, cout becomes stdout, and cerr becomes stderr.
What does printf (“String %s integer %d float %5.2f\n”, s, i, f) print?
It prints the string, then the string s. then “integer” and whatever integer comes after. The “5.2” portion says to use a field 5 characters wide, and to print 2 digits to the right of the decimal point.
How does printf verify the argument types?
Actually, printf has NO way of verifying the argument types. If you pass an integer where the format requires a pointer, you’ll get weird output and possibly a core dump. Just be careful!
How does fputs() work?
Fputs() allows us to write to a stream. int fputs(const char *str, FILE *stream)
fputs() takes a null-terminated string ‘str’ and writes it to ‘stream’. It omits the null character when it does this. It returns a non-negative integer if okay and EOF on error.
if ((fputs(“Hello world”, stdout)) == EOF)
fprint(stderr, “Whoops, something went wrong”);
How does fgets() work?
char *fgets(char *buffer, int size, FILE *stream)
Fgets() reads up to size-1 characters from stream and stores them in buffer. Fgets() stores the null character after the last character read into the buffer and returns ‘buffer’ if everything works fine, or NULL on error or EOF.
char *cptr; char buffer[256]; printf("Enter some stuff:\n"); cptr = fgets(buffer, 256, stdin); if (cptr != NULL) printf("You typed: %s\n", cptr)
How can you use fgets() to return a pointer to the first non-whitespace character?
Using the buffer ‘in’, you can read up to MAX_LINE - 1 characters into cptr. Then, you can increment cptr until you have skipped all the whitespace.
char* cptr if (cptr = fgets(in, MAX_LINE, stdin)) { while (*cptr == ' ' || *cptr == '\t') cptr++; return cptr; }
What are some C++ syntax not available in C?
- Classes
- new and delete
- stream operators
- the // comment character
- the bool keyword
- casting operators
- standard libraries like iostream
How do you dynamically allocate memory for individual values?
void *malloc (int nbytes)
Malloc() takes an int indicating the number of bytes you want allocated and it returns a void pointer to the start of the allocated memory.
int foo;
foo = (int) malloc (sizeof(int));
*foo = 5;
How do you dynamically allocate an array of values?
You just allocate the number of elements times the size of the element.
int *foo;
foo = (int ) malloc(sizeof(int)5);
foo[0] = 17;
foo[4] = 22;
How do you delete dynamically allocated memory?
void free(void *);
For example, if you wanted to free the pointer foo: int *foo; foo = (int*) malloc(sizeof(int)); *foo = 5; free(foo);
Where must you declare variables in C?
You MUST declare them at the beginning of a function BEFORE any other code, including counter variables!!
e.g. you CANNOT say
for (int i = 0; i
How must you declare constants in C?
You MUST use #define.
#define MAX_LEN 1024 #define NUM_CALLS 20
What is the preferred way to use structs in C?
Although you can simply define structs as “struct point {int x; int y;};”, you SHOULD instead use pointers.
struct point *p;
p = (struct point *) malloc(sizeof(struct point));
p->x = 0;
p->y = 0;
Also, MUST use the “struct” qualifier anytime you refer to structs. However, you can use typedefs to eliminate the repetition.
typedef struct point Point;
Point *p;
p = (Point *) malloc(sizeof(Point));
p->x = 0;
What do you use instead of booleans in C?
Instead, just use 0 for false and 1 for true. For example, this is an infinite loop:
while(1)
{ ; }