These languages are classified into 2 types.
They are 1).Low level language.
2).High level language.
1).Low level language:
Machine language is a collection of 0’s & 1’s which is known as
binary Language. The languages, in which the instructions are written
in machine language comes under low level language.
**these are best suited for writing system programs**
\
They are divided into 2 types.
i).Machine level language.
ii).Assembly level language.
Advantages:
i).Instructions of this language program are immediately executable
because computer can understand these instructions directly & since
no compilation & translation steps are required. Hence the program
executes faster.
ii).complexity of the program decreases.
Disadvantages:
i). This is not a portable language.
ii).These languages are only machine dependant.
iii).These languages require a high level of programming skills i.e., it
takes much time to write a program.
2).High level language:
A language in which the instructions are written in English like codes to
write a program is known as high level language.
Ex: COBOL, PASCAL, FORTRAN etc.
**these languages are used to write general purpose programs such as
Engineering, scientific etc.**
Advantages:
It is easy to write a program in high level language i.e., a program
can be developed very fast.
Disadvantages:
A high level language program executes slowly because the computer does not understand English. So English coding translated to computer understandable language this translation process takes time so program executes slowly.
INTRODUCTION TO C:
‘C’ instructions are written in English like language which is known as
High level language even though it is known as middle level language because of following reasons.
i). it satisfies the advantages of both low and high level languages.
ii).C can be used to write both system programs as in low level language
and general purpose programs as in high level language.
HISTORY OF ‘C’:
C language was developed at AT&T’s Bell labs (USA) by DENNIS RITCHE in 1972.It was the outcome of two other languages called BCPL
(basic combining programming language) and ‘P’ language.
FEATURES & CHARECTERISTICS OF ‘C’:
1.’C’ is a general purpose structured programming language.
2.’C’ is powerful, efficient, compact, flexible and highly portable.
3.’C’ has the ability to extend itself and we can continuously add our own
functions to the existing ‘C’ library functions.
4.It is robust language whose set of built-in functions and operators can be
used to write any complex program.
5.’C’ program can be run on any different computers with little or no
modifications.
6.It allows reference to a memory location with the help of a pointer which
can holds the address of the memory locations.
BASICS OF ‘C’ LANGUAGE:
‘C’ CHARACTER SET:
The set of elements that can be used to form words, sentences and expressions in a program.
Character set contains
i).Alphabets: a, b, c etc.
ii).Digits: 0-9.
iii).Special symbols: *,: ,:, “, %, & etc.
‘C’ TOKENS:
A ‘C’ program is collection of elements which are known as ‘C’
tokens.
They are
1. constants
2. variables
3. key words(reserved words)
1) Constants:
A constant is a value that does not change it’s value during the entire execution of the program. constants are of 2 types
i).Numeric constants:
a).integer constants:
an integer constant refers to a sequence of digits. These are 3
types of integers namely decimal, octal and hexadecimal.
Ex: a=5, b=4 etc.
b).float constants:
A float variable can hold float constants.
Ex: x=2.44 etc.
ii).Character constants:
a).single character constants:
A character variable can hold one character. There are
enclosed in single quotes.
Ex: ch = ’a’ etc.
b).string constants:
A string constant is a sequence of characters enclosed in a
double quotes.The characters may be letters,numbers,special
characters & blank spaces.
Ex: ch=”srikanth”;
Ch=”santhosh”;
2).Variables:
A variable is a name in which a constant value is stored.
(or)
A variable is a name given ti the memory location where a
constant is stored.
variables are so called because,the contents in the variables can
be modified any where in the program i.e, variables varies from
time to time.
Ex: int a=10;
Float b=20.4;
Rules for variable declaration:
1).variable name must start with an alphabet.
2).variable cannot contain any special characters(#,%,&, *, ”, ;).
3).It should not contain any blank spaces( ) but underscore( _ )can
Allowed.
4).There is no limit to the length of a variable.But the number of
characters varies from compiler to compiler. It is always better to
give a variable name upto 8 characters.
5).The upper case & lower case letters are treated as distinct.
6).Variable name should not be a reserved word.
Ex: int =10; (incorrect).
3).Reserved words:
Key words are words which have predefined meaning and are part of
the C language. Such names should not be used as user-defined
names.
*.They should be written in lower case only.
*.There are 32 key words in “C”.
Ex: char, int , float , break etc.
DATA TYPES:
Data type of a variable determines what kind of data can be stored in
the variable and the amount of memory that should be allocated to
variable.
Data types are divided into 3 types.
1). Primary data type.
2).User-defined data type.
3).Derived data types.
1).PRIMARY DATA TYPES:
Basic data types are the actual data types which are already
defined in C and they should be used in the same way as they are
defined in C.
i).Integer :
Integers are numbers without fractional parts(i.e,decimal points).
* The key word “int” is used to represent integer data type in
the C language.
* It occupies 2 bytes of memory.
* Format specifier is %d.
* Data range is -32,768 to +32,767.
* more than one variable can be declared with single int, separating
variable names by commas.
Ex: int a=10;
Int a=3,b=10;
ii).Float:
A Float is a single precision floating point number.These
numbers have a precision of 6 decimal points.
* The key word Float is used to represent float data type.
* It occupies 4 bytes of memory.
* Format specifier is %f.
* Data range is 3.4 e-38 to 3.4 e+37.
Ex: float a=3.45;
iii).Char:
* character constants are always represent in single quotations and
string constants in double quotations.
*.The char key word is used to represent char data type.
* It occupies 1 byte memory.
* Format specifier is %c for single characters and %s for string
characters.
* Data range is -128 to +127.
Ex: char ch=’s’;
Char str[10]=”srikanth”;
Char str[10]=”santhosh”;
iv).Double:
Double data type is similar to float type.The double is used to
store more than 6 decimal points which a float can not store.double
is a precision number. These numbers have a precision of 16 to 18
decimal points.
* The key word double is used to represent double data type.
* It occupies 8 bytes of memory.
* Format specifier is %lf.
* Data range is 1.7e-308 to 1.7e+308.
Ex: double a=10.123456;
2).USER-DEFINED DATA TYPES:
The user-defined data types enables the programmer to invent his own
data types and define what values it can take on.
These are 2 types. i). Type def (type declaration)
ii). Enum (enumerated data types).
i).Typedef:
The typedef allows user to define new data types that are
equivalent to existing data types. Once a new data type has been
created, then new variables, arrays etc can be declared in terms of
this new data type.
Syntax:
typedef type new-type;
where type refers to an existing data type and new-type refers to
new data-type.
Ex: typedef int sri:
Program:
# include
# include
main()
{
typedef int sri;
sri a=10;
clrscr();
printf(“a=%d”,a);
getch();
}
ii).Enum:
These are used to increase the clarity of the program by providing
identifiers for a class of related objects.
Syntax:
enum
* Here enum is the key word.
* The member’s of the enumerations assigned consecutive constants
starting from 0 i.e, member1 is assigned with 0, member2 assigned
with 1 and so on, these member’s can be freely used any where in
the program.
Ex: enum days{ mon, tues, wed, thurs, fri, sat, sun};
Program:
# include
# include
main()
{
enum colors{red, green, yellow};
clrscr();
printf(“%d\n%d\n%d”,red,green,yellow);
getch();
}
** enumeration variables can be assigned with the enumeration
members of same type.
3).DERIVED DATA TYPES:
Derived data types are built from the basic integer and floating data
types.
Ex; arrays, structures, functions, pointers.
INPUT/OUTPUT FUNCTIONS IN C:
A function is a sub program that performs a specific task or job or operation.
Parenthesis “( )” is the identification of any function.It should be followed by function name.
Ex: printf( ); //correct
( )printf; // incorrect
printf: // incorrect
Input/outputoperations are performed in C using functions.
OUTPUT FUNCTION: printf()
This is output function.It prints the the given data on the monitor.
Syntax:
Printf(“format specifier”,variables list……..);
Format specifiers is also known as format strings which controls the format of the variables to be printed.Each format specifier has to be preceded with % sign.
*Format string specifies two things,first the value of variable have to be
converted into which data type and second, the place where the value is to
be printed.
The following are the various format specifiers.
Format strings Meaning
%d Signed decimal integer
%u Unsigned decimal integer
%c Single character
%s string of character
%f Floating point(decimal notation)
%e Floating point(exponential decimal notation)
%x Unsigned integer in hexadecimal
%o Unsigned integer in octal.
Where the variables list in the syntax specifies variable names whose values to be printed.The above format strings specifies in which format the value of variable to be printed on the monitor.
Ex: 1.int a=10;
Printf(“a:%d”,a); o/p:
a:10
2.int a=10;
Printf(“%d”,a); //correct
Printf(“%c”,a); //runtime error: abnormal program termination
** With single printf( ) multiple variables can be printed.
Ex: int a=10,b=20;
Printf(“a=%db=%d”,a,b); o/p: a=10 b=20
Note:
1.Number of format specifiers should be equal to the number of variables
to be printed.
2.The order in which the format strings are given in the same order the
corresponding variables should be given otherwise we get unusual
answers sometime runtime error.
Ex: int a=10; char c=”s”, float f=2.35;
Printf(“%f%c%d”,a,f,c); // error.
** In the syntax of printf() the format string can contain text.the text that is given in the double quotes |” “| of printf( ) will be printed as ist is on the monitor.
Ex: printf(“WELCOME TO KITS “);
O/P: WELCOME TO KITS
** The format string can also contain any text that has to be printed along with the variables.
Ex; int a=10;
Printf(“ a is:%d”,a); o/p: a is:10
Programs:
1.
# include
# include
main()
{
clrscr();
printf("name:SANTHOSH");
printf("rollno:07016-T-0993");
printf("branch:CSE-II");
getch();
}
INPUT FUNCTION:scanf( )
The function that reads values from the input devices into the program is said to be input function. Input devices may be mouse, key board etc…
Scanf() is an input function that reads from the key board and stores into the variables at runtime(while program is executing).
Syntax:
scanf(“format specifier”,variables list…..);
The format strings can be %d,%c,%f……and variables does not specify variable names themselves. Instead it specifies addresses of variables in the memory, where the variables have been allocated memory, sothat scanf() can directly stores the values that are read from the key board. To specify the address of a variable, it can be done using the address operator (&). This operator, if given before any variable, it gives address of that variable.
Ex: main()
{
int a;
Printf(“enter a value”); o/p: enter a value: 10
Scanf(“%d”,&a); a=10
Printf(“a=%d”,a);
getch();
}
Program:
# include
# include
main()
{
float c,f;
clrscr();
printf("enter centegrade");
scanf("%f",&c);
f=(1.8*c)-32;
printf("cetegrade is:%f",c);
printf("foreignheat is :%f",f);
getch();
}