c
2 TopicsHands-on C: argc and argv from main() function explained
Quick Intro Learning C is is a good way to take our understanding of Operating Systems (OS) to the next level as it touches low-level structures that other languages such as Python often abstract. I'm covering C here not only because BIG-IP runs on top of Linux but also because understanding how the OS works can help us troubleshoot more complex issues. Linux has been gaining popularity and DevOps movement certainly gave it a boost as most DevOps tools run natively on Linux. As C is the nuts and bolts of Linux 'engine', it is also no surprise that we see more Platform (DevOps) Engineers interested in learning C to extend their reach when it comes to diagnosing problems and to have a more in-depth understanding of Linux. In this article, I'm going to use a simple Hello World in C but those who already had exposure to C will also benefit from understanding more thoroughly the default arguments (argc and argv) in main() function by running the code themselves. If you already know something about C, you can skip to last section and test the code yourself. Hello World This is a hello world in Python: This is a typical hello world in C: The main() function is the way we tell C where the execution of our code should start. It is the way C language signals processor where it should look for the main piece of code to run once our program is loaded in memory. The printf() function formats and print string characters to standard output. C doesn't have an interpreter like Python as it's a compiled language. Because of this, in order to execute above code we need to compile it first, i.e. create the executable file that has the binary code ready to run. How to Compile our Hello World We usegcccommand with-oparameter pointing to the name of the executable file name. Thegcccommand compiles our code and create the executablehelloworld. It's a binary file ready to be executed. If we need to change our code, we need to re-compile our code and create another executable file. Understanding #include <library_name> Adding libraries in C is similar to other programming languages. Libraries add capabilities to your code where you can find ready-to-use functions that do something that you might need to use. C libraries end in*.h(shortfor header) and compiler searches for them in/usr/includepath by default on Linux. In this particular case here, we only added thestdio.hlibrary which is the standard C I/O library with file reading/writing/streaming handling capabilities. There are also others we might occasionally bump into in most programs: unistd.h : The Unix standard library. It has utilities for interacting with the kernel such as system calls. stdlib.h : The C standard library with many useful utilities for general data type conversion, memory allocation, etc. ctype.h : Character library with for char conversion sys/types.h : System types library which contains the most definitions for base types and structures of Linux systems. string.h : String library for C string manipulation. Why does main function have argc and argv? argc= number of arguments passed when we execute program (integer) argv= list of arguments we passed when we executed program (array) Simple answer is that this is useful stuff to have natively. It's extensively used in command line tools, for example. Let's say we're creating a command line tool and we need to retrieve the 2nd argument passed to our program. You can just retrieve it directly from main() function by callingargv[2]. No need to create an extra function for that. Let's use this simple program as an example: It should print whatever arguments I pass to it: Notice that by default, the first argument is always the program's name. Even if I don't pass any arguments that's still the case: We can conclude that every program will have at least one argument which is the name of the program itself.1.4KViews1like2CommentsHands-on C: Understanding Pointers step by step
Quick Intro Pointers have always been the most confusing aspect of C for new learners. As a former lecturer, I find simple hands-on examples are the best to get one started. In this article, I will explain the following: What a pointer is How a pointer is declared in C How to assign a memory address to a pointer How to change values using pointers How to move pointer to a different memory address What is a pointer? It's literally a variable that stores and point to the memory address of another variable. Don't worry, it should be clear once we go through some examples. How a pointer is declared in C How to assign a memory address to a pointer Now, imagine there's a variablexlike this: The &x just means we're assigning topthe memory address ofx: In order to retrieve the value 5 fromp, we also need to use*pbecause p (without *) retrieves the memory address ofxand*pgoes after the value stored in the memory's address. Here's the proof: Notice that C understands that adding&in front of variable retrieves its memory address. How to retrieve pointer's value Retrieving pointer's is also known asdereferencingpointer. If we change*pwe also changex. We can also assign the value that*ppoints to a different variable too (effectively changing the value ofx): How to change values using pointers When we point a pointer to an array, it will point to the memory address of array's first item at index 0: The pointer behaves in the same way as if were making the changes to the array: How to move pointer to a different memory address In this example, I pointed p to A's memory address and then pointed p to n's memory address and that's fine: Let's uncomment last line so we can see that we can't do the same with arrays, i.e. re-assigning values: Hope that clarifies a bit of the mystery behind pointers.449Views0likes2Comments