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

CLang Lect03

1) Variables in C have names that begin with letters or underscores and have specific data types like int, char, float, etc. Variables are declared with their type and name at the beginning of functions and can be assigned values. 2) Different data types like int, char, float have different ranges of values they can hold due to their specific bit sizes. Integer types can also be signed or unsigned. 3) Strings in C are groups of characters enclosed in double quotes and special escape characters represent non-printable characters.

Uploaded by

Thành
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
18 views

CLang Lect03

1) Variables in C have names that begin with letters or underscores and have specific data types like int, char, float, etc. Variables are declared with their type and name at the beginning of functions and can be assigned values. 2) Different data types like int, char, float have different ranges of values they can hold due to their specific bit sizes. Integer types can also be signed or unsigned. 3) Strings in C are groups of characters enclosed in double quotes and special escape characters represent non-printable characters.

Uploaded by

Thành
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 20

Data type

Department of Information System


SoICT, HUST

1
Variable
• A variable has a name or identifier that must begin
with an alphabetic letter or the underscore _
character.
• Example: tong, dem, _abc, i, j, n

• In C variables do not have only names: they also


have types
• Example:
int i,j;
char ch;

2
Where are variables?
#include <stdio.h>
int main()
{
int tong=0, dem=0, sopt, sosau;
printf(“So phan tu trong day so:");
scanf("%d", &sopt);

while (dem < sopt)


{ scanf("%d", &sosau);
tong += sosau;
dem++;
}

printf(“Tong la %d\n",tong);
return 0;
}
3
Variable declaration
• Variables in C should be declared at the beginning
of a function following the syntax:
<data type> <variable list>;
• Example: int i,j; char ch;
i = j = 0 ;ch = ‘A’;
• Assignment = puts a specified value into a specified
variable
• Example: int tong=0, dem=0;

4
Basic data types
• char : a single ASCII character
• short : a short integer (usually 16-bits)
• int : a standard integer (usually 32-bits)
• long : a long integer
• float : a floating point or real number
• double : a long floating point number

5
Integer numbers
• Integers are stored as binary numbers in memory.
• Example: The binary number 10100110 corresponds to
which number in decimal format?
• There are different integer types in C and they are
called char, short, int, long and long long. The
difference between these is the size of the integer
which either can hold and the amount of storage
required for them.
• The unsigned keyword should be used to specify an
unsigned integer
• Example: signed int i; unsigned int u;

6
Size of Integers
Type Bits Possible Values
char 8 -128 to 127
short 16 -32768 to 32767
unsigned short 16 0 to 65535
int 32 -2147483648 to 2147483647
long 32 -2147483648 to 2147483647
unsigned int 32 0 to 4294967295
long long 64 -9e18 to + 8e18

Keep in mind the capacity of each integer type to avoid


number overflow

7
Find value for the calculations
unsigned char a=49;
unsigned char b=49;
unsigned char c = a + b;

• What is the value for c?

8
Integer constants
• Decimal format:
• Example: 123456, -123456
• Hexa format (starts with 0x):
• Example: 0x12AB, 0xFFFF
• Octal format (starts with 0):
• Example: 0123456
• Note: 123456 and 0123456 are two different
numbers

9
Example
short i=0,j=0;
short a = 0xFFFF
int x,y;
x = y = 123456;
char k = 0xFF;

• What is the value of k? Given that char is a signed


integer with the size of 1 byte.

10
Floating Point Numbers
• There are also long and short floating point numbers
in C : float and double
• All the mathematical functions in C that often
require double type so it is common to use.
• Only use float if you want to store small floating
point numbers.

11
Size of floating point numbers
Type Bits Possible Values
float 32 +/- 10E-37 to +/- 10E38
double 64 +/- 10E-307 to +/- 10E308

• Note: all calculations with real values in the computer are only
"approximate“ ➔ The larger representing size of the real value,
the higher accuracy of calculations.

12
Floating point constants
• Use point:
• Example: 123.456, 12.456e-2 = 12.456x10-2
• Use scientific syntax:
Example:
• double x,y,z; x = 0.1;
• y = 2.456E5
• z = 0;

13
Characters
• A character is stored as a (1 byte) number called
character code.
• Example: ‘A’ = 65, ‘0’ = 48
• English characters are coded using ASCII table
• Use char type to store a character
• The two declarations below are identical:
• char ch = 'A';
• char ch = 65; (The ASCII code of the character)

14
15
Control characters
• In the ASCII table, only characters whose code from
32 (space character) are printable characters, all
characters from 0 to 31 are used as control characters.
• Example: character code 13 is new line; 9 is horizontal tab
• Control characters are put into programs by using a backslash
\ and a special character or number. For example:
• ’\n’ new line NL (like pressing return)
• ’\t’ horizontal tab HT
• ’\0’ null character
• ’\’’ ’
• ’\\’ \

16
Strings
• Groups of char form strings.
• A string must be enclosed by double quote
• Examples:
• “Hello world!”
• “Line1\nLine2\nLine3”
• “I\’m a teacher”

17
Logic
• In C, every number give also a boolean value, false
if the number is equal to zero, true otherwise.
• We normally used
• 0 for false
• 1 for true
• Example:
int i = 1;
if ( i )
{
printf(“i has true value”);
}

18
const declaration
• A variable declaration with the prefix const indicates
that the value of that variable never changes in the
program.
• Example:
• const int i = 5;
• const char c = ‘A’;
• const float pi = 3.14;

19
Thank you
for your
attentions!

You might also like