print_lines 60 .40Write a code fragment that you would have to place in main to convert these command line arguments to an integer and a floating point number respectively.
Solution: C places the command line arguments in the argv array, which is an array of character strings. 60 will be represented as the character string "60" in argv[1] and .40 will be represented as the character string ".40" in argv[2]. We will need to use sscanf to convert these strings to the appropriate values:
// first declare a couple variables to hold the result int num_lines; float proportion_to_print; // use sscanf to do the conversions. I did not require you to type // check the input for correctness but I have done so in the solution // so that you can see how it is done. sscanf returns the number of // values that it was able to correctly convert, so if it converts // one value then the right value was input by the user; otherwise // an incorrect value was input by the user. if (sscanf(argv[1], "%d", &num_lines) != 1) { fprintf(stderr, "num_lines, %s, must be an integer\n", argv[1]); exit(1); } if (sscanf(argv[2], "%f", &proportion_to_print) != 1) { fprintf(stderr, "proportion, %6.2f, must be a floating point number\n", argv[2]); exit(1); }Notice that I used fprintf to print an error message to stderr. stderr is an output stream like stdout and typically the output of stderr and stdout are co-mingled. However, it is good form to write error messages to stderr rather than stdout.
char first_name[10]; char last_name[20]; char save_name[10]; char *temp_name; int length;
length = strlen(first_name) + strlen(last_name);
strcpy(save_name, first_name);
temp_name = strdup(first_name);
temp_name = strchr(first_name, 'a');
temp_name = (char *)malloc(strlen(first_name) + strlen(last_name) + 1); strcpy(temp_name, first_name); strcat(temp_name, last_name);Notice that I added 1 to the sum of the string lengths of first_name and last_name so that there would be room for the string terminating character, \0. I also would not quibble with your answer if you provided an extra space for a blank between first_name and last_name and concatenated a blank space to the end of temp_name after copying first_name to temp_name.
#include <stdio.h> #include <string.h> #include "fields.h" main() { IS input; // the input stream char *min_word = 0; // minimum word found thus far int num_fields = 0; // total number of fields found thus far input = new_inputstruct(0); // get a fields struct for stdin while (get_line(input) >= 0) { num_fields += input->NF; if (input->NF >= 6) { // the instructions did not ask you to print the line number but I have // done so to show you how it is done printf("%d: %s\n", input->line, input->fields[5]); // notice that there are two ways that min_word might get set. It might // not have been initialized, in which case it will be a null pointer, or // the current word might be less than the smallest word seen so far. // The order of the conditional tests is important. If the comparison // with the null pointer is successful, the second test will not be // executed and the conditional will work properly. However, if the // second test were placed first and min_word were null, then the // program would seg fault because strcmp would try to do a comparison // with a null pointer. if ((min_word == 0) || (strcmp(input->fields[5], min_word) < 0)) min_word = strdup(input->fields[5]); } else printf("%d: none\n", input->line); } printf("\nnumber of fields = %d\n", num_fields); if (min_word != 0) printf("smallest sixth word = %s\n", min_word); else printf("none of the lines was longer than 5 words\n"); }
struct employee { int start_year; char name[15]; char *occupation; } struct person *new_employee; struct person employee1; char first_name[10]; char last_name[10]; struct person employee_array[5]; void person_lookup(struct person *e);Consider each of the following statements or group of statements in isolation from the others. Place a checkmark next to any statement that would cause a compiler error and explain why it would cause a compiler error. Place an "X" next to any statement that could cause a runtime execution error and explain what type of error it might cause. Do not assume that any malloc statements have been executed before these statements are executed.
_/ = checkmark
new_employee = (char *)malloc(sizeof(struct person));