0% found this document useful (0 votes)
24 views

1_Variable,Datatype&Operator

C Progrrame

Uploaded by

Mir Masrur
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
24 views

1_Variable,Datatype&Operator

C Progrrame

Uploaded by

Mir Masrur
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 33

Variable, Data type and Operator

Md. Farukuzzaman Faruk

Lecturer

Computer Science & Engineering Department

Rajshahi University of Engineering & Technology


C Programming
• The C programming language is a general-purpose, platform independent,
high-level language that was originally developed by Dennis M. Ritchie to
develop the UNIX operating system at Bell Labs.
• The language was formalized in 1988 by the American National Standard In-
stitute. (ANSI).
• C is used for systems programming
• compilers and interpreters,
• operating systems
• database systems
• microcontrollers etc.
C Features
 Easy to learn

 Structured language

 It produces efficient programs.

 It can handle low-level activities.

 It can be compiled on a variety of computer platforms.

•C is static (compiled), typed and imperative.

•"C is quirky, flawed, and an enormous success."–Ritchie


C Basic

• Variable declarations: int i ; float f ;

• Intialization: char c=’A’; int x=y=10;

• Operators: +,−,∗,/,%

• Expressions: int x,y,z;

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).

•C does not allow punctuation characters such as @, $, and % within identifiers.


C is a case sensitive programming language. Thus, Manpower and manpower
are two different identifiers in C. Here are some examples of acceptable identi-
fiers:

Mohd zara abc move_name a_123 myname50


_temp j a23b9 retVal
C Variables and datatypes
Datatypes:

•The datatype of an object in memory determines the set of values it can have
and what operations that can be performed on it.

•C allows implicit conversions as well as forced (potentially dangerous) casting.

•Generally, the type of a variable determines how much space it occupies in


storage and how the bit pattern stored is interpreted.
C Variables and datatypes

The datatypes in C can be classified as follows:


S.N. Types and Description
1 Basic Types:
They are arithmetic types and consists of the two types: (a) in-
teger types and (b) floating- point types.
2 Enumerated types:
They are again arithmetic types and they are used to define
variables that can only be assigned certain discrete integer val-
ues throughout the program.

3 The type void:


The type specifier void indicates that no value is available.

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:

Type Storage Size Value Range


char 1 byte -128 to 127 or 0 to 255
Unsigned char 1 byte 0 to 255
signed char 1 byte -128 to 127
-32,768 to 32,767 or -2,147,483,648
int 2 or 4 bytes
to 2,147,483,647
2 or 4 bytes 0 to 65,535 or
unsigned int
0 to 4,294,967,295
short 2 bytes -32,768 to 32,767
unsigned short 2 bytes 0 to 65,535
long 4 byte -2,147,483,648 to 2,147,483,647

unsigned long 4 bytes 0 to 4,294,967,295


sizeof operator
• To get the exact size of a type or a variable on a particular platform, you can
use the sizeof operator.
• The expressions sizeof(type) yields the storage size of the object or type in
bytes.
• Following is an example to get the size of int type on any machine:

#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:

Type Storage Value Range Precision


Size

float 4 byte 1.2E-38 to 3.4E+38 6 decimal places


double 8 byte 2.3E-308 to 1.7E+308 15 decimal places
long 3.4E-4932 to 1.1E+4932
10 bytes 19 decimal places
double
Floating-Point Types
#include <stdio.h>
#include <float.h>
int main()

printf("Storage size for float : %d \n", sizeof(float));

printf("Minimum float positive value: %E\n", FLT_MIN );


printf("Maximum float positive value: %E\n", FLT_MAX ); printf("Precision
value: %d\n", FLT_DIG );

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

char Typically a single octet(one byte). This is an integer type.

int The most natural size of integer for the machine.


float A single-precision floating point value.
double A double-precision floating point value.
void Represents the absence of type.
C Variables
Variable declaration:

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.

auto, else, long ,switch ,break ,enum ,


auto,
registerelse,
,typedeflong
,case,switch ,break
,extern ,return , ,enum ,
union ,char,typedef
register ,float ,short,case
,unsigned ,const ,,return ,
,extern
for ,signed ,void ,continue ,goto ,sizeof,
union ,char ,float
volatile ,default ,if ,static,short ,unsigned
,while ,do ,int, struct ,,const
_packed, ,double
for ,signed ,void ,continue ,goto ,sizeof,
volatile ,default ,if ,static ,while ,do ,int, struct ,
_packed ,double
Character constant
There are certain characters in C when they are preceded by a backslash they
will have special meaning and they are used to represent like newline (\n) or tab
(\t). Here, you have a list of some of such escape sequence codes:

Escape Sequence Meaning

\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 identifier value

• Using const keyword.


const type variable = value;
Defining Constant
#include <stdio.h>

#define LENGTH 10

#define WIDTH 5

#define NEWLINE '\n’

int main() {

int area;

area = LENGTH * WIDTH;

printf("value of area : %d", area);

printf("%c", NEWLINE);

return 0;

}
Defining Constant
#include <stdio.h>

int main() {

const int LENGTH = 10;

const int WIDTH = 5;

const char NEWLINE = '\n';

int area;

area = LENGTH * WIDTH;

printf("value of area : %d", 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;

Operator Description Example


+ Adds two operands A+B will give 30
- Subtracts second operand from the first A-B will give -10
* Multiplies both operands A*B will give 200
/ Divides numerator by de-numerator B/A will give 2
% Modulus Operator and remainder of after an B % A will give 0
integer division
++ Increments operator increases integer value by A++ will give 11
one
-- Decrements operator decreases integer value A-- will give 9
by one
Arithmetic Operators
#include <stdio.h>
main() {
int a = 21; int b = 10; int c ;
c = a + b;
printf("Line 1 - Value of c is %d\n", c );
c = a - b;
printf("Line 2 - Value of c is %d\n", c );
c = a * b;
printf("Line 3 - Value of c is %d\n", c );
c = a / b; printf("Line 4 - Value of c is %d\n", c );
c = a % b;
printf("Line 5 - Value of c is %d\n", c );
c = a++;
printf("Line 6 - Value of c is %d\n", c );
c = a--;
printf("Line 7 - Value of c is %d\n", c );
}
Relational Operators
int A=10; int B=20;
Operator Description Example
Checks if the values of two operands are equal or not, if (A==B) is not
== yes then condition becomes true. true
!= Checks if the values of two operands are equal or not, if (A!=B) is true
values are not equal then condition becomes true.

> 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;

Operator Description Example


&& Called Logical AND operator. If both the oper- (A && B) is false
ands are non-zero, then condition becomes
true.
|| Called Logical OR Operator. If any of the two (A || B) is true
operands is non- zero, then condition becomes
true.
! Called Logical NOT Operator. Use to reverses !(A && B) is true
the logical state of its operand. If a condition is
true, then Logical NOT operator will make false.
Logical Operators
#include <stdio.h> main()
{
int a = 5;
int b = 20;
int c ;
if ( a && b )
printf("Line 1 - Condition is true\n" );
if ( a || b )
printf("Line 2 - Condition is true\n" );
/* lets change the value of a and b */
a = 0; b = 10;
if ( a && b )
printf("Line 3 - Condition is true\n" );
else
printf("Line 3 - Condition is not true\n" );
if ( !(a && b) )
printf("Line 4 - Condition is true\n" );
}
Bitwise Operators
Operator Description
& Binary AND Operator copies a bit to the result if it exists in both
operands.
| Binary OR Operator copies a bit if it exists in either operand.

^ 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)

p q p&q p|q p^q


0 0 0 0 0

0 1 0 1 1

1 0 0 1 1

1 1 1 1 0
Bitwise Operators (Example)
A = 00111100 (60)
B = 00001101 (13)

•(A & B) will give 12, which is 0000 1100

•(A | B) will give 61, which is 0011 1101

•(A ^ B) will give 49, which is 0011 0001

•(~A ) will give -60, which is 1100 0011

•A << 2 will give 240, which is 1111 0000

•A >> 2 will give 15, which is 0000 1111


Bitwise Operators (Example)
#include <stdio.h>
Void main() {
unsigned int a = 60;
unsigned int b = 13;
int c = 0;
/* 60 = 0011 1100 */
/* 13 = 0000 1101 */
c = a & b; /* 12 = 0000 1100 */
printf("Line 1 - Value of c is %d\n", c );
c = a | b; /* 61 = 0011 1101 */
printf("Line 2 - Value of c is %d\n", c );
c = a ^ b; /* 49 = 0011 0001 */
printf("Line 3 - Value of c is %d\n", c );
c = ~a; /*-61 = 1100 0011 */
printf("Line 4 - Value of c is %d\n", c );
c = a << 2; /* 240 = 1111 0000 */
printf("Line 5 - Value of c is %d\n", c );
c = a >> 2; /* 15 = 0000 1111 */
printf("Line 6 - Value of c is %d\n", c );
}
Assignment Operators
Operator Description
+ C = A + B will assign value of A + B in C
+= C += A is equivalent to C = C + A
-= C -= A is equivalent to C = C - A
*= C *= A is equivalent to C = C * A
/= C /= A is equivalent to C = C / A
%= C %= A is equivalent to C = C % A
<<= C <<= 2 is same as C = C << 2
>>= C >>= 2 is same as C = C >> 2
&= C &= 2 is same as C = C & 2
^= C ^= 2 is same as C = C ^ 2
|= C |= 2 is same as C = C | 2
Misc Operators

Operator Description Example


sizeof() Returns the size of an variable.
& Returns the address of an variable.
* Pointer to a variable.
?: Conditional Expression (a>b)?1:0
Operators Precedence in C
Category Operator Associativity
Postfix () [] -> . ++ - - Left to right
Unary + - ! ~ ++ - - (type)* & sizeof Right to left
Multiplicative */ % Left to right
Additive +- Left to right
Shift << >> Left to right
Relational <<= >>= Left to right
Equality == != Left to right
Bitwise AND & Left to right
Bitwise XOR ^ Left to right
Bitwise OR | Left to right
Logical AND && Left to right
Logical OR || Left to right
Conditional ?: Right to left
Assignment = += -= *= /= %= >>= <<= &= ^= |= Right to left
Good Luck

 Thank you 

 Any Questions

You might also like