Chapter 2: Unlocking the World of C

Understanding C 

Programming in C
Understanding C

C is a general-purpose programming language that can be used to create software like operating systems, databases, compilers, and more. C programming is a great language  to learn for beginners because it helps you to understand how computers work, how they store and process information, and how to write fast and efficient code.

In this blog post, I will show you:

  • The basics of C language in simple terms.
  • How and where to start from?
  • What you need to start coding in C?
  • The basic structure of a simple C program.
  • How to compile and run your code?
  • What happens next, with an example.

Basics of C Language in Simple Terms: 

C is a compiled language, which means that you need a special program called a compiler to turn your C code file (Human readable form) into a file (machine understanding form) that can run on your computer. A compiler is like a translator that takes your code as input and produces a file as output.

C is also a low-level language, which means that it gives you direct access to the resources of your computer, such as memory and CPU, serial port etc. This allows you to write code that can control bits and bytes directly, and make your code faster.

C is also a structured language, which means that it follows a set of rules and conventions for organizing your code into parts, such as functionsvariablesstatementsexpressions, etc. This makes your code easier to read, fix, and maintain.

Some of the main features of C language are:

  • It supports data types such as int (whole number), float (decimal number), char (letter or symbol), etc.
  • It supports operators such as + (add), - (subtract), * (multiply), / (divide), % (remainder), etc.
  • It supports control flow statements such as if…else (choose one option), for (repeat something), while (repeat something until a condition is met), switch…case (choose one option from many), etc.
  • It supports functions, which are parts of code that do a specific task and can be reused again in your program.
  • It supports arrays, which are collections of data of the same type stored next to each other in memory.
  • It supports pointers, which are variables that store the location of another variable or memory space.
  • It supports structures, which are custom data types that group together related variables of different types.

How and Where to Start From?

To start learning C programming, you need two things: text editor and a compiler.

text editor is a program that lets you write and edit your code. You can use any text editor you like, such as Notepad++Sublime TextVisual Studio Code, etc.

compiler is a program that converts your code into a file that can run on your computer. You can use any compiler you like, such as GCC (GNU Compiler Collection), Clang (LLVM Compiler Infrastructure), Visual Studio (Microsoft Compiler), etc.

You can also use an online IDE (Integrated Development Environment) that provides both a text editor and a compiler in one place. Some examples of online IDEs are Repl.itJDoodleOnlineGDB, etc.

What You Need to Start Coding in C?:

To start coding in C programming, you mainly need three things:

  1. A file name : A file name is the name of your code file that ends with .c extension. For example, hello.c.
  2. A main function: main function is the starting point of your program where the execution begins. The int word before main means that the main function returns a whole number value. The brackets after main mean that the main function takes no arguments.
  3. A return statement: A return statement is the last statement in your main function  that returns a whole number value to the operating system.

Structure of a Simple C Program:

Here is an example of a simple C program that prints “Hello World!” on the screen:

#include <stdio.h>

int main()
{
printf("Hello World\n");
return 0;
}

Let’s explain this code line by line:

  • Line 1 and 2 are comment lines that start with //. Comments are used to explain your code and are ignored by the compiler.
  • Line 4 is a preprocessor directive that starts with #. Preprocessor directives are instructions to the compiler that are done before the actual compilation starts. The #include directive tells the compiler to include the contents of another file in your code. In this case, we include the stdio.h file, which has the definition of the printf function and other input/output functions.
  • Line 6 is the main function where the program execution begins. The int word before main means that the main function returns a whole number value. The brackets after main mean that the main function takes no arguments.
  • Line 8 is a function call that prints a text followed by a new line. The printf function is defined in the stdio.h file and takes a text as an argument. The \n character is a special symbol that means a new line.
  • Line 10 is a return statement that returns 0 to the operating system. The 0 value means that the program ended successfully. You can also return other values to mean different error codes.

How to Compile and Run Your Code:

When you are done writing your code, you need to save your file with a .c extension, such as hello.c. Then, you need to compile your code using a compiler, such as GCC, Clang, Visual Studio, etc.

The compilation steps: 

C Compilation steps
C Compilations steps

  1. Source Code (hello.c): This is the original C code written by the programmer.
  2. Preprocessor: The preprocessor performs various tasks, such as handling preprocessor directives (# directives) like macro expansions, file inclusions (using #include), and conditional compilations (using #ifdef, #ifndef, etc.). It generates Preprocessed Code (hello.i) by processing the source code and expanding all the macros and including any header files.
  3. Compiler: The compiler takes the Preprocessed Code (hello.i) and translates it into low-level assembly code specific to the target architecture. During this step, the C code is translated into machine-independent intermediate code, which represents the program's logic.
  4. Assembler: The assembler takes the Assembly Code (hello.s) generated by the compiler and converts it into machine code (binary code) specific to the target processor. The assembler resolves symbolic references and generates the final Object Code (hello.o).
  5. Linker: The linker combines multiple object files (if any) along with any necessary system libraries to create the final Executable Code (hello) that can be executed by the operating system.

Compile What Happens Next?

After compiling your code, you can run your executable file using your terminal or command prompt. To run your executable file using GCC, you can use this command in your terminal or command prompt:

./hello

This command tells GCC to run hello executable file.

When you run your executable file, you will see the output of your program on your screen. For example, if you run hello executable file, you will see:

Hello World!

This is the output of your program that prints “Hello World!” on the screen.

Congratulations! You have just written and ran your first C program!

Conclusion:

In this blog post, I have shown you:

  • The basics of C language in simple terms
  • How and where to start from
  • What you need to start coding in C
  • The basic structure of a simple C program, with comments in each line
  • How to compile and run your code
  • What happens next, with an example

I hope this blog post has helped you to start learning C programming and made you interested in this powerful and versatile language. If you want to learn more about C programming, I suggest checking out some of these resources:

  • "Learn C Programming: A complete online tutorial that covers C programming from beginner to advanced level." Link: https://www.learn-c.org/

If you have any questions or feedback about this blog post, please feel free to leave a comment below. I would love to hear from you and help you with your learning journey.

Happy coding! 😊

Thank you for reading this blog post! To explore more chapters, click the button below:

Master C Programming Index

Comments

Post a Comment

Popular posts from this blog

Chapter: 1 Programming..? What is it.?

Chapter 5: Tackling Typecasting, Decision Control in C