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.

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;
}

First you have to create the ‘hello world’ application. This is more than just ‘print “Hello World”‘ – it will act as a cheatsheet for you until you familiarize yourself with the language. The point of this application is to use all the most commonly used elements of a language and putting it in a single place so that you can refer to it later.

Your first job is to go to the tutorial, the manual and the internet until you create the Hello World application with the following stuff in it. I will provide an example – how the ‘hello world’ application will look in PHP.

After each step run the application and make sure it works.
Print “Hello World\n”
Why do you think we call it a hello world application? Write the code to print a string ‘hello world’ and save it to a file. Now run it using the interpreter – and make sure it works – see it in action.

// Printing(IO)
print "Hello World!\n";
	
Comments
Put a single line comment(like //) on top of the “print ‘hello world’” code and if your language supports it, a multiline comment(/* – */) as well. Run the script to make sure the comments work as advertised.


// Single line comment.
/*
Mulitline comment.
*/
	

I was just kidding about running the script to test the comments – you did’nt do it, did you?

Use a variable(a string and an integer) and a concatenation operator.
Create a variable called name(string) – and give it your name as the value. Create another integer variable and give it the current year. You don’t have to find it programatically – just put it as 2008 or something. Now you have to print out the a string “Hello, <name> – Welcome to year <$year>”. This string will let you concatenate two different type variable to a string – and it makes me feel like a time traveler ;-) .

Yeah – the value of variable name should appear at <name>. Use the concatenation operator if possible – in PHP, write

print "Hello, " . $name . " - welcome to year " . $year;

instead of

print "Hello, $name - welcome to year $year";

This is because different languages have different concatenation operators. The other stuff is the same almost universally. + refers to addition. – refers to subtraction. * is multiplication. My point is that if you know the operators for one language, you know the operators for almost all languages. But the concatenation operator differs from language to language – its ‘+’ in javascript, ruby. Its ‘.’ in Perl, PHP, etc.

There are exceptions to this rule – ‘=’ is the equality operator in SQL while it all other language its ‘==’. The assignment operator is ‘:=’ in pascal – in all other it is ‘=’. If you notice any difference in any operator write some code using that operator in this section.

Use if/else if/else
Create something like this pseudo code…

if (year > 2008) {
	print "Welcome to the future - yes we have flying cars!"
}
else if(year < 2008) {
	print "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 {
	print "Anything wrong with your time machine? You have not gone anywhere, kiddo."
}

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s


%d bloggers like this: