in C
(Programming for Problem Solving BTPS-
101-18
Prepared By
Dr. Dinesh Gupta
Assistant Professor
Department of Computer Science & Engineering
IKG Punjab Technical University, Kapurthala
C Character Set
Character set refers to all legal characters that are allowed in C
programming language construct. These characters can be used to form
variables, expressions, statements and in C character set is classified into
4 categories:
• Letters: a to z and A to Z, means all English alphabets in both upper and
lower case.
• Digits: All the decimals digits from 0 to 9.
• White spaces: these refers to the blank area of spaces that are use to
divide words or tokens in a C program. White spaces can be given either
by using space bar, tabs, new line or comments.
• Special characters: These include special characters with certain
predefined purposes. Some of the examples of special characters in
form of operators and punctuators are as follows
Tokens
A token is the smallest element or unit in a C program that is meaningful to the
compiler. Compiler identifies various tokens that are part of the program during
its lexical analysis phase. tokens are separated with the help of any of the
following known as “White Space” like Blanks, Horizontal or vertical tabs, New
lines or Comments.
C parser recognizes following category of tokens:
• Identifiers
• Keywords
• Literals
• Operators
• Punctuators
Identifiers
Identifiers are names assigned to any user-defined object or variable. Identifiers
often referred to as user-defined elements are different from the keywords
used in C language. You need to take care of certain naming conventions
before you name any variable, function or constant.
• Name must start with an alphabet or under score.
• Name can consist of alphabets, under score and numeric digits.
• Name cannot be given separated by blank spaces.
• Must be less than 31 characters.
• No keywords can be used as identifier name.
• Naming identifier is case sensitive.
Some of the examples of valid identifiers: name, obj1, n_1, Name_student etc.
Some of the examples of invalid identifiers: 1name, n.g, int, float
Keywords
Keywords are reserved words of any programming language that have
some special pre-defined meaning understood by the compiler.
Keywords cannot be used for naming user-defined functions, class or
variables. Following is the list of keywords available in C.
auto const double float int short struct
unsigned break continue else for long signed
switch void case default enum goto register
sizeof typedef volatile char do extern if
return static union while
Operators
Operators are certain special symbols that are used to perform some
operations in C.
For example,
c=a+b;
here ‘a’, ‘b’ and ‘c’ are operands. Here we have used two operators.
First one is ‘+’ operator that is used to add the value of identifiers ‘a’
and ‘b’. second one is ‘=’ operators that is used to assign the value of
addition to identifier ‘c’.
Literals/ Symbolic Constants
• Literal is a constant value. The value assigned in a literal is never
changed during a program. In C there are 4 kinds of literals used and
they are:
• String literal: "abhi" is a string literal.
• Integer constant : Integer literal is a whole number like 20.
• Floating constant: Float literal is a number like 23.8.
• Character constant: They are simply single character values given in
single quotes, like ‘a’, ‘b’ etc. Character constants can also be escape
sequences that are special character literals having predefined
meaning that are followed by ‘\’. For instance, \a, \b,\n, \r, \t, etc.
Symbolic Constant
A symbolic constant is a name that substitutes for a sequence of character
that cannot be changed. The character may represent a numeric constant, a
character constant, or a string. When you wish to compile a program, source
file is pre-compiled and each occurrence of a symbolic constant is replaced by
its corresponding character sequence. They are usually defined at the
beginning of the program using #define preprocessor.
For example,
#define num 15
During pre-compilation, every occurence of num will be replaced by 15 in the
source code.
Punctuators
Punctuators are also called as separators and they are used to separate
two identifiers two units of data. They are used to mark the beginning
and end of programming construct. It is also used to separate a line of
codes. Punctuator symbol Name Explanation
, Comma Variable separator, like int a, b
; Semicolon Used for terminating a statement, like a=b;
: Colon Used for defining labels of goto statements and used in
syntax of switch statement. Both are defined in control
statements chapter.
() Parenthesis Used in expressions, like (a+b) +c
[] Square brackets For declaration of arrays, like int a[12]
{} Curly braces For defining compound statements
Data Types in C
Range and Description of Various
Data types
Type name Description Bytes Range of Values
int short Contains both +ve and 2 –32,768 to 32,767
int signed int –ve whole numbers
unsigned int unsigned short Contains only +ve whole numbers 2 0 to 65535
int
long int signed long int Contains both +ve and 4 –2,147,483,648 to 2,147,483,647
–ve whole numbers
unsigned long int Contains only +ve whole numbers 4 0 to 4294967925
char signed char Character or small integer 1 -128 to 127
unsigned char Character with +ve values only 1 0 to 255
Float Contains both +ve and –ve real 4 3.4E +/- 38 (7 digits)
numbers
double Contains both +ve and –ve real 8 1.7E +/- 308 (15 digits)
numbers
long double Contains both +ve and –ve real 10 3.4 X 10-4932 to 1.1 X 10+4932
numbers
Void
Void means nothing or empty. Declaring a variable with void data type
means it does not correspond to any data type. Because it means no
data type, we cannot declare an ordinary variable with void data type.
Void data type is used with pointers or functions.
Derived Data type
Array is a collection of similar or homogeneous data elements that
belong to same data type. It is a set of sequentially indexed elements
starting from 0 having same data type. All elements share the name
identifier and can be accessed individually using their unique index
number.
Function can be thought of as a block consisting of compound statements
to perform a specific operation. Function can be of two types, either
library or user-defined functions.
Pointer is a special variable that can be used to hold the address of
another variable, pointer or a function. Pointer is the most important
concept in C.
User-Defined Data types
Structure can be created using struct keyword. Structure is quite similar
to an array except the fact that structure is a collection of variables of
heterogeneous or different data types.
Union can be created using keyword union. Union is quite similar to
structure except the fact that members or data elements of union share
the same memory space.
ENUM stands for enumerated data type. Members of enum data type are
constants and written as identifiers that can have signed integer values.
Type Specifiers & Quantifiers
Type Specifiers
• short
• long
• signed
• unsigned
Type Quantifiers
• const
• volatile
Variables
An entity whose value may vary during the execution of a program is
called a variable. Variable names are identifiers that provide reference
to locations in memory. These locations can contain integer, real or
character constants.
Errors in C
• In C programming language, we face different kinds of errors. These
errors can be categorized into five different types. These are like
below −
• Syntax Error
• Run-Time Error
• Linker Error
• Logical Error
• Semantic Error
Syntax Error
This kind of error occurs, when you violate the rule of C++ syntax constructs.
This kind of errors are generally indicated by the compiler before compilation.
Example
#include<stdio.h>
main() {
printf("Hello World")
}
Output
Error] expected ';' before '}' token
Runtime Error
This kind of error occurs during the execution of the program.
Example
#include<stdio.h>
main()
{
int x = 52;
int y = 0;
printf("Div : %f", x/y);
}
Output
Program crashes during runtime.
Linker Error
This kind of error occurs when the program is compiled successfully,
and trying to link the different object file with the main object file. The
compiler fails to generate the executable file for the program.
Logical Error
Sometimes, we fail to get the desired output. If the syntax and other things are correct, then also, we
may not get correct output due to some logical issues. These are called the logical error.
Example
#include<stdio.h>
main()
{
int i;
for(i = 0; i<5; i++) ;
{
printf("Hello World");
}
}
Semantic Error
This kind of error occurs when it is syntactically correct but has no meaning or placed
incorrectly. This is like grammatical mistakes. If some expression is given at the left
side of assignment operator, this may generate semantic error.
Example
#include<stdio.h>
main() {
int x, y, z;
x = 10;
y = 20;
x + y = z;
}
Thank You