Episode: 1330 Title: HPR1330: Programming languages 3 - C Source: https://hub.hackerpublicradio.org/ccdn.php?filename=/eps/hpr1330/hpr1330.mp3 Transcribed: 2025-10-17 23:39:00 --- Hi, my name is Garjo, and you are listening to a contribution to Hacker Bobby Radio. This is episode number three on my series on programming languages, and it is entitled Get in started with the C programming language. Of course, I'm not going to teach you C, but just wait your appetite. Okay, let's start by having a look at what C is useful for. C is mainly useful for systems programming, for number crunching, for graphics, and also for embedded systems. So, for example, if you are going to do some Arduino programming, you are most likely going to be programming in C or in a subset of C, which are the advantages of the C programming language. First of all, the speed, the fine grain control of the memory management. It is very close to the metal, and it's sometimes called the portable assembly language. And which are the drawbacks of C? First of all, you have to manage the memory. Your programming is very close to the metal, and it's like programming in assembly language. So, as you can see, advantages in drawbacks are very similar. Let's have a look at the history of language. The C programming language was developed by Ken Thompson and Dennis Ricci, between 1969 and 1973 at AT&T Bell Labs. The region of C is closely tied to the development of the unique operating system, originally implemented in assembly language on a PDP7 by Ricci and Thompson. During the late 1970s and 1980s, versions of C were implemented for a wide variety of mainframe computers, mini-computers, micro-computers, including the IBM PC and its popularity began to increase. In 1983, the American National Standards Institute, the NC, formed a committee to establish a standard specification of C. This committee based the C standard on the unique implementation. In 1989, the C standard was certified. This version of the language is often referred to as NCC standard C or sometimes C89. Following the Wikipedia page about the C programming language, we can have an overview of the uses of C. As I said in introduction, C is often used for system programming, including implementing operating systems and embedded system applications. Due to a combination of desirable characteristics such as code portability and efficiency, ability to access specific hardware addresses, ability to pun types, too much externally imposed data access requirements, and long run time demand on system resources. Some reasons for choosing C over interpreted languages are its speed, stability, and near universal availability. One consequence of C's wide availability and efficiency is that compalers, libraries, and interpreters of other programming languages are often implemented in C. The primary implementations of Python, C Python, Paral 5 and PHP are all written in C. Due to its thin layer of abstraction and low overhead, C allows efficient implementations of algorithms and data structures, which is useful for programs that perform a lot of computations. C is sometimes used as an intermediate language by implementations of other languages. This approach may be used for portability or convenience, by using C as an intermediate language, it is not necessary to develop machine-specific code generators. C has also been used to implement and use the applications, but much of that development has shifted to newer languages. So which are the characteristics of the language? C is imperative compiled, static, and weekly type. Imperative means that you tell a computer how to perform the computations. Compile means that you transform the source code in machine code using a compiler. Static means that types have to be chosen before compilation, and weekly type means that the type enforcement is not very strict. C allows structure programming. It uses functions, but is not functional, but rather procedural. That means that functions are not pure. It has control for mechanisms, like if else, for and while loops. It uses curly braces and semicolons for syntax. All parameters to functions are passed by value, that means copied. And reference are simulated using pointers. That means that the memory address of objects and structures are copied in the function codes. A C problem is just a function called main, which inside will call other functions. What do you need to start programming in C? So let's have a look at tooling and environment. Although you can use integrated development environments. In order to program in C, you just need an editor, write your source code, a compiler to translate this source code in machine code. One usually uses make files for convenience in order to avoid writing by hand the cost to the compiler. A debugger may also be useful, which will allow to track memory leaks and perform a step-by-step execution of a program. The GNU project provides every tool you need in order to run in C. Of course, you have Emacs as an editor, although you can use whichever editor you want, even VI. You have GCC, the GNU compiler collection, which contains a C compiler, a C++ compiler, and many others. And then you have GDB, the GNU debugger, which also has versions with graphical frontends like X, GDB, or DDD. So now that you have all the tools that is mainly GCC and editor, we can start by writing our Hello World program. So you can start your editor with an empty file that we will save to disk by calling it hello.c. As I said before, a C program is just a function called main, which will be executed. Our Hello World program will just print to the terminal Hello World. For that, we will need to use the C standard library, which provides a function to print to the terminal. So our program will start by including a header file, which contains the definition of the function which allows to print. So we will start by writing bound sign or the hash sign, followed by include the word include space. The less than symbol, then STDIO.H and greater than symbol. So what we have written, so hash include STDIO.H, will tell the compiler to fetch this file, the header file, STDIO, which contains the standard library functions for input and output. Hence it's a name STD for standard and IO for input output. So that the input output functions of the standard library will be available for our program. So once we have this, we'll start with writing the function. A function in C is written by the following same text. You start by writing the type of the value returned by the function, for example, inferning integer, float, for float, etc. Then the name of the function, and then between parentheses, the types of the arguments, which will be passed to the function. In C, there is a data type called void, which actually is the most general data type. So our program, our C program, will be a function called main, which will return an integer, which is the error code exit code of the program. So we'll start by writing int, int for integer, which is the type of the error code that we will return from this function. Then the name of the function is main, M-A-I-N, then open parentheses, void, close parentheses. So we have int, space, main, open parentheses, void, close parentheses. So this is the signature of our function, which takes a void parameter, which actually will be a program arguments. And at the end of the end, this function will return an integer. So in Unix, we have the convention of returning zero, if everything goes okay, and other values, if there are errors. So once we have the signature of the function, we can write the code, which implements the function. So we will open a curly brace, and then we will use the printf function in order to print the terminal our heavily world string. So we will write printf, open parentheses, open quotes, double quotes, hello world, backslash, N, close quotes, close parentheses, semicodes. So we have written printf, open parentheses, double quotes, hello world, backslash, double quotes, close parentheses, semicodes. So this line uses the printf function, which takes a string as an argument. The string is between quotes, hello quotes, and it's hello world. At the end of the string, we have a scape character, which is backslash, N, which is the query edge return, the new line character. And at the end of each line, C, Nits, semicodes. So this is the core of our program, which prints hello world followed by a new line to the terminal. And then since we are done, we are going to return the exit value, which will be zero. So the right return space zero semicodes. So this is the return value. And now we can close the curly braces. So our our program will be hash include space less than STDIO dot H greater than a new line. Int, space main, open parentheses void, close parentheses, your line, curly open curly braces, printf, open parentheses, double quotes, hello world, backslash, N, double quotes, close parentheses, semicodes, semicolon, new line return, a space zero semicolon, and close curly braces. So you're done. Now you can save this file to disk with the name hello dot C. And now you can combine very easy type gcc space dash O hello space hello dot C. What we have done here is calling the the gcc compiler telling me to produce an executable dash O for output called hello by using the code which is written in the hello dot C file. So gcc space dash O space hello space hello dot C. After that, if everything goes okay, you will have in the same directory where you have compile, you will have a file code hello. So you already had hello dot C, which is your C code search file, choose your C program, search code file. And then you have an executable, which is called hello. And now you can run it over slash hello and you will see hello world printed on the screen. So it is as easy as that. So if this works, that means that you have all the tools you need to start programming in C. Okay, so now that you see that writing C programs is not so difficult. Let's talk about the most horrible thing that C has, which is pointers. By the way, it's also the most amazing thing that C has, which allows you to manage memory in a very fine and controlled way. So what are pointers? C supports the use of pointers, which is a type of reference that records the address or the location of an object or function in memory. Pointers can be the reference to access data store, a address, 0.2 or to invoke a 0.2 function. Pointers can be manipulated using assignment or pointer or analytics. The runtime representation of a pointer values typically are all memory address. Pointers are used for many different purposes in C text strings are commonly manipulated using pointers into a race of character. Dynamic memory allocation is performed using pointers. Many data types such as trees are commonly implemented as dynamically allocated struct objects linked together using pointers. Pointers of functions are useful for passing functions as arguments to hire other functions such as Qsort or Bsert or things like this. An old pointer value explicitly points to a non-valid location. Referencing an old pointer values and define often resulting in a segmentation form. Old pointer values are useful for indicating special cases such as no next pointer in the final node of a linked list or as an error indication from functions returning pointers. Void pointers, that is void star, 0.2 objects of unspecified type and can therefore be used as generic data pointers. Careless use of pointers is potentially dangerous because they are typically unchecked. A pointer variable can be made to point to any arbitrary location which can call undesirable effects. Although proper use pointers point to safe places, they can be made to point to unsafe places when using valid pointer arithmetic. The objects they are point to may be deallocated and reused. They may be used without having been initialized or they may be directly assigned to unsafe values using a cast. In general, see spermissive in allowing manipulation of unconversion between pointer types, although comparatively provide options for various levels of checking. Some other programming languages address this problem by using more restrictive reference types. So now that you have the tools that is an editor and a compiler, you will see that using control flow mechanisms as conditionals with if, else or loops with for and while. It will be a very interesting program. But of course one of the interesting things of a programming language is its standard library. If you are working on a Unix like system, the system library will be installed on your machine. One way to get started to see what's available in the standard library is having a look at the documentation. On Unix like systems, the authoritative documentation of the actually implemented API is provided in form of man pages. On most systems, man pages on standard library functions are in section 3. For example, let's imagine that you want to compute a square root of a number, which is the function that you should use and which is its syntax. Of course, the direct use of main pages is not easy because you should have to know the name of the function in order to use man 3 and then the name of the function. The interesting utility for dealing with main pages is the apropo command. Apropo, which is spelled A-P-R-O-P-O-S apropo in French, will search the main pages in the name of the command that also on their description. If you type apropo space sqrt for square root, you will see 1, 2, 3, 4, 5, 6, 7 answers, at least. You will have csqrt, csqrtf, csqrtl, sqrtf, sqrtl. The ones beginning with c, you will see the description, which is complex square root, and the ones beginning with sqrt, their description is square root function. So now you can type man 3 sqrt, and you have the main page for sqrt, which gives you the name of the different related commands, sqrt, sqrtf, sqrtl, and the synopsis of the command. So it will start telling you which is the header file you have to include, so you will see how to include math.h. So this is the header file for the math library, and then you will see the three functions which can be used as sqrt, sqrtf, and sqrtl. Why three different functions? Because they can be applied to different types of data. For example, sqrt takes a double and returns a double, sqrtf takes a float and returns a float, and sqrtl takes a long double and returns a long double. So now you know the syntax, and then the main page also tells you that you have to link with the math library. I will not talk in this podcast about link, I will give you just some resources, but just bear in mind that when you are using other libraries then the standard c library you will need to call a linker in order to integrate the binary code of this library in your exit tip. Okay, so have a look another example. Let's say that you want to use a sort function. If you type apropos sort, you will have many, many answers. So what you can do is just type this through grep, so you will type apropos sort pipe grep. Backslash, open brand, free backslash, close brand. So we are grepping for the string, open brand, free close brand. Since parentheses are special characters, we have to skate them with the backslash. And now using grep like this, we have filtered to keep only section three man pages. And we have something like 20 answers among which we have you have qsort, radix sort, version sort, merge sort, etc. And now let's say that you want to do quicksort, which may be qsort, so you can do man free qsort, which tells you that you have to include stdlib.h and gives you the signature of the function, which takes certain number of parameters and returns avoid. So you see how you can investigate the man pages in order to find the functions that are available in the standard c library or other libraries installed on your system using the man pages. You are in, for example, scientific calculus, numerical integration derivatives, these kind of things, analysis algebra. There's the new scientific library, which is a c library, which is also called GSL, which can be very useful. If you want to build a program with a graphical user interface, you can use GTK, which is the Kim Toolkit, which was originally developed for the game, the image manipulation problem, but which is also the foundation of GNOME, of the GNOME desktop environment. There are many other libraries, you just have to search for them on the internet. If you really want to go further with learning c, free online available research, which is very interesting, is the book Learn in a GNU C by Siren for Ireland, which is available at http colon for slash forward slash www dot non GNU dot port slash c dash pro dash book. There you will find detailed documentation on the c programming language, on the GNU tools, which are available, and you will get everything that you need to get your starting and programming in C. Okay, I think that's going to do it for this episode. If you want to send me any feedback, you can get in touch with me by email at garjola.net. Thanks for listening and talk to you again soon. Bye bye. Thanks for listening and talk to you again. Bye.