Let's Get Started With C :Data-Types, Keywords, Variables, Literals, Operators etc.


Data-Types in C


Data Types are the keywords that represent the variable's value typa and storage capacity of variable in memory. 
If have variable A=5 (A is storing numeric type value) another example A=3.14 (A is storing decimal value). 


These are basic type of data types that we use in programs for storing variables in memory.
Let’s understand data types, storage capacity in memory and how to represent them in C.

Data types that can store both positive and negative values known as signed data types.
Data types that store only positive range values including 0 known as unsigned data types.

S.No
Data Type
Storage Size
Memory Range
Specifiers
1
int
2 or 4 bytes
-32,768 to 32,767 or
 -2,147,483,648 to 2,147,483,648 
%d
unsigned int
2 or 4 bytes
0 to 65,535 to 0 to 4,294,967,295
%u
short
2 bytes
-32,768 to 32,767
%d
unsigned short
2 bytes
0 to 65,535
%d
long
8 bytes
-9223372036954775808 to 9223372036954775808
%ld
Unsigned long
8 bytes
0 to 18446744073709551615
%lu
2
char
1 byte
-128 to 127 or 0 to 256
%c
unsigned char
1 byte
0 to 256
%c
signed char
1 byte
-128 to 127
%c
3
float
4 byte
1.2E-38 to 3.4E+38
%f
4
double
8 byte
2.3E-308 to 1.7E+308
%lf
5
long double
10 byte
3.4E-4932 to 1.1E+4932
%Lf

Note: In the above list I have not mentioned void datatype, as void is the datatype but it holds empty value or it means nothing, void used with functions as returning value.


In case of int data type it is flexible feature for compiler writers they can define its size as per system architecture.In next chapter we will learn to store collection of characters in variable known as string and string is represented by %s format specifier. 


Variables in C

Variable is the name of memory location where we are storing our data.Variable are also known as identifiers.There are some variable naming convention rules,

-        Variable should not more than 256 characters.

 Example: longvariablename
Variable name must start with character or _

Example: _variable, variable (valid)

1variable, $variable, 1_variable (invalid)      
Upper case are distinct from Lower case variables

Example: variable, Variable (both are different names)
     Spaces and special characters are not allowed, only _ is allowed.

Example: variable name, $variable, variable-name (all are invalid)

Variable_name (valid)
Any keyword name is not allowed as variable name.

Example: int, float, const, while (are keywords and not allowed)
When we define any variable we must declare what kind of value will be store in variable. Here data types come in picture.

Example:

int variable           - Storing numeric value 

char value.            - Storing 1 char value

float variable.        - Storing decimal value 

char value[10]         -Storing 10 characters
Note: value[10] – is the example of derived data type “array” example.

Types Of Variables :-


Variables can be of three types.


1. Local Variable 


2. Global Variable


3. System or Environmental Variable  


Local variable's scope is within it's function body, any outsider function can not access this variable. 


void main () {

 int a;

}
Here a is the variable which is local accessible for main method only.

Global variable's scope is global any function body can access this variable.

#include <stdio.h>


int b;

void main () {


 int a;

 //can access b


}

void user_defined_function () {

 // can not access a

 // can access b

}
Here b is the global variable and can be accessed by any function.

Environmental Variable are the system variables, these variables tells about "C"
environment information, these are global variables and we can access them anywhere in the program. 

C provides some in-build function to deal with them,

setenv() - It can set value of any system variable

getenv() - It will return value of any system variable 

putenv() - It will update the system variable's value



Note: All methods are available in "stdlib" header file.

#include <stdio.h>

#include  <stdlib.h>


void main() {


  printf("%s", getenv("DIR"));

  setenv ("DIR","/usr/bin",50);

  putenv("DIR=/usr/bin/test");


}
In this example you can see getenv method printing environmental variable's ("DIR")
value, setenv setting new value to DIR and putenv updating new value to DIR.

Constants in C


Constant is the fixed value of variable that cannot be changed during the execution of program. Constants also known as literals.

Constants can be of any type int, char, float etc. Const keyword is used to define any constant.

Example:

const int variable_name = 3


const float variable_name = 3.14


const char variable_name = ‘d’ 

(character is enclosed within the ‘ ‘ ).


const char variable_name[10] = “string”

(string is enclosed within  the “ ” ).

Backslash Character Constants / Escape Sequences –

There are some pre-defined constants are available in c with backslash (\).
Every constant has their meaning and purpose. Let’s explore them.


S.No
Backslash Character Const.
Purpose
1
\n
Gives new line
2
\r
Carriage return – move cursor at the beginning of line.
3
\b
Backspace <-
4
\f
Form feed - ^
5
\t
Gives horizontal tab
6
\”
Print double quotes “
7
\’
Print single quotes ‘
8
\\
Print backslash \
9
\v
Gives vertical tab
10
\a
Gives alert
11
\?
Print question mark
12
\N
Define octal constant
13
\XN
Define hexadecimal constant.
14
\0
Null character. It ends the string.

Operators in C

Operators are used to perform any operation on operands.If we want to perform addition on two variables then we have to use + operator ( a + b ).On the basis of different operations let’s discuss about different operators.

Arithmetic Operator -

Used to perform mathematical operations example: +, -, *, /, %, pre & post increment, pre & post decrements etc.

Example: a+b, a-b, a*b, a/b, a++, ++a, b++, ++b, a--, --a, b--, --b

Relational Operator –
Used to perform relations operations example: >, <, >=, <=, ==, !=

Example: a>b , a>=b, a
Logical Operators –
Used to perform logical operations between operands example: && (and), || (or), ! (not). These operators used with if condition.

Example: If ( a && b), if ( a || b), if ( !a)

Assignment Operators –
Used to assign value to another operand example =, +=, -=, *=

Example: a = b, a+=b, a-=b, a*=b

Bitwise Operators –These operators perform bit operation on binary decimals.
Example : & (bitwise and), | (bitwise or), ~ (bitwise not),^ (XOR) , 
>> (right shift),<< (left shift).
Ternary Operators –
These operators are used to provide one line decision making feature.Usually we use if - else block but with ternary operator we can check conditions in one line. 

Example: a = (x >2 ? 3 : 4)

Comments in C

This is the best practice of programming to provide comments while coding.
Comments tell information or explanation about the operation we are doing.
Comments are avoidable by compilers. 


//  It is for single line comment

/*  */  It is for multiple line comment  

/* write multiple line Comment here */


Keywords in C

Keywords are reserved words that meanings have been already explained to the compiler. 
We cannot use keywords as identifiers or literals. There are wide ranges of keywords some of them are given below.

Example: for, while, do, case, if, int, char, void, else, break etc.



Thanks for reading, please do share and comment your suggestions.

6 comments:

Powered by Blogger.