All the C you need to know for GTK+

A short refresher on basic C concepts

Download the whole article as PDF

Write a full post in response to this!


If you want to develop applications with GTK+, a graphical toolkit used by the GNOME desktop environment, it is essential that you are comfortable with the C programming language. This article is meant to give you a short refresher on the basics of C that you will need to know when developing GTK+ applications.

Basic C program structure

Every C program is composed of one or more functions in the following format. The function receives a number of variable parameters, runs the commands in the function, and then returns a variable of the given type. Note that you can omit the return value by using void as your function type.

type function (parameters)
{
  local variables
  commands
}

Here is a practical example (don’t worry if you don’t understand it yet):

int my_function(int a)
{
  char b;

  b='r';
  printf("%d %c",a,c);
}

Another example function is shown below. The main() function is the only one that is required by every C program. It receives two parameters (argc and argv), which contain the command line parameters entered when the program begins and the number of parameters. The function should return an integer to exit. The following code will print out “x = 0 and y = 1”. (The printf() function will be covered later in this section!)

#include <stdio.h>

int main (int argc, char *argv[])
{
  int x, y;
  
  x = 0;
  printf ("x = %d ", x);
  
  y = 1;
  printf ("and y = %d", y);
  
  return 0;
}

Compiling your programs

If you are reading this article, you will probably need to compile your programs. Assuming that you call your file result.c, you can compile your programs by typing:

gcc -o result result.c

You will now be able to run the program by typing ./result in the directory where the program was compiled. It’s handy to have a console open with the program, and a second console with the command line above, ready to compile the code.

Variable types

C provides a number of variable types. You may notice that these are very basic types, all holding numbers, but they can be used to represent any piece of data. The following table gives an overview of the types that are available to you.

Data Type Bytes Lower Bound Upper Bound
char 1 -128 127
unsigned char 1 0 255
short (int) 2 -32768 32767
unsigned short (int) 2 0 65536
int 4 -2^31 2^31 - 1
unsigned int 4 0 2^32 - 1
long (int) 4 -2^31 2^31 - 1
unsigned long (int) 4 0 2^32 - 1
float 4 -3.2e38 3.2e38
double 8 -1.7e308 1.7e308

C Data Types

It’s important to think about where you place the declaration when creating variables. The scope of a variable refers to which parts of the application can access the variable. In general, variables use the following scope rules in C:

  1. You can only access a variable that has been declared before referencing it. In other words, the declaration of a variable must appear above its use.
  2. A variable cannot be accessed outside the set of brackets where it was declared. For example, if a variable was declared inside of a for loop, it cannot be accessed outside of that loop.

You can declare variables outside any function, such as above the main() function. This is called a global variable. Also, a variable declared within a function is local to that function.

Arrays

The variable types previously mentioned are useful, but what if you want a thousand integers? It would take quite some time to create a variable for each of these integers. To solve this problem, you could create an array of integers. The following program creates an array, and then fills it up with the numbers 1 to 1000. Notice that arrays are indexed beginning from zero.

int arrayOfInt[1000], i;

for (i = 0; i < 1000; i++)
  arrayOfInt[i] = i + 1;

It is also possible to create arrays with multiple dimensions. The following applications creates an array that has 1000 columns and 1000 rows, and fills it up with the sum of the indexes.

int multiArray[1000][1000], i, j;

for (i = 0; i < 1000; i++)
  for (j = 0; j < 1000; j++)
    arrayOfInt[i][j] = i + j;

The maximum number of dimensions is defined by the compiler, but most applications do not use more than three at the most. If you reach the maximum (GCC’s maximum is 29), you should consider rethinking your approach to the problem at hand!

Don't miss out on the other pages!
123next ›last »

Write a full post in response to this!

Similar articles

3

Do you like this post?
Vote for it!

Copyright information

Verbatim copying and distribution of this entire article is permitted in any medium without royalty provided this notice is preserved.

Biography

Andrew Krause: Andrew Krause is the author of Foundations of GTK+ Development (www.gtkbook.com). He is an active contributor to the Open Source community and is currently majoring in Computer Engineering at Penn State University. Andrew will be working for Argon ST as a software engineer beginning in May of 2008.

skypher's picture

C?

Submitted by skypher on Tue, 2008-02-19 15:49.

Vote!
0

Yuk, you don't get C by reading an article.

And you don't need to, either.

Be sensible and use a scripting language.

raseel's picture

I'm a bit disappointed to

Submitted by raseel on Wed, 2008-02-20 11:35.

Vote!
0

I'm a bit disappointed to see that all you have done is given a crash-course in C.
I was expecting some more info regarding GTK+ and how to get started.

Thanks,
Raseel
www.opensourcedeal.com

tracyde's picture

In your "practical example"

Submitted by tracyde on Mon, 2008-02-25 18:38.

Vote!
0

In your "practical example" above I believe you made an error.


int my_function(int a)
{
char b;

b='r';
printf("%d %c",a,c);
}

You will return an error because the variable 'c' has been neither declared or set.
It should read:


int my_function(int a)
{
char b;

b='r';
printf("%d %c",a,b);
}

Above I just changed the 'printf' statement to use 'b' instead of 'c'
A more noteworthy example might be:


int my_function(int a)
{
char b;

b='r';
printf("Your argument as a decimal %d and as a character %c",a,a);
printf("Your function variable b as a decimal %d and as a character %c",b,b);
}

That way new users can see how easy it is to display variables as different types and their associated values.

R/S

Derek

hotspoons's picture

@skypher - I took a couple

Submitted by hotspoons on Wed, 2008-05-07 02:31.

Vote!
0

@skypher - I took a couple of computer science classes in college (C, C++, and visual *barf* basic), but was too interested in partying to absorb and retain what I learned. Then several years later I found I have a knack for programming, and I made a career of it, learning 99% of what I know through various articles, mailing lists, API documentation, and digesting the source code of other projects. Most of my knowledge is in VHL or scripting languages (PHP, Python, Ruby, VB, C#, ColdFusion, Java), but as I've hit some limitations (specifically with Python) with what I can do, I have a renewed interest (as in the last week) in C as it can do anything, just with a lot more coding.

Where would your scripting languages come from without C anyhow? I don't think your favorite scripting language would be nearly as encompassing and could do 1/10th of what it does if it was written in assembly. Though it probably would be damn fast.

@raseel - Try programming with Python and GTK+ first as you will get a grasp on how to create, modify, and access GTK objects without needing to worry about a strongly typed language and lower level problems (like pointers, memory allocation, etc.) that you do when learning with C. I suggest looking over the Exaile source code as it is my favorite audio player for GTK, and it is written in pure, well coded python. The PyGTK API mirrors, almost verbatim, the GTK C API, so things make a lot more sense when you start coding GTK with C after learning the API in Python. That is if you already know Python. If not, I suggest you learn.

@Andrew Krause - Thanks for the article! I've been trying to get caught up on C as I haven't done much with it in almost 10 years, and last night I (re)learned pointers and pointer math, and tonight, your article answered a question I had been trying to figure out for the last couple of hours - what "->" is in C - I know in OO PHP, it is how to reference an object's property or method, and I figured it had something to do with struct's in C, but you were the only page out of about 40 or 50 I looked at that gave me an answer (google doesn't include special characters in searches, but I have seen that operator in a lot of GTK+ code - this article was on the 4th page of results when searching for "gtk c operators"). Thank you, and your article is a perfect crash course for someone who hasn't done C in 9 years.


From the FSM staff...

Odiogo