You are on page 1of 44

Character Set

Characters are the basic building blocks in C


program, equivalent to letters in English
language

Includes every printable character on the standard


English language keyboard except `, $ and @

Characters are classified into categories:


Numeric Digits: 0 - 9
Letters (Lowercase/uppercase ): a - z and A - Z
White Spaces
Blank Space
Horizontal Tab
Vertical Tab
New Line
Form feed

Special characters: , . ; ? / ( ) [ ] { } * & % ^ <


> etc
DELIMITERS
Special kind of symbols
: Colon Useful for label
; Semi-colon Terminates statement
( ) Parenthesis Expressions and functions
[ ] Square bracket Array Declaration
{ } Curly brace Scope of statement
# Hash Preprocessor directive
, Comma Variable Separator
Tokens
Smallest element in the C language
It may be a single character or a sequence
of characters to form a single item.
Tokens can be:
Keywords
Identifiers
Constants
Strings
Special Symbols
Operators
Keywords
Reserved words by compiler having a
definite meaning.
Are defined as a part of the C language.
Written in lower case
Serves as building blocks of program
statement
Reserved Words (Keywords) in C
auto break int long
case char register return
const continue short signed
default do sizeof static
double else struct switch
enum extern typedef union
float for unsigned void
goto if volatile while
Identifiers
Names of variables, functions and arrays
User-defined names, consisting of letters and
digits with letter as first character.
Lower case characters are preferred.
_ (underscore can be used as identifier, to link
between 2 words in long identifiers.
Naming Conventions
Can be of any length, but on the first 31 are
significant
Are case sensitive:
abc is different from ABC
Must begin with a letter and the rest can be
letters, digits, and underscores.
Cannot be a keyword
Must not contain white space
Begin variable names with lowercase letters
Constants
A constant specifies a value that cannot be
modified by the program
C Constants

C Constants

Numeric Character

Integer Real Single character String


Numeric Constants
Numeric constants are a sequence of digits .
Positive or negative whole numbers with no
fractional part
Examples:
123
+30
-50
Real Constants
Positive or negative decimal numbers with
an integer part, a decimal point and a
fractional part
Could be represented in decimal notation .Ex:
24.7
+30.689
-50.9898
Could be represented in scientific notation .Ex:
0.65e4
12e-2
1.5e+5
Single Character Constants
One character defined character set.
Enclosed within a pair of single quotation
marks.
They have an integer value called ASCII values
Examples:
A
a
$
4
String Constants
A sequence characters enclosed by double
quotation marks.
Can be a combination of all kind of symbols
Examples:
UMBC
I like ice cream.
123
CAR
car
Variables
A data name used for storing a data value.
Its value can be changed during program execution.
Variable names correspond to locations in the
computer's memory
Every variable has a name, a type, a size and a value
sizeof() is a function that returns the size of a given
variable in bytes (the size depends on the type of the
variable
Variables should be initialized before they are used
(e.g., in the declaration) otherwise the variables
contain a random value
Naming Conventions
Can be of any length, but on the first 31 are
significant
Are case sensitive:
abc is different from ABC
Must begin with a letter and the rest can be
letters, digits, and underscores.
Cannot be a keyword
Must not contain white space
Begin variable names with lowercase letters
Declaring Variables
Before using a variable, you must give the
compiler some information about the variable; i.e.,
you must declare it.

Syntax:
Data_type variable_name;

Examples of variable declarations:


int meatballs ;
float area ;
When we declare a variable
Space is set aside in memory to hold a value of the
specified data type
That space is associated with the variable name
That space is associated with a unique address
Unless we specify otherwise, the space has no known
value.
Visualization of the declaration
int meatballs ;
Initializing Variables
Variables may be be given initial values, or
initialized, when declared.

Syntax
variable_name = constant;
data_type variable_name = constant;
Examples:
int length = 7 ;
float diameter = 5.9 ;
char initial = A ;
Assigning values to variables
Variables may have values assigned to them through the
use of an assignment statement.
Such a statement uses the assignment operator =
This operator does not denote equality. It assigns the value
of the right-hand side of the statement (the expression) to
the variable on the left-hand side.
Examples:
diameter = 5.9 ;
area = length * width ;
Note that only single variables may appear on the left-hand
side of the assignment operator.
Constant variable
A variable whose value we want to remain
unchanged during the execution of a
program .
It can be done by prefixing the keyword
const with variable while declaring it.
Example
const m=10;
Data Types
C language is rich in its data types. The variety of
data types available allow the programmer to
select the type appropriate to the needs of the
application as well as the machine.
ANSI C supports three classes of data types:
1. Primary (or fundamental) data types
2. Derived data types
3. User-defined data types
Primary Data Types
Data type and range
Data types Range of values
char 128 to 127
int 32,768 to 32,767
float 3.4e38 to 3.4e+e38
double 1.7e308 to 1.7e+308
TYPE SIZE Range
(Bits)
Char or Signed Char 1 -128 to 127

Unsigned Char 1 0 to 255

short or int 2 -32768 to 32767

Unsigned int 2 0 to 65535

Long int or signed long int 4 -2147483648 to


2147483647
Unsigned long int 4 0 to 4294967295

Float 4 3.4 e-38 to 3.4 e+38

Double 8 1.7e-308 to 1.7e+308

Long Double 10 3.4 e-4932 to 3.4


e+4932
Type Conversion
Implicit type conversion
Carried out by compiler automatically
Explicit type conversion
Carried out by programmer using casting
The qualifier signed or unsigned may be applied to
char or any integer.
Unsigned numbers are always positive or zero,
and obey the laws of arithmetic modulo 2n,where n
is the number of bits in the type.
So, for instance, if chars are 8 bits, unsigned char
variables have values between 0 to 255, while
signed chars have values between 128 and 127.
Whether plain chars are signed or unsigned is
machine-dependent, but printable characters are
always positive.
Implicit type conversion
If operands are of different types ,the lower
type is automatically converted to higher
type before operation proceeds
The result is of higher type.
char and short can be converted to int
float can be converted to double
int i ;
float b;
i=3.5;
b=30;
printf(%d,i);
Printf(%f,i);
Float value cannot be stored in i i.e int
Float value gets demoted to int then stored in integer
variable
Similarly integer value gets stored in a float variable
after being promoted to float type
float a,b,c;
int s;
s=a*b*c/100+32/4-3*11;
printf(%d,s);
Explicit Conversion
In C, the type of a value can change during
the run time of a program, this is known as
type conversion or type cast
The change can be explicit (the programmer
does it) .
Syntax:
(type-name)expression

type-name is one of C standard data types


int ratio;
int females;
int males;
ratio= (float)females/males;
/* ratio is transformed into float */
Typedef
To create user defined data types
To construct shorter or more meaningful
names for types already defined by C or for
types that you have declared. It does not
reserve storage.
The names you define using typedef are
not new data types, but synonyms for the
data types or combinations of data types
they represent.
Example
Declare LENGTH as a synonym for int and then
use this typedef to declare length, width, and height
as integer variables:

typedef int LENGTH;


LENGTH length, width, height;

The following declarations are equivalent to the


above declaration:
int length, width, height;
void main()
{
typedef int hours;
hours hrs;
hours H=60;
clrscr();
printf(Enter Hours);
scanf(%d,&hrs);
printf(\n Minutes = %d,hrs*H);
printf(\n Seconds = %d,hrs*H*H);
}
Derived Datatypes

Array
Stuctures
Unions
Enums
Etc
Enum Data Type
enum is the keyword to declare and
initialize a sequence of integer constants.
To create our own data type and define what
values the variables of these data types can
hold
Example
enum colors {RED, YELLOW, GREEN, BLUE};

colors is the name given to the set of constants and


their values are constant unsigned integers and
start from 0
the default value for the first one in the list
- RED in our case, has the valueof 0.
YELLOW is 1, GREEN is 2 , BLUE is 3.
printf(\n%d,RED);
printf(\n%d,YELLOW);
printf(\n%d,GREEN);
printf(\n%d,BLUE);
But you can assign values if you wanted to:
enum colors {
RED=1, YELLOW, GREEN=6, BLUE };
Now RED=1, YELLOW=2, GREEN=6 and
BLUE=7.
The main advantage of enum is that if you don't
initialize your constants, each one would have a
unique value. The first would be zero and the rest
would then count upwards.
You can name your constants in a weird order if
you really wanted...
#include <stdio.h>
void main() {
enum {RED=5, YELLOW, GREEN=4, BLUE};
printf("RED = %d\n", RED);
printf("YELLOW = %d\n", YELLOW);
printf("GREEN = %d\n", GREEN);
printf("BLUE = %d\n", BLUE);
getch();
}
Output:
RED = 5
YELLOW = 6
GREEN = 4
BLUE = 5

You might also like