Intro To Algorithm and Programming 04
Intro To Algorithm and Programming 04
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
union
Enum
Typedef
DR.KALAN 2
C /C++ Programming
Primary Data Types in C/C++:
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
DR.KALAN 4
C /C++ Programming
Basic Data Type & Format in C/C++
There are different format specifiers for each data type.
DR.KALAN 5
C /C++ Programming
Data Types Default Values in C/C++:
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
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.
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
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
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-----------------------");
DR.KALAN 13
C /C++ Programming
Char / Char Data Type in C/C++
Example: Change upper case to lower case
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;
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
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)
DR.KALAN 19
C /C++ Programming
Escape Sequence List in C/C++ :
The table below lists some common escape sequences in C language.
DR.KALAN 20
C /C++ Programming
Operators in C/C++ :
Operators are used to perform operations on variables and values.
DR.KALAN 21
C /C++ Programming
Operators in C/C++ :
Example:
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.
DR.KALAN 23
C /C++ Programming
Assignment Operators in C/C++ :
Example:
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).
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:
DR.KALAN 26