Download as pdf or txt
Download as pdf or txt
You are on page 1of 26

Lecture 04

We will cover these skills:


 C/C++ Data Types
 Declaring Variables
 Input / Output Syntax
 Logical operation
 Escape Sequence
 Assignment Operators

REZA KALAN 1
C /C++ Programming
Data Types in C/C++:
Character

Primary Integer

Data Types in C
Float
Double
Boolean
Void

Function

Derived Array

Pointers

Reference

Class

User Defined Structure

union

Enum

Typedef

DR.KALAN 2
C /C++ Programming
Primary Data Types in C/C++:

Primary Data Type

Character Integer Boolean Void Float Double

Signed Unsigned
char Signed Unsigned
char char

int Short int Long int int Short int Long int

DR.KALAN 3
C /C++ Programming
Basic Data Type in C/C++
The data type specifies the size and type of information the variable will store

Data Type Size Description Example


int 2 or 4 bytes Stores whole numbers, without decimals 1
float 4 bytes Stores fractional numbers, containing one or more decimals. 1.99
Sufficient for storing 6-7 decimal digits
double 8 bytes Stores fractional numbers, containing one or more decimals. 1.99
char 1 byte Stores a single character/letter/number, or ASCII values 'A'

DR.KALAN 4
C /C++ Programming
Basic Data Type & Format in C/C++
There are different format specifiers for each data type.

Format Specifier Data Type


%d or %i int
%f or %F float
%lf double
%c char
%s Used for string (text), which you will learn more about in a later chapter

DR.KALAN 5
C /C++ Programming
Data Types Default Values in C/C++:

Data Type Default Value Comments


bool false
char \0 Null value
int 0
float 0
double 0.0f
It is a special type known as empty data type.
void It is used to state that a given variable does not have any type.
For example void pointer  It can point to any data type like integer,
character, structure, and also to other pointers.
wchar_t 0 In some compiler, it represents a 16-bit wide character

Note: C is a case-sensitive language. There is a difference between small and capital characters

DR.KALAN 6
C /C++ Programming
Size of Each Data Types in C/C++:
#include <stdio.h>
int main() {
printf("int is %2d bytes \n", sizeof(int)); // returns the number of bytes used to store a integer
printf("int * is %2d bytes \n", sizeof(int *)); // returns the number of bytes used to store a pointer
printf("long int is %2d bytes \n", sizeof(long int));
printf("signed int is %2d bytes \n", sizeof(signed int));
printf("unsigned int is %2d bytes \n", sizeof(unsigned int));
printf("\n-------------------------------------------------------------");
printf("float is %2d bytes \n", sizeof(float));
printf("float * is %2d bytes \n", sizeof(float *));
printf("double is %2d bytes \n", sizeof(double));
printf("double * is %2d bytes \n", sizeof(double *));
printf("long double is %2d bytes \n", sizeof(long double));
printf("\n------------------------------------------------------------");
printf("char is %2d bytes \n", sizeof(char));
printf("char * is %2d bytes \n", sizeof(char *));
printf("unsigned char is %2d bytes \n", sizeof(unsigned char));
return 0;
}

DR.KALAN 7
C /C++ Programming
Variable in C/C++
Variables are containers for storing data values, like numbers and characters.
Each variable has 3 main properties
 It has a type
 It has a name
 It has address in memory

Examples:
 int a; // a is an integer variable
 char ch; // ch is a character variable
 float f; // f is a float variable

 Variable may have initial value


Example:
 Int a=10;

DR.KALAN 8
C /C++ Programming
Variable in C/C++
C++ Identifiers :
 variables must be identified with unique names.
 These unique names are called identifiers.
 Identifiers can be short names (for example x or y) or more descriptive names (age, sum, result).
 It is recommended to use descriptive names in order to create understandable and maintainable code:

Example:
 int minutesPerHour = 60; // it is good because it is more clear
 int m = 60; // it is OK, but not so easy to understand what m actually is
 const float PI = 3.14; // we will discuses about const in next slides

DR.KALAN 9
C /C++ Programming
Number/ Integer Data Type in C/C++
It is used to store integer values.
The size of int is compiler dependent.
Note: \t (or Horizontal tab) inserts some whitespace to
 For example, 2 bytes or 4 bytes the left of the cursor and moves the cursor accordingly.

For 2 bytes, it’s 0 to 65,535


For 4 bytes, it’s 0 to 4,29,49,67,296 #include <stdio.h> output
void main() {
int a, b; a is : 10
Note: a and b are variable that has initial value a = 10; b is : 15
b = 15; a+b is : 25

printf("\n a is :\t%d", a);


printf("\n b is :\t%d", b);
Note: \n (or New Line) moves the
cursor to the start of the next line.
printf("\na+b is :\t%d",a+b);
}

DR.KALAN 10
C /C++ Programming
Number/ Float Data Type in C/C++
 Floating point numbers are used to store decimal numbers.

The range of values it can store also varies from compiler to compiler.
For 32-bit and 64-bit compilers, it is the same as 4 bytes

#include <stdio.h> output


void main() {
float f, a; f is :1.200000
f = 1.20 ; a is :1213.46
a = 1213.456;

printf("\n f is :%f", f);


printf("\n a is :%.2f", a);
}

DR.KALAN 11
C /C++ Programming
Number /Double Data Type in C/C++
 The size of the double is 8 bytes.
It can store values up to 15 decimal points without loss of precision.
The format specifier for double is %lf

#include <stdio.h> output


void main() {
double a, b; a is :1.000000
a=1; b is :1213.456000
b = 1213.456; b is :1213
b is :1213.5
printf("\n a is :%lf", a); b is :1213.46
printf("\n b is :%f", b);
Note: Do not use %d for double printf("\n b is :%.f", b);
printf("\n b is :%.1f", b);
printf("\n b is :%.2f", b);
}

DR.KALAN 12
C /C++ Programming
Char /Char Data Type in C/C++
#include <stdio.h> output
Char is used to store a single void main() {
character and requires 1 byte. char ch='a'; ch is :a
char char1=66; ----------------------------
A character could be any alphabet, signed char char2=-123; char : 66
number or special unsigned char char3=-123; Signed char :-123
Unsigned char :133
printf("\n ch is :%c", ch); ----------------------------
Example: printf("\n-----------------------"); char : B
Signed char : �
char ch =‘a’; printf("\n char :%d", char1); Unsigned char : �
printf("\n Signed char :%d", char2);
char ch=‘*’; printf("\nUnsigned char :%d", char3);
printf("\n-----------------------");

printf("\n char :\t%c", char1);


printf("\n Signed char :\t%c", char2);
printf("\nUnsigned char :\t%c", char3);
}

DR.KALAN 13
C /C++ Programming
Char / Char Data Type in C/C++
Example: Change upper case to lower case

#include <stdio.h> output

int main() ch is: A


{ ASSIC code for ch is: 65
char ch='A'; Lower case of 'A' is : a

printf("ch is: %c", ch);


printf(“\n ASSIC code for ch is: %d", ch);
printf(“\n Lower case of 'A' is: %c", ch+32);

return 0;
}

DR.KALAN 14
C /C++ Programming
Char / String in C/C++
If you try to store more than a single character, it will only print the last character:
Note:Don't use the char type for storing multiple characters, as it may produce errors.
 For more than one character we use string,

#include <stdio.h>
int main() {
char Text1 = 'Hello'; // not valid definition
printf("%c", Text1); // return error
printf("\n----------------------");
char Text2[] ="Hello";
printf("\n%c", Text2); // print one unknown character
printf("\n----------------------");
char Text3[] ="Hello";
printf("\n%s", Text3); // print Hello

return 0;
}

DR.KALAN 15
C /C++ Programming
Boolean data Type in C/C++
A Boolean data type is declared with the bool keyword and can only take the values true or false.
When the value is returned, true = 1 and false = 0.
#include <iostream> output
using namespace std;
3 == 5 is 0
int main() { 3 != 5 is 1
int a = 3, b = 5; bool result; 3 > 5 is 0
result = (a == b); // false
cout << "3 == 5 is " << result << endl;

result = (a != b); // true


Note: endl is equal with \n used for moving new line cout << "3 != 5 is " << result << endl;

result = a > b; // false


cout << "3 > 5 is " << result << endl;
return 0;
}

DR.KALAN 16
C /C++ Programming
Constants in C/C++
When you do not want others (or yourself) to change existing variable values, use the const keyword (this will declare the
variable as "constant", which means unchangeable and read-only):

Example
const int myAge= 35; // myAge will always be 35
myAge = 25; // return error: assignment of read-only variable 'myAge'

DR.KALAN 17
C /C++ Programming
Static Variables in C/C++ :
Static variable only initialized one time #include <stdio.h> Output

void static_vars(void){ i = 1, f = 0.100000, c = c


static int i = 0; i = 2, f = 0.200000, c = d
static float f = 0.0; i = 3, f = 0.300000, c = e
static char c = ‘b'; // static char c = '\0';
i++;
f += 0.1;
c++;
printf("i = %d, f = %f, c = %c\n", i, f, c);
}

int main(){
static_vars();
static_vars();
static_vars();
return 0;
}

DR.KALAN 18
C /C++ Programming
I/O Command in C/C++ :
printf (or cout “<<“) is used to print something (user output)
scanf (or cin “>>”) is used to reading from keyboard (user input)

#include <stdio.h> #include <stdio.h> output

int main(void) int main(void) { please enter first number :2


{ int num1, num2, sum; please enter second number :3
int num1, num2, sum; printf("please enter first number :"); sum = 5
printf("Type the two integers to sum :"); scanf(" %d", &num1);
scanf(" %d %d", &num1 , &num2); printf("please enter second number :");
sum = num1 + num2; scanf(" %d", &num2);
printf("\n sum = %d ", sum); sum = num1 + num2; Note: we can read value from
return 0; printf("\n sum = %d ", sum); input one by one.
return 0; scanf(" %d , &num1);
} } scanf(" %d, &num2);

DR.KALAN 19
C /C++ Programming
Escape Sequence List in C/C++ :
The table below lists some common escape sequences in C language.

Escape Sequence Name Description


\a Alarm or Beep It is used to generate a bell sound in the C program.
\b Backspace It is used to move the cursor one place backward.
\n New Line It moves the cursor to the start of the next line.
\r Carriage Return It moves the cursor to the start of the current line.
\t Horizontal Tab It inserts some whitespace to the left of the cursor and moves the cursor accordingly.
\v Vertical Tab It is used to insert vertical space.
\\ Backlash Use to insert backslash character.
\’ Single Quote It is used to display a single quotation mark.
\” Double Quote It is used to display double quotation marks.
\? Question Mark It is used to display a question mark.
\0 NULL It represents the NULL character.
\s Space Character It represents the ASCII space character.

DR.KALAN 20
C /C++ Programming
Operators in C/C++ :
Operators are used to perform operations on variables and values.

Operator Name Description Example


+ Addition Adds together two values x+y
- Subtraction Subtracts one value from another x-y
* Multiplication Multiplies two values x*y
/ Division Divides one value by another x/y
% Modulus Returns the division remainder x%y
++ Increment Increases the value of a variable by 1 ++x
-- Decrement Decreases the value of a variable by 1 --x

DR.KALAN 21
C /C++ Programming
Operators in C/C++ :
Example:

#include <iostream> Output


using namespace std; 11

int main(void){ 4
int a=12, b=3;
cout << --a << endl; 7
cout << ++b << endl;
cout << a-b << endl; 15
cout << a+b << endl;
cout << a*b << endl; 44
cout << a/b << endl;
cout << a%b << endl; 2
return (0);
} 3

DR.KALAN 22
C /C++ Programming
Assignment Operators in C/C++ :
Assignment operators are used to assign values to variables.

Operator Example Same As


= x=5 x=5
+= x += 3 x=x+3
-= x -= 3 x=x-3
*= x *= 3 x=x*3
/= x /= 3 x=x/3
%= x %= 3 x=x%3
&= x &= 3 x=x&3
|= x |= 3 x=x|3
^= x ^= 3 x=x^3
>>= x >>= 3 x = x >> 3
<<= x <<= 3 x = x << 3

DR.KALAN 23
C /C++ Programming
Assignment Operators in C/C++ :
Example:

#include <iostream> Output


using namespace std;
x/2 is: 4
int main(void){ x shift to right: 2
int x=8; x XOR'ed by 1: 3
x/=2;
cout<< "x/2 is:\t\t\t" << x << endl;
x>>=1;
cout << "x shift to right:\t"<< x << endl;
x^=1; // XOR
cout << "x XOR'ed by 1:\t\t" << x << endl;
return (0);
}

DR.KALAN 24
C /C++ Programming
Comparison Operators in C/C++ :
Comparison operators are used to compare two values (or variables). This is important in programming, because it
helps us to find answers and make decisions.
The return value of a comparison is either 1 or 0, which means true (1) or false (0).

Operator Name Example


== Equal to x == y
!= Not equal x != y
> Greater than x>y
< Less than x<y
>= Greater than or equal to x >= y
<= Less than or equal to x <= y

DR.KALAN 25
C /C++ Programming
Logical Operators in C/C++ :
Like comparison operators, we can also test for true (1) or false (0) values with logical operators.
Logical operators are used to determine the logic between variables or values:

Operator Name Description Example


&& Logical and Returns true if both statements are true x < 5 && x < 10
|| Logical or Returns true if one of the statements is true x < 5 || x < 4
! Logical not Reverse the result, returns false if the result is true !(x < 5 && x < 10)

DR.KALAN 26

You might also like