Lecture 6
Lecture 6
Lecture 6
IIT (BHU)
Another Program
/* Addition of two numbers */
# include <stdio.h>
main()
{
int number;
float amount;
number = 100;
amount = 30.75 + 75.35;
printf(“%d \n”, number);
printf(“%5.2f \n”, amount);
}
Syntax and meaning of this program
▷ number and amount are variables (names of variables) that
are used to store numeric data – Have a data type
▷ Data type of number is int (integer) and amount is float
(floating point number)
▷ All variables have to be declared before use by specifying
their name and data type
▷ Declarations normally occur in the beginning of a program
and end with a semicolon
▷ Note that int and float are called keywords they cannot be
used as variable names.
Continued…
▷ Data is stored in variables by assignment statements e.g. a =
10; b = 1.33;
▷ printf function here prints the value of variables number and
amount.
▷ There are two arguments to the printf function – first between
quotes, second the variable name
▷ The last line prints the value of amount in floating point format
(for real numbers).
▷ The format specification %5.2f tells output must be at least 5
places totally with two decimal places to the right of decimal
point
Basic Structure of C
▷ Building blocks of C are functions (subroutines that include
one of more statements to perform a specified task)
▷ Difference between compiling and execution
▷ Keywords in C – Fixed meaning cannot be changed – building
blocks of program statements
▷ ANSI C has 32 keywords - auto, break, case, char, const,
continue, default, do, double, else, enum, extern, for, goto, if,
long, register, return, short, signed, sizeof, static, struct,
switch, typedef, union, unsigned, void, volatile, while
Identifiers in C
▷ Identifiers are variable names – There are rules for what is
a valid identifier in C
▷ These can contain letters, digits and underscore – digits
cannot be first character
▷ Both uppercase and lowercase are permitted
▷ First character must be alphabet or underscore
▷ Only first 31 characters are significant
▷ Examples of valid identifiers – a, a10, _VAL
▷ Some invalid identifiers 10a, ad3@@
Classification of Characters
▷ In C, characters are classified as
1. Letters – uppercase A to Z and lowercase a to z
2. Digits – from 0 to 9
3. Special characters – comma, period, semicolon, colon,
question mark, apostrophe (‘), quotation mark, exclamation
mark (!), vertical bar (|), slash (/), backslash (\), tilde (~),
underscore (_), $, %, &, ^, *, -, +, >, <, (, ), {, }, #, [, ]
4. White spaces - blank space, tab (horizontal), carriage return,
newline
C Tokens
▷ Individual words and punctuation marks are called
tokens
▷ In C tokens are the smallest individual unit
▷ Tokens can be
1. Keywords
2. Identifiers
3. Constants
4. Strings
5. Special symbols
6. Operators
Constants
▷ Constants do not change during execution of program. They
can be
1. Integer constants
2. Real constants
3. Single character constants
4. String constants
▷ Integer constants – sequence of digits – numbers can be either
in decimal, octal or hexadecimal
▷ They can contain digits 0 to 9 and an optional +/- sign
Integer Constants
▷ Valid integer constants – 123, -11, 0, +78
▷ Invalid integer constants 12 00, 1,200, $1200
▷ Note that octal numbers are preceded by 0 and can contain
digits 0 to 7– Examples 037, 066 (Valid), 098 (Invalid)
▷ Hexadecimal numbers are preceded by 0x or 0X and can
contain digits 0 to 9 and alphabets A to F - Example 0X12A,
0x9F, (Valid), 0xG1 (Invalid)
▷ Largest integer that can be stored is machine-dependent
▷ Generally integers occupy 16/32 bits of storage, long integer
more space
▷ Unsigned integer is a positive integer
Signed Arithmetic
▷ Binary numbers have only 0/1 as digits –
0,1,10,11,100,101,110,111,…
▷ For example 11 corresponds to three
▷ How to convert binary to decimal and back ?
▷ What is 40 in binary and 1101 in decimal ?
Signed Arithmetic
▷ How can you represent -20 (minus twenty) in binary ?
▷ You can give a separate bit (0/1) for +/-
▷ But if you do this you will have -0 and +0 as different
▷ Instead computer performs operations in 2’s complement
▷ One’s complement – invert one’s and zero’s, i.e., change 0 to 1
and 1 to 0
▷ Example 6 in binary in 110 its one’s complement is 001
▷ Two’s complement – add one to ones complement – for
example two’s complement of 6 is 010