Skip to content

Instantly share code, notes, and snippets.

@rchowe
Created July 15, 2010 02:44
Show Gist options
  • Save rchowe/476426 to your computer and use it in GitHub Desktop.
Save rchowe/476426 to your computer and use it in GitHub Desktop.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
static void name_prompt( char *buffer, size_t len );
int main( void )
{
char name[15];
// Zero the name. In C, arrays are just pulled out of memory without being
// zeroed, so you may end up with junk you don't want.
memset( name, 0, 15 * sizeof( char ) );
name_prompt( name, 15 );
printf( "Ok, your name is %s.\n", name );
}
static void name_prompt( char *buffer, size_t len )
{
int i = 0;
while ( i < len )
{
char c = getchar();
if ( c == '\n' || c == EOF )
break;
buffer[i] = c;
i++;
}
}
@VoX
Copy link

VoX commented Jul 15, 2010

oh very cool

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment