Unit II
C programming
C Character Set
C language also has a set of characters which include alphabets,
digits, and special symbols. C language supports a total of 256
characters.
Every C program contains statements. These statements are
constructed using words and these words are constructed using
characters from C character set. C language character set contains
the following set of characters...
Alphabets
Digits
Special Symbols
1.Alphabets
C language supports all the alphabets from the English language. Lower and upper
case letters together support 52 alphabets.
lower case letters - a to z
UPPER CASE LETTERS - A to Z
2.Digits
C language supports 10 digits which are used to construct numerical values in C
language.
Digits - 0, 1, 2, 3, 4, 5, 6, 7, 8, 9
3.Special Symbols
C language supports a rich set of special symbols that include symbols to perform
mathematical operations, to check conditions, white spaces, backspaces, and
other special symbols.
Special Symbols - ~ @ # $ % ^ & * ( ) _ - + = { } [ ] ; : ' " / ? . > , < \ | tab
newline space NULL bell backspace verticaltab etc.,
C Keywords
Keywords are the reserved words with predefined
meaning which already known to the compiler
In the C programming language, there are 32
keywords. All the 32 keywords have their meaning
which is already known to the compiler.
Keywords are also known as reserved words in C
programming language.
C data types
Data used in c program is classified into
different types based on its properties. In
the C programming language, a data type
can be defined as a set of values with
similar characteristics. All the values in a
data type have the same properties.
INTEGER DATA TYPE
BASIC DATA TYPES
C programming language also allows
to define various other types of
variables, which we will cover in
subsequent chapters like
Enumeration, Pointer, Array,
Structure, Union, etc. For this
chapter, let us study only basic
variable types.
Variable Definition in C
A variable definition tells the compiler
where and how much storage to create
for the variable. A variable definition
specifies a data type and contains a list
of one or more variables of that type as
follows −
Syntax:
type variable_list;
Some valid declarations are shown here −
int i, j, k;
char c, ch;
float f, salary;
double d;
The line int i, j, k; declares and defines the variables i, j,
and k; which instruct the compiler to create variables
named i, j and k of type int.
Variables can be initialized (assigned an initial value) in
their declaration. The initializer consists of an equal sign
followed by a constant expression as follows −
Syntax: type variable_name = value;
Variable Declaration in C
A variable declaration provides assurance to
the compiler that there exists a variable with
the given type and name so that the compiler
can proceed for further compilation without
requiring the complete detail about the
variable. A variable definition has its meaning
at the time of compilation only, the compiler
needs actual variable definition at the time of
linking the program.
Assigning values to variables
Symbolic Constant is a name that substitutes
for a sequence of characters or a numeric
constant, a character constant or a string
constant.
When program is compiled each occurrence of
a symbolic constant is replaced by its
corresponding character sequence.
The syntax of Symbolic Constants in C
#define name text
where name implies symbolic name in caps.
text implies value or the text.
For example,
#define printf print
#define MAX 50
#define TRUE 1
#define FALSE 0
#define SIZE 15
The # character is used
for preprocessor commands.
C - Operators
An operator is a symbol that tells the compiler to perform
specific mathematical or logical functions. C language is
rich in built-in operators and provides the following types
of operators −
Arithmetic Operators
Relational Operators
Logical Operators
Bitwise Operators
Assignment Operators
Misc Operators
Arithmetic Operators
The following table shows all the arithmetic operators
supported by the C language. Assume variable A holds
10 and variable B holds 20 then −
Relational Operators
The following table shows all the relational operators supported by C. Assume
variable A holds 10 and variable B holds 20 then −
Logical Operators
Following table shows all the logical operators supported by C language.
Assume variable A holds 1 and variable B holds 0, then −
Assignment Operators
Misc Operators ↦ sizeof & ternary
Operators Precedence in C
Operator precedence determines the grouping of
terms in an expression and decides how an
expression is evaluated. Certain operators have
higher precedence than others; for example, the
multiplication operator has a higher precedence than
the addition operator.
For example, x = 7 + 3 * 2; here, x is assigned 13, not
20 because operator * has a higher precedence than
+, so it first gets multiplied with 3*2 and then adds
into 7.
Here, operators with the highest precedence appear
at the top of the table, those with the lowest appear
at the bottom. Within an expression, higher
C - Type Casting
Converting one datatype into another is known as type
casting or, type-conversion.
C' programming provides two types of type casting operations:
Implicit type casting
Explicit type casting
Implicit type casting
Implicit type casting means conversion of data types
without losing its original meaning. This type of
typecasting is essential when you want to change
data types without changing the significance of the
values stored inside the variable.
Implicit type conversion happens automatically when
a value is copied to its compatible data type. During
conversion, strict rules for type conversion are
applied. If the operands are of two different data
types, then an operand having lower data type is
automatically converted into a higher data type
Explicit type casting
In implicit type conversion, the data type is converted
automatically. There are some scenarios in which we
may have to force type conversion. Suppose we have a
variable div that stores the division of two operands
which are declared as an int data type.
int result, var1=10, var2=3; result=var1/var2;
It requires a type casting operator. The general syntax for type
casting operations is as follows:
(type-name) expression
Summary of type casting
Typecasting is also called as type conversion
It means converting one data type into
another.
Converting smaller data type into a larger one
is also called as type promotion.
'C' provides an implicit and explicit way of type
conversion.
Implicit type conversion operates
automatically when the compatible data type
is found.
Mathematical functions
“math.h” header file supports all the mathematical
related functions in C language.
Read and Write Character in C
We can read and write a character on screen using
printf() and scanf() function but this is not applicable in
all situations. In C programming language some function
are available which is directly read a character or number
of character from keyboard.
When we say Input, it means to feed some data into a
program. An input can be given in the form of a file or
from the command line. C programming provides a set of
built-in functions to read the given input and feed it to
the program as per requirement.
When we say Output, it means to display some data on
screen, printer, or in any file. C programming provides a
set of built-in functions to output the data on the
computer screen as well as to save it in text or binary
files.
The getchar() and putchar() Functions
Getchare():
This is a predefined function in C language which is available in
stdio.h header file. Using this function we can read a single
character from keyboard and store in character variable. When we
want to read a number of character form keyboard the store all
the data in a character array.
Example
char ch; ch=getch();
getchar function has no any parameters.
putchar
putchar function is a same as getchar but is function is used for display
a character value of screen or console. This function must be take one
parameters.
Example
char ch='A'; putchar (ch);
putchar is equivalent to printf("%c",ch);
The gets() and puts() Functions
gets
This is a predefined function in C language which is available in
stdio.h header file. This function is used to read a single string
from keyboard.
Example
gets(string);
puts
puts is same as gets function but this is used to display a string
on screen or console. This function takes single arguments.
Example
puts(str);
Formatted input and output.
Formatted Output:
The function printf() is used for formatted output to standard output
based on a format specification. The format specification string, along
with the data to be output, are the parameters to the printf() function.
Syntax:
printf (format, data1, data2,……..);
In this syntax format is the format specification string. This string
contains, for each variable to be output, a specification beginning
with the symbol % followed by a character called the conversion
character.
Example:
printf (“%c”, data1);
The character specified after % is called a
conversion character because it allows one data
type to be converted to another type and
printed.
Formatted Input
The function scanf() is used for formatted input from standard
input and provides many of the conversion facilities of the
function printf().
Syntax
scanf (format, num1, num2,……);
The function scnaf() reads and converts characters from the
standards input depending on the format specification string
and stores the input in memory locations represented by the
other arguments (num1, num2,….).
For Example:
scanf(“ %c %d”,&Name, &Roll No);