Tag Archive: C


This is a nifty trick you can use to impress your friends.  Imagine you have some smallish program that you want to wrap up into a single file for simplicity.  Part of final output perhaps relies on a clever bit of shell scripting and another part relies on a binary of some sort that you need to get the job done.  Here’s a way to wrap it all up in a tidy little package:

#!/bin/sh
grep SRC: $0 | grep -v “grep SRC:” | sed -e s/SRC://g > /tmp/hello.c
gcc -o hello /tmp/hello.c
./hello
exit
SRC: #include <stdio.h>
SRC: int main(int argc, char *argv[])
SRC: {
SRC: printf(“Hello World\n”);
SRC: return 0;
SRC: }

Everything below exit is completely ignored by your shell.  The grep statement goes through the shell script line by line and returns the all the lines starting with “SRC: ” to sed.  Sed uses a simple regex to strip the “SRC: ” off your source code and sends the output to a file in your /tmp directory.  Then the file is compiled and ran.  You could go further and delete the source file out of the /tmp directory if you like or don’t bother.

Hello, world is the most basic of all test programs.  It’s one of the first steps to determine if you have your development environment set up correctly and is also a good first program to learn in any language because it will give you a good idea of the bare minimum requirements to build a program in a given language.

BASIC

Let’s start with Hello, World in basic since that was the first programming language I ever encountered.  The code is very simple:

10 PRINT “Hello, World”

RUN

10 is a line number which is a requirement of old basic compilers.  Generally the lines are numbered  10, 20, 30, etc although they don’t have to be.  Part of the reason for this is so you can later insert other line numbers into your code without renumbering all of your lines of code.  Newer BASIC implementations have sub-routines and other features have have allowed them to ditch the line numbers.  The program is executed from top to bottom so everything in basic has to be in order.  The command RUN at the bottom runs the program and will display your text.  It is not actually part of the program.

Ruby

In Ruby, there are two easy ways to do a hello world program.  I’ll assume you are using a Unix-based machine to try this example.  You’ll need to create a text file called hello.rb with a text editor.  The first way is:

puts ‘Hello, World’

Save your file and type ruby hello.rb at the command prompt.  If all went well, it should print Hello, World and then give you back your command prompt.  I’m assuming that you have installed ruby and it’s in your path.  On a Debian system, it’s easy to install ruby if it’s not already there.  Use a command such as sudo apt-get install ruby.  The other ruby hello world example is:

print(“Hello, World\n”)

C

Hello, World in C is far more complex than any of the other examples.  This is because C is highly structured and not very forgiving.  It also doesn’t have the printf function in the core language so the standard input and output library must be included.  I will also assume you are using a Unix environment for this example.  If you try to run this in a dos/windows environment it will probably fail.  Here is the hello world code in C:

#include <stdio.h>

int main(int argc, char *argv[])
{
printf(“Hello World\n”);
return 0;
}

To run it, you’ll need to put the code in a text file called hello.c.  Then type in gcc hello.c at the command prompt.  This should generate a file called a.out.  At the command prompt, type ./a.out to run the program.  Different compilers and environments may give different results.

Bash

The last example I’ll give is for bash scripting.  Some people would argue that bash is not a programming language but nevertheless it’s a very powerful way to accomplish tasks with other programs and it’s a good way to automate repetitive tasks.  Here is hello world in bash:

#!/bin/sh

echo “Hello, World”

To run this you could simply type the bottom line at the command prompt.  Instead, save it to a file called hello.sh.  Now before you run it, you’ll need to set the executable bit.  Type chmod 755 hello.sh at the command prompt.  Now you can type ./hello.sh to execute the script.

Powered by WordPress. Theme: Motion by 85ideas.