You are on page 1of 24

Chapter 3

C Fundamentals
C Character Set
 Lower case letters :abc … z
 Upper case letters : A B C… . Z
 Digits :0123 …9
 Special characters : + - * / = % ( ) & % $ # { } [ ] ’ “ :
;^?~<>,
 White space characters : blank, new line, tab space

This is similar to the characters which appear in


the script of a human language
C token set

This is similar to the parts of the grammar


which of a human language like verb,
adjective etc
Constants
 As the name suggests constants are those C tokens
which never changes it’ s value.
Constants

Numeric Character

Integer Real Single String


10 5.25 ‘c’ “ program”
Identifiers
 It is the names of variables, functions, labels etc given by
the programmer in writing the code.
 Identifiers contains alphabets, digits and underscore_
symbol.
 It can’t include spaces.
 It can only start with an alphabet or underscore.
Valid Identifiers Invalid Identifiers

Order_no order-no

NAME1 area circle

_apple 1a
Keywords
 Also known as reserve words.
 Keywords are defined by C compiler and each word has
different meaning.
 These cannot be used as identifiers for the names of
variables, functions or labels.

 The keywords can be grouped into two broad categories


 data types
 control flow
ANSI C keywords
Category Keywords
Data Types Basic data types char int float double
Type modifiers short long signed unsigned
Type qualifiers const volatile
Storage Classes auto static register extern
User Defined data enum struct union typedef
types
other void sizeof
Control Selection if else switch case default
Flow Loops for while do
Transfer of Control break continue return goto
Data types in C
 Data type determines the type of data a variable will
hold and also the range of values it can store. .
 It also informs the compiler how much bytes of
memory to reserve for a variable of a given data type.
 Classes of data types in C
 1. Basic or primitive data type
 2.Derived data type
 3. User defined data type
Data Size

 Bit - 0/1
 1 Nibble = 4 Bits
 1 Byte = 8 Bits
 1 Word = 2 Bytes/ 16 bits
 1 kb= 103 Bytes
 1 mb =103 kb
 1 gb = 103 mb
Basic data types
 There are 4 basic data types in C.
 char data type for a single character data.
 int data type for integer numbers.
 float and double data types for real numbers which differ in precision.

 In addition C provides a void type which is used to indicate absence of


type which is generally used in function declarations.
Data type modifiers
 These are keywords which alter the range of values which the basic data
type can store.
 short : It reduces the size of the data type by half.
 long : It doubles the storage capacity of the data type being used.
 signed : It means that the data type is capable of storing negative
values. It is by default
 unsigned : It can only store positive values.

Data type qualifiers


 These are keywords that control how the variables may be accessed or
modified.
 const : It is to specify that the data element is constant. i.e it’s value
cannot be changed.
 volatile : It is to specify that the data element may change at any
time because of some external sources
Basic data types cont…
Variables in C
 A variable in C is a name given to storage location in
memory.
 In other words it is an entity whose value changes during
program execution.
 An identifier is used as a variable name.
 The rules which apply to an identifier are the same for a
variable.
 Usually lowercase names are used

Each variable has a name and a


data type associated with it.
Variable declaration
 Each variable must first be declared before it is used in a
program.
 It consists of a data type followed by the variable name
terminated by a semicolon.
 Syntax
data_type variable_name;
E.g int num;

 Multiple variables of the same data type can be declared in the


same C statement seperated by commas
data_type variable_name1, variable_name2, variable_name3, ….;
E.g int num1,num2,sum;
Variable initialization or definition
 It is the expression in which values are initialized to the declared
variables.
 In a multi variable C statement, it is possible to initialize only a
few of them. But it is not a good programming practice.
 Syntax
data_type variable = expr ; E.g int num = 5;
int num1=5, num2=10, sum;

There should be a white space between the keyword int and


the variable name.
Also C allows flexibility in variable initialization. The above
statement can be split
int num;
num=5;
Symbolic Constant
 It is a name given to a sequence of characters using the #define
preprocessor directive.
 It gives more meaning to literal constants and expressions making the
program more readable.
 It behave as a macro and is usually written in uppercase to distinguish
them from variables.
 Once the symbolic constant is defined we can use it in the rest of that
program instead of the value associated with it.
Example
#define PI 3.142
There is no = operator between PI and 3.142
and as such #define is not an assignment
statement.

Symbolic constants are not variables


Storage Classes in C
 In C, the scope and lifetime of a variable or function
within a program is determined by its storage class.
Each variable has a lifetime, or the context in which
they store their value.
 There are 4 storage classes in C:
 auto
 register
 static
 extern
Storage Classes cont….
 auto  register
 auto is the default storage  It offers faster access than
class. those stored in memory.
 It is stored in the memory  It is stored in CPU registers.
 it has garbage value initially.  It has garbage value initially.
 Its scope is local to the  Its scope is local to the
function or block in which it function or block in which it
is declared. is declared.
 It’s lifetime is until the  It’s lifetime is until the
program is in the block program is in the block
Storage Classes cont….
 static  extern
 It is stored in the memory  It is stored in the memory
 It’s value is 0 initially.  It’s value is 0 initially.
 Its scope is local to the  Its scope is global i.e it is
function or block in which it visible in all files.
is declared.  It’s lifetime is until the
 It retains its value between program execution doesn’t
function calls end

Scope of a variable is where the variable is available


Lifetime of a variable is how long the value persists.
 Compiler directives such as define and include do not end
with a semicolon.
 main() function is where the program execution begins.
 Only main() function section is the mandatory section of a
C program.
 C is a free form language and therefore proper
programming styles like indentation should be used.
 Comments help the programmer to develop code.
 /* */ is used as multiline comment.
 // is used as single line comment.
 Use meaningful and intelligent variable names.
 All variables must be declared with their data types before
they are used in the program
 Only the first 31 characters are recognized by the compiler.
 Use single quotes ‘ for character constants.
 Use double quotes “ for string constants.
 All variables must be declared with their data types before
they are used in the program.
Define the following C tokens
 Constant Identifier Keywords
 Mention the different classes of data types in C.
 What are variables in C ? Distinguish between
variable declaration and variable definition.
 Explain the fundamental data types in C
Explain the following
 Data type qualifier Data type Modifier
 Symbolic constant Pre processor statements
 What are Storage classes in C ?
Distinguish between auto, static, extern and register

You might also like