Skip to content

Instantly share code, notes, and snippets.

@Technicus
Created October 7, 2015 04:17
Show Gist options
  • Save Technicus/c8e2baa0226c1dc6aca7 to your computer and use it in GitHub Desktop.
Save Technicus/c8e2baa0226c1dc6aca7 to your computer and use it in GitHub Desktop.
#include <stdio.h>
#include <stdlib.h>
// function declerations
void readString(void);
// function definitions
void readString(){
char *line = NULL; /* forces getline to allocate with malloc */
size_t len = 0; /* ignored when line = NULL */
ssize_t read;
printf ("\n\tEnter string below [ctrl + d] to quit\n\t\tString > ");
read = getline(&line, &len, stdin);
/* Remove trailing newline character from the input buffer if needed. */
if (line[len] == '\n'){
line[len] = '\0';
}
printf ("\n\tRead %zd chars from stdin, allocated %zd bytes for line: \"%s\"\n\n", read, len, line);
}
int main (int argc, char *argv[]) {
readString();
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment