C Programming for Beginners : Learning Fundamentals Made Easy

Suraj Das
7 min readOct 22, 2021

--

Photo by Glenn Carstens-Peters on Unsplash

Meaning of Functions in the World of Programming

Functions are those parts of code that accomplish a specific task.

For example, to print anything to the screen, we use the printf() function in C.

The Print Statement

Here is the print statement in C Programming.

Output :

Hello World! It's me Suraj.

Breaking Down the Code

With this line of code, the standard input/output related functions are included in the program.

Here we created a function called main() which is an integer type.

As of the definition of functions, this main function will do the tasks which are inside those curly braces.

The function printf() will print anything we type inside those brackets.

To print a sentence or anything, it must be inside the “ ”
Don’t forget to type \n at the end. It basically tells the computer to go to the next line after printing.
Oh! and don’t forget the semi-colon at the end of the line to make it a particular line of code.

At last, the return statement.
This return statement is basically telling the computer to return the number 0.

Why Return Statement ?

Well, we all do some errors right :)
It is important to know if the program contains an error. And for that, we use the return statement at the very end of our main function.

The main function contains the number 0 if no errors is made. So, when the return statement returns 0 and the main function contains 0 errors, it will compile the program successfully.
But when the return statement returns 0 and the main function contains 1 or more errors, it won’t compile the program and instead it will show the error.

Variables in C

Variables are names which can store data within it.
For example, we can store the number 5 in the variable x like this :

There is another way of doing this and that is by following these two steps :
Step 1 : Declaration
Step 2 : Assignment

It is important to specify the data type stored in the variable; which is int (integer type) in these case.

Data Types

There are mainly four types of data type.
These are : Basic, Derived, Enumeration, Void

In this lesson, we will be focusing only on the Basic and Derived data types.

Some of the Basic Data Types

  1. Integer
  2. Float
  3. Double
  4. Character

Some Derived Data Types

  1. Array
  2. Pointer
  3. Structure
  4. Union

Integer, Float, Double, Character Data Types

Array Data Type

Want to know how you can store a word in a variable ? :)
For that, you need to understand the concept of Array data type.

An array is a collection of similar data types.

We can store a collection of char data type, i.e., a string.
We can also store a collection of int data type, i.e., a list of numbers.
We can do much more with it.

Clap! Clap! You just learnt how to use the most used data types in C Programming.

Printing the Variable

Here is how you can print the variables :

Output :

Tatshat

Don’t be scared. I’m here to make you understand each and every line of code.

Breaking Down the Code

First, we stored the string “Tatshat” inside the character array named as firstName :

Then, the print statement :

The wrong way of printing :

This method of printing a variable it considered to be a bad practice even though it gives us the same output.
With this method, we may run into an unexpected behavior because here the the format in which we want it to be printed is not specified.

We’ve already mentioned the type of data when we created the variable firstName. So why specify the format again?

Let me answer this question with an example.

In the world of programming, ASCII codes are used much frequently.
Suppose, you don’t know the ASCII code of the letter A.
One thing you can do is to search for it on google.
But c’mon. You are a programmer. You can do something much interesting.

So you create a character variable and assign the letter ‘A’ with it.
And then print the variable but with the integer type format specifier.
(The integer type format specifier is %d)

Output :

65

There you go! now you know the ASCII code of any character you want.

The keyword used to specify the format is called a placeholder.

Placeholders

For each of the data type, there is a corresponding placeholder.

  • For integer type, the placeholder is %d (d is short for decimal)
  • For float type, the placeholder is %f (f is short for float)
  • For double type, the placeholder is %lf (lf is short for long float)
  • For character type, the placeholder is %c (c is short for character)
  • For string array type, the placeholder is %s (s is short for string)

Arithmetic Operations

Arithmetic Operators in C :

  • +
    Used as addition.

  • Used as subtraction.
  • *
    Used as multiplication.
  • /
    Used as division.
  • %
    Used as modulus. It helps us to find the reminder when dividing two numbers.
  • ++
    Used as increment.
  • — —
    Used as decrement.

Addition, Subtraction, Multiplication

Output :

Addition : 7
Subtraction : 3
Multiplication : 10

Division

Division process is a bit different.

Output :

Dividing numbers and using %d and int func can get you wrong output like : 2

The expected output is 2.5 but this prints 2. Why?

Because, the variable type was integer.

We should not declare the variable as int type. Also, we should change the placeholder.

Correct way of division :

Output :

2.500000

Why is the output 2.500000 and not 2.5 ?

Because float takes up a particular amount of memory.

You don’t have to learn about this now.
Just think of it as : by default, float shows six characters after the decimal point.

Want to print 2.5 ?

Good news! You can specify the number of characters after the decimal point.
Edit the placeholder with %.1f
This specifies that you want to display only one character after the decimal point.
After editing it, it should look like this :

And you got your desired output.

Play with the modulus operator on your own :)

Increment and Decrement

Output :

6
1

Hope you enjoyed this blog.

Hurray! You’ve now completed some of the basics with an ease.

Have any doubt ?
DM me on Instagram

Peace.

Index of C Programming lessons :

  1. Getting Started within 5 minutes
  2. Learning Fundamentals Made Easy
  3. Circumference and Area of Circle
  4. scanf() vs fgets()
  5. Hypotenuse Calculator
  6. Learn by Building a Calculator
  7. Functions
  8. More on Functions
  9. Length of an Array
  10. String Functions
  11. The While Loop

--

--