Friday, 26 February 2021

Variables & Data Types In C: C Tutorial #6



So guys in this tutorial, we are going to learn about variables and go over various data types. This tutorial is mostly going to be theoretical, and we will only touch the code for the purpose of understanding, except for that we will not be performing any coding related work, as theory is what makes your basis strong and a firm foundation can help you grasp the coding part more efficiently. 

As we have already discussed in the previous tutorial while going through identifiers that variables are nothing more than simple names given to a specific space in memory for reservation. I will get into more detail about it with the help of an example but first, let us cover some basics.

Declaration:

We cannot declare a variable without specifying its data type. The data type of a variable depends on what we want to store in the variable and how much space we want it to hold. The syntax for declaring a variable is simple:

data_type  variable_name;

or

data_type  variable_name = value;

the data type can be int, float, char, depending on what kind of value we want to store.

Naming a Variable:

A variable name can be of anything, we want to call out variable. Yet there are specific rules we must follow while naming a variable:

  • A variable name can contain alphabetsdigits, and underscore (-) only.
  • The starting letter can not be a digit.
  • White spaces cannot be used.
  • The name should not be reserved keyword or special character.

We can declare and assign value to a variable in two ways.

1st way:

int a = 12;

2nd way:

int a;

a= 12;

Both of these have exactly the same working.

 A variable as its names define can be altered, or its value can be changed, but the same is not true for its type. If a variable is of integer type, then it will only store an integer value, which means that we cannot assign a character type value to an integer variable. We can not even store a decimal value into an integer variable.

Let’s see this with an example:

Example 1:

#include <stdio.h>

 

int main()

{

    int a = 12.2221;

    printf("Output = %d" , a);

    return 0;

}

We are sending 12.2221 as a value in a, but since it is an integer type variable, the output will be only 12.

Output = 12

 

Example 2:

#include <stdio.h>

 

int main()

{

    float a = 12.2221;

    printf("Output = %f" , a);

    return 0;

}

Here we are using float as a data type. In this case, you can see the output below is 12.222100

Output = 12.222100

Note that we used %f instead of %d in the case of float.

The reason is that int can store only 2 bytes worth of data as its storage capacity is 2 bytes while float storage capacity is 4 bytes.

 

DATA TYPE

MEMORY (BYTES)

RANGE

 

Char

1

-128 to 127

 

signed char

1

-128 to 127

 

unsigned char

1

0 to 255

 

short int

2

-32,768 to 32,767

 

unsigned short int

2

0 to 65,535

 

unsigned int

4

0 to 65,535

 

Int

2

--32,768 to 32,767

 

long int

4

-2,147,483,648 to 2,147,483,647

 

unsigned long int

4

0 to 4,294,967,  295

 

Float

4

 

 

Double

8

 

 

long double

10



 

Code as described

#include <stdio.h>

 

int main()

{

  

    printf("%lu",sizeof(int));

    return 0;

}

 


Saturday, 23 January 2021

Basic Syntax Of a C Program: C Tutorial #5

 



In this tutorial we are going to understand the basic syntax of a C program. We will be using the same program we have been using for previous two programs, so you may develop a better understanding about the syntax before proceeding further. So, let’s get started with our tutorial.

A C program is made up of different tokens combined together. These tokens include:

  • Keywords
  • Identifiers
  • Constants
  • String Literal
  • Symbols
       int a;
       printf("Enter number a\n");
       scanf("%d", &a)
       return 0;

I have written a four-line code above so I can explain the tokens in a better way by using the references from the code above.

Keywords:

Keywords are reserved words that can not be used elsewhere in the program for naming a variable or function, instead they have a specific function or tasks and they are solely used for that. In the above given code, the return statement in the third line is a keyword.

 

Image: List of all the Keywords of C

Identifiers:

Identifiers are names given to variables or functions in order to differentiate them from one another. They are solely based on our choice but there are few rules that we have to follow while naming identifiers. According to the rules the name can not contain special symbols such as @, - , *, < , etc. In the above given code the “a” integer is an identifier.

Note: C is a case sensitive language so an identifier containing a capital letter and another one containing a small letter at the same place will be different. For example the three words: Code, code and cOde can be used as three different identifiers.

Constant:

Constant are very similar to variable and their values can be of any data type. The only difference between constant and variable is that a constant’s value never changes. We will see constants in more detail in the upcoming tutorials. In the above given code the “0” in the last line is a constant.

String literal:

String literal or string constant is a line of characters enclosed by double quotes. In the above given code “Enter number a” is a string literal. printf is being used there to print string literal onto the screen.

Symbol:

Symbols are special characters reserved to perform certain actions. They are used to notify the compiler so they can perform specific tasks on the given data. In the above example code & is being used as a symbol.

Let’s talk a little about white space. White space or blank space does not create any difference while using C. Unlike Python where we have to press enter to go to new line, in C we use semi-colon (;) to end a line of code. So until a semi colon arrives, the compiler will treat the code as a single liner so no matter how many lines we consume the code will run accurately if written correctly.

There are two code snippets given below. You can notice that they differ a lot regarding while space but their execution wills how the same output onto the screen i.e. “Hello World”.

 

Code1:

 

#include <stdio.h>

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

 

Code2:

 

#include <stdio.h>

int main()
{
    Printf
(
"Hello World\n"
)

;
    return 0;
}

C Programming Exercise 1 - Multiplication Tables: C Tutorial #8

This is the first exercise of C programming tutorials. This C programming series contains many exercises that will help you to become a gr...