1_Variable,Datatype&Operator
1_Variable,Datatype&Operator
Lecturer
Structured language
• Operators: +,−,∗,/,%
x=y∗2+z∗3;
• Function: int factorial (int n); /∗function takes int, returns int∗/
C Variables and datatypes
Identifiers
•A C identifier is a name used to identify a variable. An identifier starts with a
letter A to Z or a to z or an underscore _ followed by zero or more letters, un-
derscores, and digits (0 to 9).
•The datatype of an object in memory determines the set of values it can have
and what operations that can be performed on it.
4 Derived types:
They include (a) Pointer types, (b) Array types, (c) Structure
types, (d) Union types and (e) Function types.
Integer Types
Standard integer types with its storage sizes and value ranges:
#include <stdio.h>
#include <limits.h>
int main() {
printf("Storage size for int : %d \n", sizeof(int));
return 0;
}
Floating-Point Types
Standard integer types with its storage sizes and value ranges:
return 0;
}
Output
Storage size for float : 4
Minimum float positive value: 1.175494E-38
Maximum float positive value: 3.402823E+38
Precision value: 6
C Variables
variable is nothing but a name given to a storage area that our programs can
manipulate. Basic variable types:
Type Description
type variable_list;
Example:
int i, j, k;
char c, ch;
float f, salary;
double d;
Variable initialization:
type variable_name = value;
Example:
extern int d = 3, f = 5;
int d = 3, f = 5;
char x = 'x';
Key Words
The following list shows the reserved words in C.
These reserved words may not be used as constant
or variable or any other identifier names.
\a Alert or bell
\b Backspace
\f Form feed
\n Newline
\r Carriage Return
\t Horizontal tab
\v Vertical tab
\ooo Octal number of one to three digits
\xhh Hexadecimal number of one or more digits
Defining Constant
• Using #define preprocessor.
#define LENGTH 10
#define WIDTH 5
int main() {
int area;
printf("%c", NEWLINE);
return 0;
}
Defining Constant
#include <stdio.h>
int main() {
int area;
printf("%c", NEWLINE);
return 0;
}
C Operators
• Operator performs mathematical or logical operations on the variables
Arithmetic Operators
Relational Operators
Logical Operators
Bitwise Operators
Assignment Operators
Misc Operators
Arithmetic Operators
int A=10;
int B=20;
> Checks if the value of left operand is greater than the (A>B) is not
value of right operand, if yes then condition becomes true
true.
< Checks if the value of left operand is less than the value (A<B) is true
of right operand, if yes then condition becomes true.
>= Checks if the value of left operand is greater than or (A>=B) is not
equal to the value of right operand, if yes then condition true
becomes true.
<= Checks if the value of left operand is less than or equal to (A<=B) is true
the value of right operand, if yes then condition becomes
true.
Relational Operators
#include <stdio.h>
void main() {
int a = 21;
int b = 10;
int c ;
if( a == b )
printf("Line 1 - a is equal to b\n" );
else
printf("Line 1 - a is not equal to b\n" );
if ( a < b )
printf("Line 2 - a is less than b\n" );
else
printf("Line 2 - a is not less than b\n" );
if ( a > b )
printf("Line 3 - a is greater than b\n" );
else
printf("Line 3 - a is not greater than b\n" );
/* Lets change value of a and b */
a = 5; b = 20;
if ( a <= b )
printf("Line 4 - a is either less than or equal to b\n" );
if ( b >= a )
printf("Line 5 - b is either greater than or equal to b\n" );
}
Logical Operators
int A=1;
int B=0;
^ Binary XOR Operator copies the bit if it is set in one operand but
not both.
~ Binary Ones Complement Operator is unary and has the effect
of 'flipping' bits.
<< Binary Left Shift Operator. The left operands value is moved left
by the number of bits specified by the right operand.
>> Binary Right Shift Operator. The left operands value is moved
right by the number of bits specified by the right operand.
Bitwise Operators (Example)
0 1 0 1 1
1 0 0 1 1
1 1 1 1 0
Bitwise Operators (Example)
A = 00111100 (60)
B = 00001101 (13)
Thank you
Any Questions