Refer Author: E.
Balagurusamy and
Yashwant Kanitkar book for C Programming.
C By Dr. Deepika Bhatia 1
Steps in learning C language
Character
Set Constants
(Alphabets,
Digits, Variables Instruction Programs
Keywords
Special
Symbol)
C By Dr. Deepika Bhatia 2
The C Character Set
Note : White spaces may be used to separate words, but are prohibited between the
characters of keywords and identifiersC By Dr. Deepika Bhatia 3
C Tokens
The words formed from the character set are building
blocks of C and are sometimes known as tokens.
These tokens represent the individual entity of
language.
Types
• Keywords
• Identifiers
• Constants
• Strings
• Special Symbol
• Operators
C By Dr. Deepika Bhatia 4
C By Dr. Deepika Bhatia 5
System defined words. Keywords
Reserved words of the programming language.
Specific meaning in the language
Cannot be used as variable orconstant names.
Must be written in lower case
ANSI C (C89) has 32 Keywords in C language
auto double int struct
break else long switch
case enum register typedef
char extern return union
const float short unsigned
continue for signed void
default goto sizeof volatile
do if static
C By Dr. Deepika Bhatia
while 6
Identifiers
Identifier is nothing but a label or a name given
to a variables, constant, function, array, etc.
User-defined
An identifier consists of letters, digits and
underscore, in any order, except that the first
character cannot be a digit.
C By Dr. Deepika Bhatia 7
Rules for identifiers
Must begin with a letter or an underscore.
Uppercase and lowercase are significant (different) .
Length can be 31 characters, however, first 8 character are
considered significant by most compilers.
It should not be a keyword.
Whitespaces are not allowed.
C By Dr. Deepika Bhatia 8
Identify valid/invalid identifiers
•second
•1hell
•Hell1
•Price$
•Group one
•Co_po
•Average-one
•ONE
C By Dr. Deepika Bhatia 9
Variables
•A variable is a data name which can be used to store a value.
•It is a storage area that our programs can manipulate.
•Each variable in C has a specific type, which determines the size and
layout of the variable's memory; the range of values that can be stored
within that memory.
•The name of a variable can be composed of letters, digits, and the
underscore character. Upper and lowercase letters are distinct because
C is case-sensitive.
•Syntax for variable declaration :
datatype nameofvariable;
EXAMPLE: int salary;
C By Dr. Deepika Bhatia 10
C By Dr. Deepika Bhatia 11
Rules for Defining Variables:
C By Dr. Deepika Bhatia 12
• Declaring variables: • Initializing Variables:
The declaration of variables should be done in • Variables declared can be assigned or initialized
declaration part of the program. The variable must using assignment operator ‘=’. The declaration
be declared before they are used in the program. and initialization can also be done in the same
line.
Declaration ensures the following two things: (i)
compiler obtains the variable name and (ii) it tells • Syntax:
to the compiler data type of the variable being variable_name = constant;
declared and helps in allocating the memory. or data_type variable_name= constant;
The variable can also be declared before main()
such variables are called external variables.
Example:
Syntax: of declaring a variable is as follows.
Data_type variable_name; x=5; where x is an integer variable
Example: int y=5;
int age; int x,y,z; /*declaration of variables */
char m;
float s;
double k;
int a,b,c;
The int, char, float and double are keywords to
represent data types. Commas separate the
variables, in case variables are more than one. C By Dr. Deepika Bhatia 13
Constant
• A constant is a value or an identifier whose value cannot be altered in
a program. For example: 1, 2.5,
• As mentioned, an identifier also can be defined as a constant. eg.
const double PI =3.14
• Here, PI is a constant. Basically what it means is that, PI and
3.14 is same for this program.
C By Dr. Deepika Bhatia 14
Type of constant
Integer
Real
primary
Character
String
constant
Array
secondary Pointer
Structure
etc
C By Dr. Deepika Bhatia 15
Integer Constant
• An integer constant is a numeric constant (associated with
number) without any fractional or exponential part.
• There are three types of integer constants
• Decimal consist of a set of digits 0 through 9, preceded by an
optional – or + sign. comma, blanks and non-digit character are not
allowed between integer.
• Octal consist of any combination of digits from 0 to7 with a leading
0
• Hexadecimal consist of a set of 0 to9 and A to F ( 10 to 15) with a
leading 0x or 0X
Valid : a)426 b)-765 c)037 d)0x2 e)0xF
Invalid: a)4,267 b) $1000 c) 13 435
C By Dr. Deepika Bhatia 16
Real Constant
A floating point constant is a numeric constant that has either a
fractional form or an exponent form.
For example: 2.0,0.0000234,-0.22E-5
Rules for fractional form
1) Must have at least one digit.
2) Must have decimal point
3) Either positive or negative, default positive
4) No comma or blanks spaces allowed
C By Dr. Deepika Bhatia 17
Real Constant
Exponential form used to represent too small or too large.
Represented in two parts. The part appearing e is called mantissa
whereas the part following ‘e’ is called exponent. Thus 0.000342 can be
represented in exponential form 3.42e-4.
Rules for exponential form
a) The mantissa part and the exponential part should be
separated by a letter e or E.
b) Mantissa part positive or negative
c) Default positive
d) Exponent must have one digit C By Dr. Deepika Bhatia 18
Character and String Constant
Character constants
A character constant is a constant which uses single quotation
around characters. For example: 'a', 'l', 'm', 'F'
String constants
String constants are the constants which are enclosed in a
pair of double-quote marks. For example: "good" ,"x", "Earth
is round\n"
C By Dr. Deepika Bhatia 19
C By Dr. Deepika Bhatia 20
Escape Sequence
Sometimes, it is necessary to use characters which cannot be typed
or has special meaning in C programming. For example:
newline(enter), tab, question mark etc. In order to use these
characters, escape sequence is used.
For example: \n is used for newline. The backslash ( \ ) causes
"escape" from the normal way the characters are interpreted by the
compiler.
Escape Sequences Character
\b Backspace \f Form feed
\n Newline \r Return
\t Horizontaltab \v Vertical tab
\\ Backslash \’ Single quotationmark
\" Double quotationmark \? Question mark
\0 Null character
C By Dr. Deepika Bhatia 21
• Form feed: \f it moves the cursor to the first position of the next page.
• New line: \n move the cursor to the first position of the next line.
• Backspace: \b it will move the cursor by one step back in destructive mode.
You can say that it moves the cursor back to one position.
• Carriage return: \r move the cursor to the first position of the current line.
• Horizontal tab: \t to move the cursor to the next horizontal tabular position.
• Similarly vertical tab \v
• Backslash: \\ to produce a single backslash.
• Octal Number
• Octal number escape sequence in C is written by a combination of a backslash
followed by nnn ( \nnn ).
• Hexadecimal
• The hexadecimal escape sequence is written by a combination of a backslash
followed by xhh ( \xhh ).
• Null
• A null escape sequence is written by a combination of a backslash followed by 0 (
\0 ). It produces a null character.
C By Dr. Deepika Bhatia 22
#include <stdio.h>
int main()
{
// Here we are using \n, which
// is a new line character.
printf("Hello\n");
printf(“How are you?");
printf("Hello \t DEEP"); //Hello DEEP
printf("\' Hello Students\n"); // ‘Hello Students
printf("\" Hello Students"); // “ Hello Students
return (0);
} C By Dr. Deepika Bhatia 23
Data types
▪ Primary/fundamental
▪ Derived
▪ User defined
• Data types tell about 2 things :
1. type of value stored,
2. How much memory storage requirement needed
to store
C By Dr. Deepika Bhatia 24
1. Primitive Data Types
a) int (short/int/long- 2/2 or 4 /4 BYTES ----signed/unsigned)
b) char (signed/unsigned)- 1BYTE
c)float (float,double,long double)
4, 8, 10 BYTES)
a)void
Example:
short x; //use %d, by default
unsigned unsigned short x; //use %u
2.Derived Data Types
a) Arrays b)Strings Data types
3.User Defined Data Types
a)Structure b) Enumeration
c)Union d) typedef
Short/long/unsigned and signed are known as type modifiers which change the basic
data type and produces a new data type. These are applied on int data type only. Signed
C By Dr. Deepika Bhatia
and unsigned can also be applied on char data type. Long on double too.25
C By Dr. Deepika Bhatia 26
Primary/Fundamental Data types
Data type Memory Range Format
requirement specifier
int 2 byte -32768 to 32767 %d
char 1 byte -128 to 127 %c
float 4 byte 3.4E-38 to 3.4 E+38 %f
double 8 byte 1.7E-308 to 1.7 %lf
E +308
C By Dr. Deepika Bhatia 27
C By Dr. Deepika Bhatia 28
int b = -9;
// U or u is Used for Unsigned int in C.
int c = 89U;
// L or l is used for long int in C.
long int d = 99998L;
printf("Integer value with an unsigned int data: %u\n", c);
printf("Integer value with an long int data: %ld", d);
double z = 2312312312.123123;
printf("%lf\n", z);
int x=sizeof(double); //sizeof operator in C
printf("The size of data type : %d\n",x);
C By Dr. Deepika Bhatia 29
short a;
long b;
long long c;
long double d;
// valid codes
unsigned int x = 35;
int y = -35; // signed int
int z = 36; // signed int
// invalid code: unsigned int cannot hold negative integers
unsigned int num = -35;
• According to C99 standard, long long is an integer type which is at least
64-bit wide. There are two integer 64-bit types specified: long long int and
unsigned long long int So, yes, this is the biggest integer type specified by
C language standard (C99 version).
C By Dr. Deepika Bhatia 30
Type Conversion in C
• Type conversion in C is defined as if we assign any data type to
another data type and then call it “Type conversion”.
• Any programming language with lower data type values can
automatically be cast into upper data type values.
• In this situation, there is no loss of data, whereas in the case of
upper data type value into lower data type value there may
chance of loss of data.
• Lower data type to upper data type can automatically be done
by C compiler but upper data type to lower data typecasting,
we must need explicit type conversion. Which is known as
“Explicit conversion”. Let’s take an example of long value into
int value is an explicit typecasting.
C By Dr. Deepika Bhatia 31
C By Dr. Deepika Bhatia 32
Smaller data type to bigger
data type conversion is Implicit type
said to be “Implicit type conversion
conversion “. This is
automatically done by the
C compiler. There is no
loss of data.
#include <stdio.h>
int main()
{
int number = 34.78;
printf("%d", number);
return 0;
}
// Output: 34
C By Dr. Deepika Bhatia 33
Explicit typecasting example: (generarlly-bigger data type to smaller
data type conversion is said to be “Explicit type conversion”. This is not
automatically done by the C compiler. There may be a loss of data. This must
be done by the developer explicitly.
int sum=17,count=5;
double mean;
mean=(double) sum/count;
//here cast operator has precedence over division, so the value of sum is first
converted into type double and finally divided by count yielding double
value.
double x = 214.14;
float y=222.22;
// explicit conversion of double into integer
int Q=(int)x;
// explicit conversion of double into float
float R = (float)x;
C By Dr. Deepika Bhatia 34
Rules to Write a C program
1. C program need main() function where program execution begins.
2. All variables must be declared for their types before their use inside the
program
3. Each instruction in a C program is written separately.
4. Code is executed in sequence unless a deliberately jump
is given
3. Insert blank space between two words to improve readability
4. All statement entered in small case. Use uppercase for symbolic names
and output strings.
5. Each statement end with semicolon
6. C has no specific position from where statement has to start. Therefore
it is known as free-form langauage.
C By Dr. Deepika Bhatia 35
Executing a C program
C By Dr. Deepika Bhatia 36
C By Dr. Deepika Bhatia 37
1. Creating the Program: (same for any operating system)
If the file existed before, it is loaded else it is created new. This program is known as source
program.
2,3 Compiling and linking: Let us assume that we created a file named ebg1.c
4. Executing the Program:
C By Dr. Deepika Bhatia 38
Thankyou!
C By Dr. Deepika Bhatia 39