Hello Script for C Language

C – the root of all modern language’s syntax. Here is the hello script for C. A ‘Hello Script‘ is a file that contains the most commonly used elements of a programming language so that it can be used as a cheat sheet when working with that language.

C is a general-purpose, block structured, procedural, imperative computer programming language developed in 1972 by Dennis Ritchie. Although C was designed for implementing system software, it is also widely used for applications. It is widely used on a great many different software platforms and computer architectures, and several popular compilers exist.

Wikipedia

Unfortunately, I am not an expert in C – I prefer dynamic languages over static ones. So if you find any problems in the script, please let me know in the comments.


#include <stdio.h>
#include <string.h>

// Function declaration
void hello(char name[]) {
 	char result[50] = "Hello, ";
 	strcat(result, name);
 	printf("%s\n", result);
}

int main() {
	int i;
	// Printing(IO)
	printf("Hello World!\n");
	
	// Variables, concatenation
	char name[] = "Binny";
	int year = 2008;
	printf("Hello, %s - welcome to %d\n", name, year);
	
	//If, else
	if (year > 2008) {
		printf("Welcome to the future - yes, we have flying cars!");
	}
	else if(year < 2008) {
		printf("The past - please don't change anything. Don't step on any butterflies. And for the sake of all that's good and holy, stay away from your parents!");
	}
	else {
		printf("Anything wrong with your time machine? You have not gone anywhere, kiddo.");
	}
	printf("\n");

	// For loop
	for(i=0; i<3; i++) {
		printf("%d) Hi there!\n", i);
	}
	
 	//Numerical Array, while
 	char rules[3][20]; //I am sure there is a better way of doing this using poiters. If you know, please leave a few 'pointers' in the comment.
 	strcpy(rules[0], "Do no harm");
 	strcpy(rules[1], "Obey");
 	strcpy(rules[2], "Continue Living");
 	i=0;
	while(i<3) {
		printf("Rule %d : %s\n", i+1, rules[i]);
		i++;
	}
	
	// Struct - nearest thing C has to an associated array
	struct structure {
		char hello[20];
		int number;
		char foo[20];
	} associated, new_associated;
	strcpy(associated.hello, "world");
	associated.number = 1337;
	strcpy(associated.foo, "bar");
	printf("hello: %s\n", associated.hello);
	printf("number: %d\n", associated.number);
	printf("foo: %s\n", associated.foo);

	// Function calling
	hello("John Oldman");

	// Writing to a file
 	FILE *out;
 	out = fopen("/tmp/Hello.txt", "w");
 	char *str = "Hello From C";
 	fputs(str, out);
 	fclose(out);
	
	// Reading and displaying a file.
	FILE *in;
	in = fopen("Hello.c", "r");
	if (in) {
		while (!feof(in))
			printf("%c",fgetc(in));
	}
	fclose(in);

	// Run a system command.
	system("ls");

	return 1;
}
About Binny V A 102 Articles
Freelance Web Developer on the LAMP Platform. Maintains a blogs http://blog.binnyva.com/. Other sites include Bin-Co and OpenJS

4 Comments

Leave a Reply to heyme Cancel reply

Your email address will not be published.


*