Bin-Blog logoBin-Blog

Learn about the latest in Web Development – as soon as I do.

Hello Script for C Language

By • Jul 4th, 2008 • Category: Scripts, Tutorials, Web 2.0

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

4 Responses »

  1. Niyaz PK says:

    Good one.
    This should have come first.

  2. ym.xiao says:

    You must declare the var before any statement, please check it

  3. Dhananjoy says:

    The comments ‘// Function declaration’ should be changed with ‘// Function definition’

    visit http://chakra2009.blogspot.comg

  4. heyme says:

    hey guys see hw can we run a c program without main function…
    to see visit on http://technoease.com/520/programming/c-programming/can-we-run-c-program-without-main-function/

Leave a Reply

XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>