Step into the World of C Programming

Welcome to the exciting world of C programming! If you are a beginner interested in learning how to code and dive into the programming world, you have come to the right place. C is a powerful and widely used programming language that forms the basis of many other languages. In this blog, we will take you through the basics of C programming step by step in a simple and easy-to-understand way. Let this journey of coding adventure begin!

Why do you need to learn programming in 2023?

In 2023, learning programming will be crucial due to the increasing integration of technology into our lives. It offers a lot of career opportunities in software development, AI, data analytics, and cyber security. Programming improves problem-solving skills, logical reasoning, and creativity, making it valuable outside of the tech industry. Understanding programming empowers individuals to be tech-literate, adapt to technological changes, and innovate in various fields. It enables entrepreneurship, collaboration, and individual project pursuit. In the age of automation and AI, programming skills are a valuable asset to staying competitive in the job market and understanding the digital world we live in.

Why should you learn C programming?

Learning C programming is highly beneficial as it builds the foundation of many other programming languages, enhancing your understanding of programming concepts. C's efficiency and portability make it ideal for systems programming and embedded system development. Mastering C allows you to work with lower-level hardware, giving you more control over memory management and system resources. Moreover, the prevalence of C in various industries ensures a lot of job opportunities, making it a valuable skill for both new and experienced programmers alike.

Chapters at a Glance:

  1. What is C programming?
  2. A brief history of C programming language
  3. Setting up the Development Environment
  4. The basic structure of C program
  5. Variables and Data Types
  6. Basic Input and Output
  7. Decision-making with conditional statement
  8. Looping with loops


1. What is C programming?

C programming is a way of giving instructions to a computer in a language that it understands. It allows us to create software and applications, such as games, websites, and tools. With the help of C, we can solve problems, perform calculations and make the computer perform specific tasks. It's like talking to a computer and telling it step by step what to do. C is a primary language that forms the basis of many other programming languages and is widely used in various fields of technology.


2. A brief history of C programming language:




C programming was developed by Dennis Ritchie at Bell Labs in the early 1970s as an evolution of the B programming language. It is designed to rewrite parts of the Unix operating system, providing a powerful and efficient language for system programming and low-level hardware access. The publication of "The C Programming Language" by Kernighan and Ritchie in 1978 popularized C among programmers. In 1989, ANSI standardized C (C89), followed by ISO in 1990 consolidating its syntax and semantics. C's portability across platforms contributed to its widespread adoption, which became the basis for many other languages. With subsequent revisions such as C99 and C11, C continues to be an essential and influential language in the programming world, employed in various software systems, applications, and embedded systems.


3. Setting up the Development Environment:

Before we start writing our first C program, we need to set up the development environment. For beginners, we recommend using a simple text editor like Notepad++ or Visual Studio Code, paired with a C compiler like GCC (GNU Compiler Collection). GCC is freely available and supports multiple platforms, making it a great choice for beginners.


4. The Basic Structure of a C Program:

Let's take a look at the basic structure of a C program. All C programs consist of functions, and the primary function is called "main." Here's a simple C program to get you started:




In this example, we include the 'stdio.h' header file to use the 'printf' function for output. The '#' is called the preprocessor directive. The main function is where the execution of our program begins. The 'printf' function "Hello, world!" display the text on the screen, and return 0; Indicating a successful program execution.


5. Variables and Data Types:

Variables are used to store data in a C program. Before using a variable, we need to declare its data type. C supports various data types like 'int' for integers, 'float' for floating-point numbers, 'char' for single characters, and more. Here's an example of declaring and using variables:


In this code, first, we initialize three variables age, height, and grade which contain integer, floating-point, and characters respectively. Then we store the value 25 to age, 5.8 to height, and character 'A' to grade. Finally, we print the three variables, in the 'printf' statement, '%d', '%f', and '%c' are called format specifiers.

6. Basic Input and Output:

Input and output are essential for interacting with the user. We can use the 'scan' function for input and the 'printf' function for output. Let's modify our previous program to take input from the user:


In this code, we used 'scanf' statement to scan the value entered by the user. As the user entered an integer value, '%d' format specifier is used, and '&' is attached to the term 'age', this '&' is called the 'Address of Operator'.

7. Decision-making with conditional statement:

Conditional statements allow us to make decisions in our programs. The most common ones are 'if', 'else if', and 'else'. These statements help execute specific code blocks based on certain conditions. Let's see an example:



The motive of this program is to take an integer as input from user and checks whether the number is positive, negative, or zero. To solve this, we take input by using the 'scanf' statement. Then we check if the number is greater than 0, if so, we print that the number is positive. If the number is not greater than 0 then move to the next statement where we check if the number is less than 0, if so, we print that the number is negative. In the end, if the previous statements are not correct, we simply print that the number is zero.

8. Looping with loops:

Loops allow us to repeat a block of code until a certain condition is met. C supports 'for', 'while', and 'do-while' loops. Let's look at a simple example using the for loop:



The structure of for loop follows a pattern, where the first part is called initialization, which means we assigned some value to the variable to be used in the loop. The second part is called the condition part, where some condition is written, if the statement meets a certain condition, the loop will continue until it does not meet that condition again. The third part is called iteration or increment/decrement, where the value of the variable is either decreased or increased.

In the following code, we assigned 1 to i, the loop will continue until the i is greater than or equal to 5, and the i will be incremented by 1 until it will be 5. Every time the loop statement executes and it prints the "Hello, World!" along with the value of i. The '\n' (pronounced as 'backslash n') in the 'printf' statement simply says that the cursor will move to the next line.

The output of the code:

Hello, World! 1
Hello, World! 2
Hello, World! 3
Hello, World! 4
Hello, World! 5

Example of while loop:



In the while loop, we assign the value '1' to the 'count' variable outside of the loop statement. And the condition is set in the parenthesis after the 'while' keyword. Also, the iteration is set into the statement segment.

The output of the code:

Count: 1
Count: 2
Count: 3
Count: 4
Count: 5

Example of do-while loop:



There is a little difference between a while and a do-while loop, that is if there is no condition satisfied, still the statement in the do-while loop will be executed at least once. For this reason, a do-while loop is known as an 'exit-controlled loop' because in this loop only the exit point is controlled by the condition part. In other words, while and for loops are known as 'entry-controlled loop' because the test condition is checked at the beginning of the loop statement.

The output of the code:

1 2 3 4 5 6 7 8 9 10


Conclusion:

Congratulations! You have taken your first step into the world of C programming. We covered the basics of C programming, from setting up the development environment to writing simple programs using variables, input/output, conditional statements, and loops. Keep practicing, explore more concepts, and you'll soon be ready to tackle more complex challenges in the fascinating realm of programming.

Dedicated articles on each topic will be provided very soon.

Happy coding!