Thumbnail for Writing a simple Program in C by LiveOverflow

Writing a simple Program in C

LiveOverflow

11m 54s1,930 words~10 min read
YouTube auto captions
Transcript source

YouTube auto captions

This transcript was extracted from YouTube's auto-generated caption track. The transcript below is server-rendered so it can be read, searched, cited, and shared without opening the original YouTube player.

Timestamped outline
Pull quotes
[0:06]To be able to find security issues in software, one has to understand how software is written.
[0:06]It's very helpful to explore different programming languages to understand their differences and similarities and to get into the special way of thinking.
[0:06]Often, when I use a software, I imagine how a certain piece of it might have been implemented.
[0:06]This makes me think about the software architecture and potential difficulties that developers may have had, which helps me to find bugs that can be exploited.
Use this transcript
Related transcript hubs

[0:06]To be able to find security issues in software, one has to understand how software is written. It's very helpful to explore different programming languages to understand their differences and similarities and to get into the special way of thinking. Often, when I use a software, I imagine how a certain piece of it might have been implemented. This makes me think about the software architecture and potential difficulties that developers may have had, which helps me to find bugs that can be exploited. When I was younger, I used programming to write damage calculators for browser games, or simple chat programs that we could use at LAN parties to communicate. Nowadays, I use programming more as a tool to solve other problems or help me with repetitive tasks. To get a feeling what programming is like, we will have a look at a very simple program, implemented in a very old and low-level language C. And in the next video, we will use a more modern scripting language, Python. You will notice a lot of similarities in terms of structure, but also how different the syntax looks like. Let me first show you what our little program will do. To execute it, you simply have to enter the path. Because we are in the same folder, we have to start the path with the dot, which is an alias for the current location. Maybe you are wondering why the other programs, like LS, don't require a full path to execute them. The reason for that is, when you enter a command, or generally speaking, the name of a program, the shell will look at the configured path environment variable, which contains a list of directories where to look for programs. There are many different environment variables that all have their purpose. But because we are only interested in the path, we can use the pipe to redirect the output of ENV into another program called grep, which can filter for lines that only contain path. I use grep to filter output of other programs all the time. It's one of the most useful tools. Another way to get the content of path is to use echo to print the variable. Now let's use where is to find the location of the LS program, which shows that it is indeed in a location specified by path. The paths are separated by colons. If you would want to execute our program from every location, like LS, we can simply add this current directory to the path variable with export. A nice trick if you want to copy something in the terminal, it's to just mark it, and then press the mouse wheel. This will automatically paste the marked text at your current cursor location. Now add the user's home folder to the path, and then you can execute the program like any other.

[2:56]But let's dive in. C is over 40 years old, and despite its age, it's still used a lot. Many beginners feel that C is hard, because it feels unintuitive and too low-level at first. But you will notice in later episodes that it's very close to assembler, which is ideal to understand and imagine how computer works. Our goal is to obviously look nerdy, so we will use a command-line text editor called Vim. Jokes aside, it really makes sense to learn Vim, because we actually need it a lot, and sometimes you connect to a remote server and you have nothing else than just a text interface. VI is already installed, but Vim, which stands for VI improved, is a little bit nicer and is not yet there. But we get this nice little suggestion to install it with sudo apt get. The sudo prefix is needed because only the root user can install new software. Enter Y to continue.

[4:01]Now enter vim and the file name .c to start editing. Before you can type, you have to enter the insert mode by pressing I. Keep an eye on the status bar at the bottom, which tells you what you are doing. To exit the insert mode, hit escape. And then you can enter commands with a colon, such as colon w, to save the file, or colon Q to exit the editor. Or simply colon w q to save and exit. We don't really have to know more features than that for now. Except maybe we want to enable syntax highlighting and displaying the line numbers. So, syntax on and set number. First, we want to use the include statement to add the standard IO, the input output functionality to our program. Next, we define the entry function, which is always called main. Every C program has this function, and this is where the our program starts to execute. A function has a name and encloses code that belongs to this function. The parameter variables defined here are standardized. The first parameter is an integer number called arc C, which stands for argument count. And the second parameter is a list called arg V, which stands for argument vector. For now, we'll use array list and vector as synonyms. So those function parameters are, in fact, what we can pass to a program on the command line. Just think of the CAT command. It takes a file name as a first argument.

[5:43]And if you would program CAT yourself, you would access the file name with arc V. Now let's print something by using the print f function and a string as a parameter. The backslash N is a special character, which stands for new line. Maybe you remember it from the last video, where we had it in the hex dump and it shows up as hexadecimal A. Let's save it with escape colon w q and enter. Just a friendly reminder. Read the man page of print F to learn more about this function. To access the manual for the C function print F, you have to look in the third section of the manual, so man 3 print F.

[6:29]To create a program from this text, we have to compile it. The compiler converts the text into a binary machine code that can be executed by the CPU. We can use the GNU C compiler GCC for that. GCC file name of the code and minus O to specify the output file name. Oh, I made a mistake. There is a conflict for arc V. So let's quickly fix that by calling this variable probably arc C.

[7:01]Now you can execute it. Awesome. But let's be a good program from the beginning and use minus capital W all to enable all warnings. Now we get a warning that at the end of the function is no return value, despite having main defined to return an integer number. So let's fix that by adding a return zero at the end. Zero means that the program exited without an error. You can hit O instead of I to enter the insert mode in Vim, which automatically creates a new line below for you to write. Maybe you start to realize now how powerful Vim can get when you get familiar with the shortcuts. And now it compiles just fine. To make this program a bit more interesting, we will add an if statement. We say that the argument count has to be equal to two. And if that is the case, we will print the same phrase as before, just without a fixed name. We can use this format string syntax of percentage S to indicate that at this place belongs a string. And as first parameter, we specify the string, which is the second element of the arguments vector. Yes, it says one, but it's the second entry in the list because we always start counting from zero. If we don't have exactly two arguments, we will print an error message. Good practice is to print the error message not to the standard output, but to the standard error. It may seem weird for now, but you will understand it in the future. And instead of printing the second argument, because we might not have one, we will print the first argument instead. But why do we have a first argument? You will see in a second. Let's also add a return one here, because that indicates that the program exited with an error. Compile this code and test it. As you can see, if you don't specify an argument, it will display the usage error message with the program name. The shell passes everything you typed on the command line separated by spaces to the program as arguments. And this means that the first argument will be your program name. Now, if you set the second argument, the personalized message is printed. Also notice that when you want to have a string with spaces, you have to put it in quotes to indicate that it belongs together. With dollar question mark, you can display the exit code of the previously ran program, which is either zero or one, depending if we got an error or not. Now let's play a little bit with the program. We already scratched the topic of environment variables. If you look at them again, you can find the user variable, which contains your username. So when you execute the program with dollar user, it will print live overflow. But what if you actually want to print dollar user? To achieve that, we have to escape the dollar. The dollar has a special meaning on the command line. When you enter this text, the shell will read what you typed character by character, and once it reaches the dollar, it knows that the next characters will be the name of a variable. It will then look up the value of this variable and replace it. But if you use another special character, the backslash before the dollar, the shell will first read the backslash and knows that if a dollar is following, it will not treat it as a special character anymore. But wouldn't it be cool to always get greeted when you open the terminal? So browse to your home folder again and check out the dot files there. There is one file called bash RC, which is a special file that will be loaded once you open the terminal, and it can contain commands that are executed every time. Open it in Vim and you can go quickly to the bottom of the file with shift G. Then switch to insert mode with I or O. Now you can add the export command from earlier. But because we know about environment variables and how they are recognized and replaced, we can use this better technique to change the path. And on the next line, we add our program with the user variables as an argument. Now save it by pressing escape to enter the command mode, colon W. Now open another terminal and be excited. Obviously, there are easier solutions than this. But just using echo instead of a compile C program, it doesn't matter, we learned something. As an exercise, you should add an alternative version with echo and the environment variable to the bash or C. And you can also modify the vim RC to always enable the syntax highlighting and the line numbers for Vim. I remove the export command again because I don't want to have the home folder in the path.

Need another transcript?

Paste any YouTube URL to get a clean transcript in seconds.

Get a Transcript