Sunday, January 31, 2016

Relational operator

Relational operators are binary operators(operates on two operands) and are used to relate or compare two operands. There are four relational operators in C (i.e <, <=, >, >=). If the relationship between the operands is correct, it will return 1 and returns 0 otherwise.
Apart from four relational operators, C has two equality operator (== and !=) as well for comparing operands. Now let's take a look at different relational and equality operators and how they operate on the operands.
OperatorNameDescriptionExample
<'Less than' operatorChecks if the first operand is less than second operand and returns 1 if it's true, else returns 010 < 5 returns 0 and5 < 10 returns 1
<='Less than or equals to' operatorChecks if the first operand is less than or equals to second operand and returns 1 if it's true, else returns 010 <= 10 returns 1 and 10 <= 5 returns 0
>'Greater than' operatorChecks if the first operand is greater than second operand and returns 1 if it's true, else returns 010 > 5 returns 1 and5 > 10 returns 0
>='Greater than or equals to' operatorChecks if the first operand is greater than or equals to second operand and returns 1 if it's true, else returns 010 >= 10 returns 1 and 5 >= 10 returns 0
==Equality operatorChecks if the two operands are equal are returns 1 if it's true, else returns 010 == 10 returns 1 and 10 == 1 returns 0
!=Non-equality operatorChecks if the two operands are equal are returns 1 if they are not equal, else returns 010 != 10 returns 0 and 10 != 1 returns 1

Thursday, January 28, 2016

One Day National Conference on Emerging Trends in Science, Engineering and Technology, NCETSET16




College: Nehru Institute of Engineering and Technology
City: Coimbatore
State: Tamil Nadu

 To create a world class gathering of intuitive students and innovative researchers from academia and industry. The global competitiveness and unlimited innovations are emerging as the key aspects of the today’s engineering era.

The main objective of this conference is to address and provide platform to discuss the emerging trends there by ability to share the knowledge in the recent advancements.

The conference also provides a perfect platform for academicians to upgrade their knowledge in Science Engineering and Technology domain.

This conference is highly inter-disciplinary and encompasses all the key aspects of Energy and Environment and its intrinsic relationship to  Engineering and Management . Papers pertinent to the topic and are invited from all relevant disciplines.





Department: CSE, IT, MCA, ECE, EEE, Industrial




College Address: Nehru Institute of Engineering and Technology,
Nehru Gardens, T M Palayam,
Coimbatore – 641 105, Tamil Nadu, INDIA.
Phone: : 0422-2622008, Fax: 0422-2975131

Contact Details: 


Dr. A. Suresh,

Convenor,


 NCETSET-2016,

Department of Computer Science and Engineering,



Assignment Operator

Assignment operator assigns value of the expression on the right side to left side variable. The base assignment operator is '='. In C, you can use this operator like the following variable = expression
Here variable can be any kind of a variable and expression can be a simple constant, another variable or may be a more complex expression, like a formula. The value of the expression will be evaluated and assigned to the variable. But, there are some things to note about the assignment operator.


If the value of the expression on the right side is not same data type as variable on the left, then the value of the expression will be converted to same data type as the variable. Suppose, the expression is a floating point number and the variable is an integer. Then, the floating point number will be converted to integer and then will be assigned to the variable. It is better to use same data types on both sides unless it is a must, else it may lead to information loss.

C also allows multiple assignment statement like variable1 = variable2 = ... = variableN = expression.


This means, the value of the expression will be assigned to 'variableN', which in turn will be assigned to the variable left of 'variableN' and it propagates till 'variable1'. So, after the execution of multiple assignment operation, all the variables will have the same value.

Wednesday, January 27, 2016

Increment and Decrement operators ( ++, --)

There are two special arithmetic operators called increment and decrement operators available C. They are unary operators unlike all other arithmetic operators. The operators are denoted as : '++' (increment operator), '--' (decrement operator).
OperatorNameDescriptionExample
++Increment OperatorIncrements the value of operand by 1a++ : This is equivalent to a = a + 1
--Decrement OperatorDecrements the value of operand by 1a-- : This is equivalent to a = a - 1

Variants of Increment and Decrement Operators - Pre Increment, Post Increment, Pre Decrement and Post Decrement

There are two different variants of increment and decrement operators known as pre increment, post increment, pre decrement and post decrement operators.
  • Pre Increment Operators: When '++' is used as prefix of the operand, it's called pre increment operator. Pre increment operator will increment the value of operand before using it in the expression.
  • Post Increment Operators: When '++' is used as postfix of the operand, it's called post increment operator. Post increment operator will increment the value of operand after using it in the expression. ie; The current expression will use non-incremented value of operand.

Tuesday, January 26, 2016

Arithmetic Operators

Arithmetic operators are used to perform basic mathematical operations like addition, subtraction, multiplication and division. There are five arithmetic operators available in C (+, -, *, /, %). All arithmetic operators are binary operators, ie; they operate on two operands to produce the result.
The table below lists the arithmetic operators
OperatorNameDescriptionExample
+Addition Operator'+' Operator adds two values. This operator works on integer, float and character variables.20 + 10return 20
-Subtraction Operator'-' Operator produces a result after subtracting two values. It also works on integer, float, character variables.20 - 10returns 10
*Multiplication Operator'*' Operator produces result after multiplication of operands.20 * 10returns 200
/Division Operator'/' Operator produces result of division of first operand by second.20 / 10returns 2
&Modulus Operator'%' Operator generates the remainder after integer division.25 % 10returns 5

Monday, January 25, 2016

C Operators - Arithmetic, Assignment, Relational and Logical

Operator Basics

Operators in C language are symbols which tells the compiler to perform some mathematical calculations or logical operations (we will look at in a while) on the variables and constants. For example, consider the mathematical operation "10 + 2 - a". Here '+' and '-' are called operators and the values on which operators work ('10', '2', and 'a') are known as operands. C programming language has wide variety of operators which are categorised into different groups based on the type of operations they perform.

Operator Arity

Arity implies the number of operands the operator is defined to work on. Based on that we have operators of following articles
ArityDescription
Unary OperatorOperates on single operand.
Binary OperatorOperates on two operands, For example: '/' (used for division).
Ternary OperatorOperates on three operands. Example : conditional operator (we will discuss about it in this chapter).
n-ary OperatorOperators that operate on more than 3 operands.

Sunday, January 24, 2016

String Constants/Literals

String constants consist of any number of consecutive characters in enclosed quotation marks ("). String constants can have any valid character as part of it, including escape sequences. Example of string literal is given below
"Hello World!"
Some string constants can span multiple lines like the one given below
"First line \nSecond line \nThird line"
// Above statement when printed will look like as given below
First line 
Second line 
Third line
If you want to declare a string constant with quotation marks or backslash as part of the string, then you have to include proper escape character (\) for that. For example if you want a double quote (") as part of your string (like - Press the "Enter" button) you can do it as given below

Thursday, January 21, 2016

Character constants

Character constants hold a single character enclosed in single apostrophes. Characters are short integers. So, it can hold an integer value as well (maximum value character constants can hold is 127 and 255 for unsigned character constants). Also, different characters have associated integer values (like 'A' is 65, 'a' is 97 etc.). These values are standard and known as ASCII (American Standard Code for Information Interchange).
There are some characters which have special use in C language compared to other characters. They are preceeded by a backslash(\) (like \n, \b). These characters are called escape sequences. Below, you can find examples of escape sequences available in C.
CharactersEscape sequence
bell\a
backspace\b
horizontal tab\t
vertical tab\v
newline\n
form feed\f
carriage return\r
quotation mark\"
apostrophe\'
question mark\?
backslash\\
null\0
For example, using '\n' in printf statement will feed a line in the output. Character constants can hold escape sequences too. Examples of few of the character constants are given below.

Wednesday, January 20, 2016

Floating Point Constants/Literals

Floating point constants represents the floating point numbers, as the name suggests ie; numbers like 1.2, 3.6894 etc. In C programming language, we can represent a floating point constant in two ways,
  • Decimal point format or in
  • Exponent format.
In order to represent a floating point constant in decimal format, you need to include the decimal point (.) in the number. Floating point numbers can be expressed in exponent format like 1.23E-4 or 1.23e-4. It is another way to represent 1.23 x 10-4. The number after E can be signed, but should always be an integer. So, 3.56E1.2 is an invalid floating point constant. Some valid floating point constants in C are given below
3.968421
3968421E-6L
// Both of the above float literals represent the same value.

Tuesday, January 19, 2016

C Constants and Literals

Constants in C programming language, as the name suggests are the data that doesn't change. Constants are also known as literals. There are five types of constants or literals available in C programming language as shown below.
  • Integer Constants
  • Floating-point Constants
  • Character Constants
  • String Constants
  • Enumeration Constants

Integer Constants/Literals

Integer constants, as the name suggests, are integer values ie; numbers like 1, 20 etc. In C programming language, we can have integer constants of 3 different bases (or radix) namedDecimal (Base 10), Hexadecimal (Base 16) or Octal (base 8). During the declaration of an integer constant, a prefix is used along with the value to specify the base or radix.
  • For decimal literals, no prefix is used.
  • Prefix used for hexadecimal: 0x / 0X
  • Prefix used for octal: 0

Examples of different integer constants
123        /* decimal constant*/
0x9b       /* hexadecimal constant*/
0X9c       /* hexadecimal constant*/
0456       /* octal constant*/
In C programming, you can use upper case or lower case letters (ABCDEF or abcdef) as part of hexadecimal literals.

Monday, January 18, 2016

Types of Type Casting in C: Upcasting and Downcasting

There are two type of casting available in C language, known as upcasting and downcasting. Upcast means converting lower (like int) to higher (float, long int) data type. The reverse is called downcast. 
Upcast results in no information loss. But downcast results in information loss as lower data type have lesser bits and can hold the lesser amount of information/data. One data type considers higherif maximum value allowed to store in it is greater than the other data type. For example, float is lower compared to double because double can store more precisions

Second program in this chapter is example of upcasting(int value is converted to float) and the third program is example of down casting(float is converted to int)
C Type Casting with examples for Float to Int/Int to Char

Sunday, January 17, 2016

Rules for Implicit Type Casting

When different operands of an expression are of different type, complier performs implicit type conversion to match them. Below are the rules followed by compiler to perform the implicit conversion.
  • Integer Promotion: All types lower than int (char, short etc) are first promoted to int.
  • If the types of operands differ even after integer promotion, then following actions are taken
    • If one of the operand is long double, convert all others to long double
    • If one of the operand is double, convert all others to double
    • If one of the operand is float, convert all others to float
    • If one of the operand is float, convert all others to float
... This operation proceeds from the highest type to lowest.
Order of the data types from highest to lowest is given below
long double > double > float > unsigned long long > long long > unsigned long > long > unsigned int > int

Thursday, January 14, 2016

Implicit and Explicit Type Casting

Type casting can either be performed by the compiler automatically or user can specify them using the syntax described above. Former type is called implicit type casting and the latter is called explicit type casting.
What is implicit type casting/ implicit type conversion?
We've explained the explicit type conversion in the above paragraphs. Now lets's take a look at how implicit type conversion works. In implicit type conversion compiler takes care of the casting without requiring the programmer to specify them explicitly. For example in the program given below, compiler converts the sum of two float numbers to an int.
  1. ##include <stdio.h>
  2.  
  3. int main()
  4. {
  5. float value1 = 2.2;
  6. float value2 = 3.3;
  7. int result;
  8.  
  9. result = value1 + value2;
  10. printf("Result : %d", result);
  11. return 0;
  12. }
  13.  
  14. Output:
  15. Result : 5
Note: Though compiler performs implicit type casting, it's always recommended to specify the conversion explicitly because this improves the code readability and makes it easier to port to other platform.

Wednesday, January 13, 2016

C Type Casting with examples - float to int / int to char / int to long

The value of a variable or expression can be converted to another data type if desired. It is called type casting or type conversion. If you have a variable of data type long and you want to assign it to an int type variable, type casting can be used for that purpose. To type cast a variable or expression it must be preceded by the desired datatype as follows.
(data type) expression;

Why do we need type casting?

Type casting would be pretty handy when your inputs to an operation is of a particular type and you need more precision in the output. Let's see the use of type casting through a simple example
  1. #include <stdio.h>
  2.  
  3. int main()
  4. {
  5. int value1 = 10, value2 = 3;
  6. float result;
  7. result = value1 / value2;
  8. printf("Result without type casting: %f", result);
  9. return 0;
  10. }
  11.  
  12. Output:
  13. Result without type casting: 3.000000
In the above example we assign the result of division operation to float type variable result. The result is supposed to be 3.333333. But we get the result as 3.000000. Can you think why? Because the two operands of the expression, value1 and value2 are of int type. This causes compiler to treat the result also as integer; that means, it will ignore the decimal part of the result and store the integer result in float variable.

Tuesday, January 12, 2016

C Qualifiers - Constant and Volatile type Qualifier

C has some basic data types like 'int', 'float', 'char' etc. Each of which can hold a data of a specific type and has a set of specific properties defined for them. But what if you need to modify the properties of them? For example, if you want to make an 'int' variable constant, that means it's value shouldn't be changed from any where else in the program except the variable definition part. C type qualifiers can be used for such purposes.

'Const' Qualifier in C

Two type qualifiers available in C are 'const' and 'volatile'.
'Const' qualifier will impose a restriction on the variable, such a way that its value can't be changed or modified. The main use of 'const' is to define constants in your program; so that it's values can't be changed. Please take a look at the example given below to understand the use case of 'const' qualifier
  1. #include <stdio.h>
  2. // PI is defined as 'const' so that you may not change it's value accidentally.
  3. const float PI = 3.14;
  4.  
  5. double find_area(float radius) {
  6. return PI * radius * radius;
  7. }
  8.  
  9. int main() {
  10. printf("Area: %f", find_area(5.5));
  11. return 0;
  12. }
  13.  
  14. Output:
  15. Area: 94.985001

'Volatile' Type Qualifier in C

If a variable is declared as 'volatile', its value can be changed from outside the program. Declaring a variable with 'volatile' is actually a hint to the compiler to not perform any optimizations on the access restrictions of the variable. Let's take a look at it with the help of an example using 'volatile' keyword.

Monday, January 11, 2016

Variables

Void Type

Void type is defined using keyword 'void'. It's used to represent absence of data. For example, if a functions is declared with 'void' return type, it means that function doesn't return any values. But you cannot define a variable with 'void' type, program will not compile. You can declare void pointers though. 
Variables
In C programs, sometimes you may need to store some values so that you can later do some calculation or operation on the stored values. To hold these values we need variables. The variable can be considered as a type of identifiers that are used to represent some type of value or information in a designated portion of a program. A variable can be assigned different values in the course of its life span, i.e. the value stored in a variable may change. However, the type of data associated with a variable can never be changed.
Variable declaration
Declaration means associating a variable with some data type. Declaring variable will enforce that the variable can only represent values of already specified data types. A declaration has a specific format. Let’s consider following declaration as an example.
int number;
By this declaration, we are specifying that the number is a variable of data type integer. Until now, the variable number does not contain the value we want. The values can be assigned to the variable as follows

Sunday, January 10, 2016

Enumeration Types

Enumeration types are used to hold a value that belongs to a particular set. Keyword 'enum' is used to define enumeration types. Each element in the enumeration type is assigned with a consecutive integer constant starting with 0 by default.
Syntax for declaring enum is given below
enum type_name{value_1, value_2, value_3,...}
Each of the values in the value set (ie; value_1, value_2 etc) are integer constants. By default, they start with 0 and increments by 1. You can override this values your self. Let's take a look at it with the help of an example.
  1. #include <stdio.h>
  2. enum day_of_week{ sunday, monday, tuesday, wednesday, thursday, friday, saturday};
  3. int main(){
  4. enum day_of_week today;
  5. //Assigns enum value sunday to the variable today.
  6. today = sunday;
  7. printf("%d", today);
  8. return 0;
  9. }
  10.  
  11. Output:
  12. 0
In the above program enum type "day_of_week" consists of 7 values; Sunday to Saturday. Each of them is internally assigned with an integer constant starting with 0 by default. 'Sunday' gets 0, 'Monday' gets 1 and so on. Here the 'printf' statement will output the value 0, because we've assigned enum value 'Sunday' to the variable 'today' of type 'day_of_week'.