Home Blogs Books NewTab rsrc

Introduction to C Programming

programming c

C is a general-purpose programming language with features economy of expression, modern flow control and data structures, and a rich set of operators. C is not a "very high level" language, nor a "big" one, and is not specialized to any particular area of application. But its absence of restrictions and its generality make it more convenient and effective for many tasks than supposedly more powerful languages.

Why learn C?

C is a popular programming language that is known for its efficiency and versatility. It is used in a wide range of applications, including operating systems, embedded systems, and high-level programming languages like Perl, PHP, Python, and Ruby.

One of the main reasons to learn C is its widespread use in operating systems. Many popular operating systems, such as Unix and Linux, are written in C, and knowledge of C can give you a deeper understanding of how these systems work. C is also a good choice for low-level programming tasks, such as those that require direct interaction with hardware or need to be highly optimized for performance.

Another reason to learn C is its portability. C code can be compiled for a wide range of platforms, making it a good choice for applications that need to run on multiple devices. It is also a stable and mature language that is unlikely to disappear any time soon, which means that the skills you learn in C will remain useful for a long time.

Why C and not any other language?

The primary design of C is to produce portable code while maintaining performance and minimizing footprint (CPU time, memory usage, disk I/O, etc.). This is useful for operating systems, embedded systems or other programs where performance matters a lot (“high-level” interface would affect performance). With C it’s relatively easy to keep a mental picture of what a given line really does, because most of the things are written explicitly in the code. C has a big codebase for low level applications. It is the “native” language of UNIX, which makes it flexible and portable. It is a stable and mature language which is unlikely to disappear for a long time and has been ported to most, if not all, platforms.

Let's start with the traditional hello world program:

 #include <stdio.h>
int main()
{
printf("Hello, World\n");
}


* * *

OUTPUT

Hello, World
*** Exited normally ***

OK, so let's dive in and look at the first line in our program:

#include <stdio.h>

So, in order to understand what's happening here, you need to know that your machine comes with some already established utilities in order to relieve you from doing stuff from the bare basics. In this case we are invoking a standard library defined in the C language called the 'stdio' which is basically a utility for taking input from the user and flashing back the output. Having a library in hand always saves us from the burden of diving into the extreme low level code which we don't even have to go on and about tweaking.
Therefore we tell the compiler beforehand to include library code by using preprocessor directives. One of the very first tasks your compiler will perform is to search through your source code for preprocessor directives which modify your source code in some way. In our case, the #include preprocessor directive tells the compiler to copy source code from a library and insert it directly into the code where the preprocessor directive is found. Since our directive is at the very top of the file, the library code will be inserted at the top of the source file. (Note that this all happens in the computer's memory, so the original file on your disk never actually gets altered.)

int main()

Here we create a function named main that is the starting point for all C programs. All C programs require a function called "main" or they will not compile. Our function name is surrounded by two mysterious symbols, int and (void). The "int" bit tells the compiler what kind of value our function will return while the "(void)" bit tells our compiler what kind of values we will "pass" into our function. We'll skip over what exactly this means for now as these values will be covered in more detail later in the book. The most important thing to understand right now is that together, these symbols declare our function to the compiler and tell it that it exists.

So what is a function? In computer science, the term “function” is used a bit more loosely than in mathematics, since functions often express imperative ideas (as in the case of C) - that is, how-to process, instead of declarations. For now, suffice it to say, functions define a set of computer statements that work together to carry out a specific task. In C, the statements associated with a function are placed between a set of curly braces, { }, which mark the beginning and end of the statements. Together, the curly braces and the statements are called a block. Let's take a look at the first line in our function's block:

printf("Hello, World\n");

What will you need

No one ever became a musician just by reading sheet music. Musicians have to constantly play and practice on their instruments to get good. Similarly, the only way to become a programmer is to write and execute lots of code. To do that, you will need two different pieces of software: a compiler and a text editor. Both can be had for no cost.

Compilers

The compiler used in this course will be the GNU C Compiler (GCC).

Text editors and IDEs

Aside from a compiler, the only other software requirement is a text editor for writing and saving your C code. Note that a text editor is different from a word processor, a piece of software with many features for creating visually appealing documents. Unlike word processors, text editors are primarily designed to create plain text files. On Windows, the Notepad text editor can be used but it does not offer any advanced capabilities such as syntax highlighting and code completion. There are hundreds of text editors (see List of Text Editors). Among the most popular are Vim and Emacs as well as Notepad++, Atom, Sublime Text, gedit, which are also available on other operating systems (“cross-platform”). These text editors come with syntax highlighting and line numbers, which makes code easier to read at a glance, and to spot syntax errors. Many text editors have features for increasing your coding speed, such as keystroke macros and code snippets, that you can take advantage of as you gain skill as a programmer.

Name Website Platform License
Eclipse CDT Eclipse Windows, Mac OS X, GNU/Linux Free/Libre and Open Source
Netbeans Netbeans Cross-platform CDDL and GPL
GNOME Builder Builder GNU/Linux GPL
Anjuta Anjuta GNU/Linux GPL
Geany geany Cross-platform GPL
KDevelop KDevelop Cross-platform GPL
Little C Compiler (LCC) lcc Windows Open Source but not Libre
Pelles C Pelles C Windows, Pocket PC Proprietary, free of charge
Dev-C++ Dev C++ Windows GPL
CodeLite CodeLite Cross-platform GPL
Code::Blocks Code::Blocks Cross-platform GPL

The next program uses the formula C=(5/9)(F-32) to print the following table of Fahrenheit temperatures and their centigrade or Celsius equivalents:

 #include <stdio.h>

/* print Fahrenheit-Celsius table
for fahr = 0, 20, ..., 300 */


int main()
{
int f, c;
int lower, upper, step;
lower = 0; /* lower limit of temperature scale */
upper = 300; /* upper limit */
step = 20; /* step size */
f = lower;
while (f <= upper) {
c = 5 * (f - 32) / 9;
printf("%dt%dn", f, c);
f = f + step;
}
}



* * *

OUTPUT

0 -17
20 -6
40 4
60 15
80 26
100 37
120 48
140 60
160 71
180 82
200 93
220 104
240 115
260 126
280 137
300 148
*** Exited normally ***

Program to simulate different logic gates and to contruct a new gate FIN_GATE using the given truth table condition:

 #include <stdio.h>

int AND(int a, int b)
{
if(a == 1 && b ==1)
return 1;
else return 0;
}

int OR(int a, int b)
{
if(a == 1 || b ==1)
return 1;
else return 0;
}

int NOT(int a)
{
if(a == 1)
return 0;
else
return 1;
}

int NAND(int a, int b)
{
return(NOT(OR(a, b)));
}

int NOR(int a, int b)
{
return(NOT(AND(a, b)));
}

int FIN_GATE(int a, int b)
{
if(NOR(a, b) == NAND(a, b))
return 1;
return 0;
}

int main()
{
printf("%s\t%s\t%s\n%d\t%d\t%d\n%d\t%d\t%d\n%d\t%d\t%d\n%d\t%d\t%d", "A", "B", "Output", 0, 0, FIN_GATE(0,0), 0, 1, FIN_GATE(0,1), 1, 0, FIN_GATE(1,0), 1, 1, FIN_GATE(1,1));
}



* * *

OUTPUT

A B Output
0 0 1
0 1 0
1 0 0
1 1 1
*** Exited normally ***


* * *

Wifi hacking with Airmon-ng