Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdlib.h>
- #include <stdio.h>
- #include <string.h>
- /*
- * Basically this program was simply made as practice
- * The only job of the program is to "echo" what the user inputs back,
- * And possibly print a number, if it was there.
- */
- int main() {
- int a_num = 0;
- int boofer_size = 200;
- // Use calloc to allocate an array which can hold 200 characters
- char *boofer = calloc(sizeof(char), boofer_size);
- // Did the allocation fail?
- if(boofer == NULL) {
- // Skip to the end of the program after printing a warning
- printf("Failed to allocate buffer!");
- goto end;
- }
- // Use fgets() to get the user input
- // I heard this was the safest way to do it
- char* success = fgets(boofer, boofer_size, stdin);
- if(success != NULL) {
- // If we succeeded, then use sscanf_s() to get leading number
- int items_assigned = sscanf_s(boofer, "%d", &a_num);
- printf(boofer);
- if(items_assigned == 1) {
- // If we got the number, print it.
- printf("%d", a_num);
- }
- // Successfully free the memory knowing nothing could go wrong??
- free(boofer);
- }
- // Return 0 to indicate everything went fine
- end:
- return 0;
- }
Add Comment
Please, Sign In to add comment