Posted: . At: 3:30 PM. This was 12 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.

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

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
jimbob

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

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.