You are on page 1of 30

CSE 203G: Introduction to C

Lecture – 4

Course Teacher: Dr. Md Sadek Ferdous


Assistant Professor, CSE, SUST
E-mail: ripul.bd@gmail.com
Outline
• Signed and unsigned numbers
• C data types
• C constants
• C escape sequence
• C keywords
• C input and output
Signed and Unsigned numbers
• Every computer programmer must understand Signed and Unsigned Numbers
and its significance
• Positive numbers are represented as unsigned numbers
• So we don’t need to use +ve sign in front of them
• However, when it comes to negative numbers we use –ve sign
• This shows that number is negative and different from positive unsigned value
• This is why it’s represented as signed numbers
• Decimal numbers are to easy interpret as positive or negative:
• (+)10, -245 and so on….
• For binary it is a bit tricky
Signed and Unsigned numbers
• How to represent a signed number in binary?
• We need to know MSB (Most Significant Bit) and LSB (Least Significant
Bit)
• (MSB)10110010(LSB)
• We use the MSB to imply if a binary number is positive or negative
• That is why MSB is called a sign bit
• If the sign bit is 0, the number is positive, and if the sign bit is 1, the
number is negative
• We use the two’s complement method to represent a signed number
Signed and Unsigned numbers
• Let’s assume a 4 bit number:
• (6)10 = (0110)2
• (-6)10 = ?
• At first represent 6 in binary: 0110
• Make it one’s complement: 1001
• Add one to LSB: 1
1010 -> (-6)10
Signed and Unsigned numbers
• What are the numbers you can represent in 4 bits unsigned/signed
number?
• For unsigned, it is: 0 – 15 (0000 - 1111)
• For signed, it is: -8 to + 7
• -2(n-1) to +(2(n-1) – 1)
• A byte (8 bits)
• Unsigned: 0 - 255
• Signed: -128 to +127
C variables contd…
• If you remember
• Declaring a C variable is allocating a memory space for the variable
• But, how much space?
• It will depend on the variable type
• Every type has a specific space allocated in the memory
• How much space will you require?
• It will depend on the data your variable will deal with
• If it deals with large data, it will require to deal with a large memory
space
C variables contd…
• Let’s Assume, you need to deal with two numbers: 7 and 700
• It is not important what you will do with these numbers
• Now, you would like to use two variables for these two numbers
• Let’s assume, these two variables are: a and b
• How much space each of these variable need to occupy?
• Enough space so that you can accommodate these two number in these
variables
• How many bits for a? (Hint. How many bits required to represent a’s
content?)
C variables contd…
• Let’s Assume, you need to deal with two numbers: 7 and 700
• It is not important what you will do with these numbers
• Now, you would like to use two variables for these two numbers
• Let’s assume, these two variables are: a and b
• How much space each of these variable need to occupy?
• Enough space so that you can accommodate these two number in these
variables
• How many bits for a? (Hint. How many bits required to represent a’s
content?)
• Ans: a -> at least 3 bits
C variables contd…
• Let’s Assume, you need to deal with two numbers: 7 and 700
• It is not important what you will do with these numbers
• Now, you would like to use two variables for these two numbers
• Let’s assume, these two variables are: a and b
• How much space each of these variable need to occupy?
• Enough space so that you can accommodate these two number in this variables
• How many bits for a? (Hint. How many bits required to represent a’s
content?)
• Ans: a -> at least 3 bits
• How many bits for b? Will b having 8 bits be sufficient?
C variables contd…
• Let’s Assume, you need to deal with two numbers: 7 and 700
• It is not important what you will do with these numbers
• Now, you would like to use two variables for these two numbers
• Let’s assume, these two variables are: a and b
• How much space each of these variable need to occupy?
• Enough space so that you can accommodate these two number in this variables
• How many bits for a? (Hint. How many bits required to represent a’
content?)
• Ans: a -> at least 3 bits
• How many bits for b? Will b having 8 bits be sufficient?
• Ans: b -> at least 9 bits
C data types: revisited
• Four common data types in C
• int - can store integers (usually 4 bytes)
• float - can store single-precision floating point numbers (usually 4 bytes)
• double - can store double-precision floating point numbers (usually 8 bytes)
• char - can store a character (1 byte)
C data types: revisited
C data types: revisited
C constants
• Variables whose values can be initialised during declaration,
but cannot be changed after that
• Declared by putting the const keyword in front of the
declaration
• Storage allocated just like any variable
• Used for variables whose values need not be changed
• Prevents accidental change of the value
C constants
void main() {
const int LIMIT = 10;
int n;
scanf(“%d”, &n); Incorrect: Limit changed
if (n > LIMIT)
void main() {
printf(“Out of limit”); const int Limit = 10;
} int n;
scanf(“%d”, &n);
Limit = Limit + n;
printf(“New limit is %d”, Limit);
}
C constants
• Integer constants
• Consists of a sequence of digits, with possibly a plus or a minus sign
before it
• Embedded spaces, commas and non-digit characters are not permitted
between digits
• Floating point constants
• Two different notations:
• Decimal notation: 25.0, 0.0034, .84, -2.234
• Exponential (scientific) notation
3.45e23, 0.123e-12, 123e2 e means “10 to the power of”
• Character constants
• Contains a single character enclosed within a pair of single quote marks.
• Examples :: ‘2’, ‘+’, ‘Z’
C escape sequences
• Certain nonprinting characters can be expressed in terms of escape
sequences
• An escape sequence always begins with a backward slash and is followed
by one or more special characters
• For example, a line feed (LF), which is referred to as a newline in C, can be
represented as \n
• Newline means the next line
• Such escape sequences always represent single characters, even though
they are written in terms of two or more characters
• Examples:
• '\n', '\t', '\b', '\\', '\'' and so on
C escape
32 sequences C FUNDAMENTALS [CHA

_Character Escape Seauence ASCII Value


bell (alert) \a 007
backspace \b 008
horizontal tab \t 009
vertical tab \v 01 1
newline (line feed) \n 010
form feed \f 012
carriage return \r 013
quotation mark (") \ I' 034
apostrophe (') \ I 039
question mark (?) \? 063
backslash 0) \\ 092
null \O 000
C keywords
• Used by the C language, cannot be used as variable names
• Examples:
• int, float, char, double, main, if else, for, while. do, struct, union, typedef,
enum, void, return, signed, unsigned, case, break, sizeof,….
f u t u r e - v a l u e are appropriate symbolic names. However, v or f v would probably be too brief, since the intended
representation of these identifiers is not clear. On the other hand, the identifier f u t ure-value-of-an-investment
would be unsatisfactory because it is too long and cumbersome.

C keywords
There are certain reserved words, called keywords, that have standard, predefined meanings in C. These
keywords can be used only for their intended purpose; they cannot be used as programmer-defined identifiers.
The standard keywords are

auto extern sizeof


break floatn static
case for struct
char got0 switch
const if typedef
continue int union
default long unsigned
I

do register void
double return volatile
else short while
enum signed

Some compilers may also include some or all of the following keywords.
2.6 DECLARATIONS
C variable declaration
A declaration associates a group of variables with a specific data type. All variables must be declared
they can appear in executable statements.
• A declaration
A declaration consistsassociates a group
of a data type, of by
followed variables withvariable
one or more a specific
names, data type
ending with a sem
(Recall•that
All the permissible
variables mustdatabetypes are discussed
declared beforeinthey
Sec.
36 2.3.) Each array
can appear in variable
executablemust be followe
CF
pair of square brackets, containing a positive integer which specifies the size (i.e., the number of eleme
statements
the array.
• A declaration consists of a data type, followed bydeclarations
These one or more could also have been writte
variable names, ending with a semicolon
EXAMPLE 2.21 A C program contains the following type declarations. i n t a ;
i n t b;
int a , b, c ; i n t c;
f l o a t r o o t l , root2; float rootl;
char f l a g , t e x t [ 8 0 ] ; f l o a t root2;
char f l a g ;
char
Thus, a, b and c are declared to be integer variables, r o o t l and r o o t 2 are t e x t [ 8 0 ] ;variables, f l a g is a ch
floating-point
variable and t e x t is an 80-element, char-type array. Note the square brackets enclosing the size specification for t e
short i n t a , b, c;
char f l a g ;
long i n t r, s , t ;
char t e x t [ 8 0 ] ;
int P , 9;
C variable declaration
This form may be useful if each variable is to be accompanied its purpose.
by a comment
Some compilers explaining
will allocate less storage space toInth
however, items of the same type are usually combinedandinq.a single declaration.
Typical values are two bytes for each short inte
• A declaration associates a group of variable.variables Thewith a specific
maximum data
permissible type
values of a, b and c
Integer-type variables can be declared to bewhenshort integer
using for smaller
a compiler integer quantities, or lo
of this type.
• All variables must be declared before they canallocate
larger integer quantities. (Recall that some C compilers
appearless in executable
Similarly, some compilersstorage spaceadditional
will allocate to short
statements
additional storage space to long integers.) Suchinteger
variables are declared
variables by writing
p and q. Typical valuess hare
o rtwo nta
t i words
• A declaration
or simply s h o r t and lconsists of a data type,
o n g , respectively. for followed by integer
each ordinary one orvariable.
more The maximum per
variable names, ending with a semicolon permissible values of p and q when using one of these co
EXAMPLE 2.22 The above
A C program contains the following type declarations could have been written as
declarations.

short i n t a , b, c; short a, b y c ;
long i n t r, s , t ; long r, s , t ;
int P , 9; int P , 9;

to thesshort
Some compilers will allocate less storage spaceThus, h o r t integer r t i n t are, bequivalent,
and s h ovariables long
and c thanastoarethe int
and q. Typical values are two bytes for each short integer variable, and four bytes (one word) for each
Input: scanf function
• Performs input from keyboard
• It requires a format string and a list of variables into which the value
received from the keyboard will be stored
• format string = individual groups of characters (usually ‘%’ sign,
followed by a conversion character), with one character group for
each variable in the list
int a, b; Variable list (note the &
float c; before a variable name)

scanf(“%d %d %f”, &a, &b, &c);


Format string
Input: scanf function
• Commonly used conversion characters
c for char type variable
d for int type variable
f for float type variable
lf for double type variable
• Examples
scanf ("%d", &size) ;
scanf ("%c", &nextchar) ;
scanf ("%f", &length) ;
scanf (“%d%d”, &a, &b);
Reading a single character
• A single character can be read using scanf with %c
• It can also be read using the getchar() function
char c;
c = getchar();
• Program waits at the getchar() line until a character is typed,
and then reads it and stores it in c
Output: printf function
• Performs output to the standard output device (typically
defined to be the screen)
• It requires a format string in which we can specify:
• The text to be printed out
• Specifications on how to print the values
printf ("The number is %d\n", num);
• The format specification %d causes the value listed after the
format string to be embedded in the output as a decimal
number in place of %d
• Output will appear as: The number is 125
Output: printf function
• General syntax:
printf (format string, arg1, arg2, …, argn);
• format string refers to a string containing formatting information and data
types of the arguments to be output
• the arguments arg1, arg2, … represent list of variables/expressions whose
values are to be printed
• The conversion characters are the same as in scanf
Output: printf function
• Examples:
printf (“Average of %d and %d is %f”, a, b, avg);
printf (“Hello \nGood \nMorning \n”);
printf (“%3d %3d %5d”, a, b, a*b+2);
printf (“%7.2f %5.1f”, x, y);
• Many more options are available for both printf and scanf
• Read from the book
• Practice them in the lab
The lecture slides can be found in the following location!

You might also like