UNIT-2
BASICS OF C PROGRAM
2.1 Variables ,Declaration of variables
A variable is a name of the memory location. It is used
to store data. Its value can be changed, and it can be
reused many times.
It is a way to represent memory location through
symbol so that it can be easily identified.
The name of a variable can be composed of letters,
digits, and the underscore character. It must begin with
either a letter or an underscore. Upper and lowercase
letters are distinct because C is case-sensitive
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.the compiler needs
actual variable definition at the time of linking the
program.
A variable declaration is useful when you are using
multiple files and you define your variable in one of the
files which will be available at the time of linking of the
program. You will use the keyword extern to declare a
variable at any place. Though you can declare a variable
multiple times in your C program, it can be defined only
once in a file, a function, or a block of code.
Rules for defining variables:-
1.A variable can have alphabets, digits, and underscore.
2.A variable name can start with the alphabet, and
underscore only. It can't start with a digit.
3.No whitespace is allowed within the variable name.
4.A variable name must not be any reserved word or
keyword, e.g. int, float, etc.
Valid variable names:
int a;
int _ab;
int a30;
Variable Declaration example:--
A typical variable declaration is of the form:
type variable_name; or
for multiple variables: type variable1_name, variable2_name,
variable3_name;
A variable name can consist of alphabets (both upper and lower case),
numbers and the underscore „_‟ character.
The example of declaring the variable is given below:
int a; float b; char c;
Here, a, b, c are variables. The int, float, char are the data types.
We can also provide values while declaring the variables as given
below:
int a=10,b=20;//declaring 2 variable of integer type
float f=20.8;
char c='A';
2.2 C Identifier
Identifier refers to name given to entities such as
variables, functions, structures etc.
Identifiers must be unique. They are created to give
a unique name to an entity to identify it during the
execution of the program.
For example:--int money; double accountBalance;
Here, money and accountBalance are identifiers.
Also remember, identifier names must be different
from keywords. You cannot use int as an identifier
because int is a keyword.
2.3 C-Keywords
In C, we have 32 keywords, which have their
predefined meaning and cannot be used as a
variable name. These words are also known as
“reserved words”. It is good practice to avoid using
these keywords as variable name. Keywords are
predefined, reserved words used in programming
that have special meanings to the compiler.
Following are the keywords:-
2.4 Data Types And Qualifiers
Data types specify how we enter data into our
programs and what type of data we enter. C
language has some predefined set of data types to
handle various kinds of data that we can use in our
program. These datatypes have different storage
capacities. A data type specifies the type of data
that a variable can store such as integer, floating,
character, etc.
Types Data Types
Basic Data Type int, char, float, double
Derived Data Type array, pointer, structure, union
Enumeration Data Type enum
Void Data Type void
Basic Data Types:-
The memory size of the basic data types may
change according to 32 or 64-bit operating
system.
Let's see the basic data types. Its size is
given according to 32-bit architecture.
Data Types Memory Size Value Range
char 1 byte −128 to 127
signed char 1 byte −128 to 127
unsigned char 1 byte 0 to 255
int 2 byte −32,768 to 32,767
signed int 2 byte −32,768 to 32,767
unsigned int 2 byte 0 to 65,535
short 2 byte −32,768 to 32,767
long int 4 byte -2,147,483,648 to
2,147,483,647
unsigned long int 4 byte 0 to 4,294,967,295
float 4 byte
double 8 byte
long double 10 byte
2.5 Qualifiers
The Qualifiers are the keywords which are applied to the data types
or type modifiers in C.
A qualifier applied to basic data types to alter or modify its sign or
size.
There are three types of qualifiers namely, Size Qualifiers (short, long)
and Sign Qualifiers (signed,unsigned) and the Type qualifiers.
Qualifiers are words or phrases that are added to another word to
modify its meaning, either by limiting it or by enhancing it .
In C, qualifier is a keyword that is applied to data types .
Like for example, int num represents a variable num having integer
data type but if we write const int num then it represents a variable
num which is of type constant integer ,here const is qualifier.
There are no.of qualifers such as short, long, signed,
unsigned can be applied to these primary data types.
There are 2 types of qualifier in C:-
Size qualifier—short,long
Sign qualifier—signed,unsigned
Data type Qualifiers
char signed,unsigned
int short,long,signed,unsigned
float No qualifier
double long
void No qualifier
2.6 Constants and Types of constants
There are 4 basic types of constants. A constant is a
value or variable that can't be changed in the program,
for example: 10, 20, 'a', 3.4, "c programming" etc.
There are different types of constants in C programming.
1.Integer constant 2.floating point constant 3.character
constants 4.string constant
C Constants is the most fundamental and essential part of
the C programming language. Constants in C are the
fixed values that are used in a program, and its value
remains the same during the entire execution of the
program.
Constants are also called literals.
Constants can be any of the data types.
Integer constants :- Integer constants do not have
decimal point (.). They can be written using decimal
numbers (base 10), octal numbers(base 8) and
hexadecimal (base 16).
It contains digits between 0 and 9, but should not
begin with a zero.
Eg:-43, 199, 3452, -100
Floating point or real constants:-
Numeric constants having decimal point are called
floating point or real constants. Floating-point
constants can be written in two forms:
Fractional forms
Exponential form or Scientific notation
Here are the rules for creating floating point constants
in Fractional form:
1.Must have one at least one digit
2.Must have a decimal point
3.Can be positive or negative, the default is positive
4.No comma, blanks, or any other symbols are allowed
Here are some examples:--3.14 ,899.0 ,-0.999
Exponential form is used in cases when a number is too
small or too large. For example, 0.00000941 can be
represented as 9.41e-6. The part of the number
before e is called mantissa i.e 9.41, whereas, the part
following e is called the exponent i.e -6.
Character Constants:-A Character constant is a single
alphabet, digit or any special symbol enclosed using
single quotes. Here are some examples:
'A', 'c', '4', '$', '^'
Note: Character constant must be always enclosed in
single quotes
Every character constant has a unique integer associated
with it. An ASCII table represents the decimal number
used to represent every known character in the English
Language.
String Constant:--String constants consist of zero or
more characters enclosed in double quotes ("").
At the end of the string, the null character i.e '\0' is
automatically placed by the compiler. Here are some
examples of string constants:
"hello"
"123“
"" // This is empty string it consists of only one
character '\0' which is added by the compiler
automatically.
2.7 Comments
Comments in C language are used to provide
information about lines of code. It is widely used for
documenting code. It makes a program more readable
and error finding become easier. Comments are
statements that are not executed by the compiler and
interpreter.
There are 2 types of comments in the C language.
Single Line Comments-:Single line comments are
represented by double slash \\.
Multi-Line Comments-:Multi-Line comments are
represented by slash asterisk \* ... *\. It can occupy
many lines of code, but it can't be nested.
2.8 Input Output Statements(Standard &
Formated)
Input and Output statement are used to read and write the
data in C programming. These are embedded in stdio.h
(standard Input/Output header file). There are mainly two
of Input/Output functions are used for this purpose.
Unformatted I/O functions :-- There are mainly six
unformatted I/O functions discussed as follows:
a) getchar()
b) putchar()
c) gets()
d) puts()
e) getch()
f) getche()
a) getchar():-- This function is an Input function. It is used for
reading a single character from the keyborad. It is buffered
function. Buffered functions get the input from the keyboard and
store it in the memory buffer temporally until you press the Enter
key.
The general syntax is as:
v = getchar();
where v is the variable of character type. For example:--
char n;
n = getchar();
b) putchar():--This function is an output function. It is used to
display a single character on the screen. The general syntax is as:
putchar(v);
where v is the variable of character type. For example:
char n;
putchar(n);
c) gets():--This function is an input function. It is used to read a
string from the keyboar. It is also buffered function. It will read a
string, when you type the string from the keyboard and press the
Enter key from the keyboard. It will mark nulll character ('\0') in the
memory at the end of the string, when you press the enter key. The
general syntax is as:
gets(v);
where v is the variable of character type. For example:
char n[20];
gets(n);
d) puts():--This is an output function. It is used to display a string
inputted by gets() function. It is also used to display an text
(message) on the screen for program simplicity. This function
appends a newline ("\n") character to the output.The general
syntax is as:
puts(v);
or
puts("text line");
where v is the variable of character type.
e) getch()--This is also an input function. This is used to read a
single character from the keyboard like getchar() function. But
getchar() function is a buffered is function, getchar() function is a
non-buffered function. The character data read by this function is
directly assign to a variable rather it goes to the memory buffer,
the character data directly assign to a variable without the need
to press the Enter key.
Another use of this function is to maintain the output on the screen
till you have not press the Enter Key. The general syntax is as:
v = getch();
where v is the variable of character type.
f) getche():--All are same as getch(0 function execpt it is an
echoed function. It means when you type the character data from
the keyboard it will visible on the screen. The general syntax is
as:
v = getche();
where v is the variable of character type.