Sample C programming code. How to get input from the console.

Posted: May 25, 2012. At: 3:30 PM. This was 6 years ago. Post ID: 3918
Page permalink.
WordPress uses cookies, or tiny pieces of information stored on your computer, to verify who you are. There are cookies for logged in users and for commenters. These cookies expire two weeks after they are set.

This is a sample C program showing how to define a function as well as accepting input from the command-line. Hopefully this is useful for someone who is writing a console application for Linux or UNIX.

#include <stdio.h>
#include <string.h>
int main(int argc, char **argv) {
	char *name;
	name = argv[1];
	if (strlen(name) < 5) {
		printf("Name too short.\n");
	printf("%s\n", name);
	return 0;

Compile the program like this.

localhost% cc msg.c

And run it with some input on STDIN to get the proper output.

localhost% ./a.out jimbob

This works very well, this is how easy it is to get input into a C program.

No comments have been made. Use this form to start the conversation :)

Leave a Reply