GTK4 Tutorial Pt. 1

Monday, February 27, 2023 | Permalink

Viewpoint may be largely unrelated to its usefulness. Many if not most of the software we use is probably obsolete according to the latest design criteria. Most users could probably care less if the internals of the operating system they use is obsolete. They are rightly more interested in its performance and capabilities at the user level. Ken Thompson, The Tanenbaum-Torvalds Debate

This note or tutorial is written as part of the learning process. So, obviously it will have errors in terms of grammar, typo, code, explanation and so on. You are welcome to suggest a correction. The note is concise and contains numerous reference (archive) links for detail/better explanation. Enjoy!


Create a new file with name hello.vala. Here, hello is the name of the file and .vala is the extension of the Vala programming language.

$ touch hello.vala

(Of course, you can create this new file in many different ways. I wrote command to make instruction clear!)

Open the created hello.vala in text-editor of your choice and write the text hello, world as that's what we're interested to print.

hello, world

This hello, world text is string in Vala. String in Vala must be enclosed within double quotes only (single quotes are for characters).

"hello, world"

The string hello, world is not going to be printed in terminal by default. We need to invoke printf() method from stdout object to print the string in terminal. Don't worry about this stdout object for now. It is default available in any project to use.

stdout.printf("hello, world")

Vala is one of those programming language that requires semicolon or ; to terminate the statement.

stdout.printf("hello, world");

Vala, being compiled programming language requires main() method to start the execution.

int main() {
  stdout.printf("hello, world");
}

We need to return 0 from the main() method at the end as we have already mentioned int as return type of this method.

int main() {
  stdout.printf("hello, world");

  return 0;
}

Finally, let's add \n after hello, world text to print it nicely in terminal.

int main() {
  stdout.printf("hello, world\n");

  return 0;
}

That's it! This is our hello, world program in Vala!

To run this program, we need to invoke Vala compiler or valac on this file using following command.

$ valac hello.vala

After successful compilation, you'll get hello (after the name of the file) as an executable. You can simply run this executable using following command.

$ ./hello
hello, world

In other words, you need to run two command to see the final output in Vala.

$ valac hello.vala
$ ./hello
hello, world

Or you can use && operator to run both commands in single command line.

$ valac hello.vala && ./hello
hello, world

In future, we'll going to accept the command-line arguments while running the program/application. To make that works, I'm updating the prototype of the main() method to access the command-line arguments.

int main(string[] args) {
  stdout.printf("hello, world\n");

  return 0;
}

You wouldn't see any difference while running this program again in terms of output. But, this modification is good to have.

$ valac hello.vala && ./hello
hello, world

Let's do some modifications in this hello.vala program while on the same time printing the hello, world text in the terminal. How about creating a new method with name greet() that prints the same hello, world text?

void greet() {
  stdout.printf("hello, world\n");
}

int main(string[] args) {
  stdout.printf("hello, world\n");

  return 0;
}

This greet() method returns nothing hence I mentioned void as return type of this method. We can call this method from main() method at the place of printf() statement.

void greet() {
  stdout.printf("hello, world\n");
}

int main(string[] args) {
  greet();

  return 0;
}

Even after doing these changes, we'll see the same output in the terminal as it was previously - hello, world text.

$ valac hello.vala && ./hello
hello, world

Let's even go further and print the text world from the name variable define inside the main() method as follows.

void greet(string name) {
  stdout.printf("hello %s\n", name);
}

int main(string[] args) {
  string name = "world";
  greet(name);

  return 0;
}

In main() method, we defined the name variable with world as the initial value. Then in next line, we passed this defined variable to greet() method as an argument. We also modifies the greet() method to accept one string parameter. Finally, we used %s placeholder to print the value of name at the place of text world.

Again, running this program prints the same output.

$ valac hello.vala && ./hello
hello, world

This is still hello, world program in Vala but with variable and supporting method.

Let's take a break and meet again to write the hello, world program but this time in object-oriented manner.

Labels: ,