Sunday, February 3, 2019

Basics of Programming in C (part-2)

Hello Everyone!

Welcome to my 3rd post.First of all, I should thank my parents, sister and my friends for supporting me and encouraging me.

In the previous post I covered topics like installation of compiler, Writing our first C program etc.
Basics of Programming in C (part-1):
https://codewithsujithsagar.blogspot.com/2019/02/basics-of-programming-in-c-part-1.html

In this post I am going to explain about constants, variables and data-types with some simple examples. This post will take only 5 minutes to completely read and understand it.

Understanding constants, variables and data-types

By the name itself we can understand that constants are those whose value doesn’t change and variable are those whose value can be changed.

The Data-type of a value or variable is an attribute that tells what kind of data that value or variable can have. Some of the data-types are 
int (for integer values like -56, 0, 89 etc)
float (for floating values like 2.36, 5.298 etc)
char (for characters like ‘A’, ’b’, ’%’ , ‘4’, compiler will treat ‘4’ as a character as it is enclosed within apostrophe. So 4 is an integer and ‘4’ is a character).

A variable having integer data-type indicates that we can only store integer values in that variable and a constant is said to be having integer data-type if it is an integer.

Similarly, a variable having float data-type indicates that we can store floating values in that variable and a constant is said to have float data-type if it is a floating number.

We will store constants in variables by assigning them to variables. Here we need to remember that we can only assign that constant to variable where both of their data-types are same.

However, there are some exceptions. We can store an integer value in a float variable (that integer variable is automatically converted to float, for example ‘2’ is converted to ‘2.0’).
Similarly, we can store float values in integer variables but the floating number are rounded off to integer values while assigning them (for example, ‘3.67’ is converted to ‘3’).

Memory

Each variable occupies some amount of memory in the computer. The amount of memory that is occupied by a variable is depended upon the data-type of that variable. Each memory location will have some address. That memory is allocated to the variable when we initialize the variable. In background, the required amount of memory that will be occupied by the variable is named as that variable_name and each memory location have some address.

In order to assign any value to a variable, we should use = (assignment operator).
Variable should be on the left side of the = operator and the value which we want to store should be on the right side of the = operator. We can also assign the value of one variable to other. We can also perform arithmetic operation on variables and constants while assigning them to a variable.
Example:
int x = 3;
int y = 2;

For example, we can consider a bag which can hold at most one book at a time as a variable and it’s data-type is book (so, that bag can only hold books) and books of different subjects each as constants. Here that school bag may contain a book related to math or science or social or any other subject but at an instance of time it will contain anyone of them. So here we can change books in that bag. But if we observe, we cannot transform math textbook into social textbook, we can only replace them by removing one from bag and by adding the other. We can say that the data-type of the school bag is book. That means we cannot keep a cricket bat in that school bag. Don’t think about the size of bat and bag, we can’t even keep a pencil in that bag instead of book because the data-type of the bag is book.

Syntax for variable initialization and declaration:

Data-type variable_name;
Data-type variable_name = constant;
Data-type variable_name = another_variable + constant * (another_variable);

Example:
int x = 3;
int y = 2;
int z = x*(y+3);

To display the value stored in a variable we should know about format specifiers which tells to compiler about which type of variable you are trying to display.

int -> %d
float -> %f
char -> %c

syntax:
printf(“format_specifier”,variable_name);

usage:
printf(“The value stored in the variable X is %d”,X);

Now we know how to display the text and the values stored in a variable using printf statement. We can store the values in variables by using scanf statement.

Syntax:
scanf(“format_specifier”,&variable_name);

example:
scanf(“%d”,&x);

Here x is variable of int data-type and we need to declare x as an int variable above the scanf statement. Otherwise, compiler will throw an error because we already saw that compiler starts compiling from first line to last line of program sequentially. It is our job to tell compiler that we are going to use some type variable before using that variable.

If you notice, in the syntax of scanf statement we used symbol ‘&’ which tells the compiler to store this constant in that memory location(at the address of variable x).

Note: while printing a value of a variable we don’t use & symbol as a prefix for variable_name whereas while using scanf statement to store them we should use & symbol as a prefix for variable_name.

Try executing the following statement,

int x=2;
printf(“%u is the address of variable x”,&x);

'%u' is for unsigned integer.
for more information:
https://www.w3schools.in/c-tutorial/format-specifiers/

That’s all for this post. In the next post we’ll discuss about Conditional statements.
https://codewithsujithsagar.blogspot.com/2019/02/basics-of-programming-in-c-part-3.html

Share it with your co-geeks and help me to improve by giving feedback in the comments section below.

Thank you.

Thanks to Binshumesh Sachan and Shaik Saqlain Musthak for helping me in editing.

Thanks alot to Nikhitha, Bhavani, Sadhana, Kailash varma, Devendhar, Manoj, Rakesh, Karthik, Koushik, Soma Sekhar and all other who are sharing these posts with their Co-geeks. Once again thank you guys. 

 ABOUT ME




MYSELF SUJITH SAGAR. I AM A B.tech SECOND YEAR  COMPUTER SCIENCE AND ENGINEERING STUDENT AT NATIONAL INSTITUTE OF TECHNOLOGY DELHI.

Linkedin profile: www.linkedin.com/in/sujith-sagar-gandi-2b3544179

7 comments:

How Can You Test and Revert a Failover in Azure Cache for Redis Premium with Geo-Replication?

If you're using Azure Cache for Redis Premium with geo-replication across different regions, knowing how to test failover and revert it ...