About
C
C is a programming language developed at AT&T’s
Bell Laboratories of USA in 1972. It was designed and written by a man named
Dennis Ritchie. In the late seventies C began to replace the more familiar
languages of that time like PL/I, ALGOL etc.
History
of C
By 1960 a hoard of computer languages had come into
existence, almost each for specific purpose. For example, COBOL was being used
for Commercial Applications, FORTRAN for Engineering and Scientific
Applications and so on. At this stage people started thinking that instead of
learning and using so many languages, each for a different purpose, why not use
only one language which can program all possible applications. Therefore, an
international committee came out with a language called ALGOL 60. However,
ALGOL 60 never really became popular because it is too abstract, too general.
To reduce this new language called Combined Programming Language (CPL) was
developed at Cambridge University. However, CPL turned out to be so big, having
so many features, that it was hard to learn and difficult to implement.
Basic Combined Programming Language(BCPL), developed
by Martin Richards at Cambridge University aimed to solve this problem by
bringing CPL down to its basic good features. Around the same time a language
called B was written by Ken Thompson at AT&T’s Bell Labs. Ritchie inherited
the features of B and BCPL, added some of his own and developed C. Ritchie’s
main achievement is the restoration of the lost generality in BCPL and B, and
still keeping powerful.
C
Stand between……
Problem oriented languages or High level languages:
These languages have been designed to give a better
programming efficiency, i.e. faster program development. Examples are FORTRAN,
BASIC, PASCAL.
Machine oriented languages or Low level languages:
These languages have been designed to give a better
machine efficiency, i.e faster program execution. Examples are Assembly
languages and Machine language.
C stands in between these two categories. That’s why
it is often called as a middle level language, since it was designed to have
both a relatively good programming efficiency (as compared to Machine oriented
language) and relatively good machine efficiency (as compared to Problem
oriented languages).
Getting
Started with C
The
C Character Set
A character denotes any alphabet, digit or special symbol
used to represent information. Following table shows the valid alphabets,
numbers and special symbols allowed in C:
Constants,
Variables and Keywords
The alphabets, numbers and special symbols when
properly combined form constants, variables and keywords. A constant is a
quantity that doesn’t change.
A variable can be considered as a name given to the
location in memory where this constant is stored. Naturally the constant of the
variable can change.
Types
of C Constants
Rules
for Constructing Integer Constants
a)
An integer
constant must have at least one digit.
b)
It must not have
a decimal point.
c)
It could be
either positive or negative.
d)
If no sign
precedes an integer constant it is assumed to be positive.
e)
No comments or blanks
are allowed within an integer constant.
f)
The allowable
range for integer constant is -32768 to +32767.
Ex.: 426
+782
-8000
-7605
Rules
for Constructing Real Constants
Real constants are often called Floating Point
constants. The real constants could be written in two forms, Fractional form
and Exponential form.
a)
A real constant
must have atleast one digit.
b)
It must have a
decimal point.
c)
It could be
either positive or negative.
d)
Default sign is
positive.
e)
No commas or
blanks are allowed within a real constant.
Ex.:
+325.34
426.0
-32.76
-48.5792
Rules
for Constructing Constants
a)
A character constant
is either a single alphabet, a single digit or a single special symbol enclosed
within single inverted commas Both the inverted commas should point to the
left. For example, ‘A’ is a valid character constant.
b)
The maximum
length of a character constant can be 1 character.
Ex.:
‘A’
‘I’
‘5’
‘=’
Types
of C Variables
In C, a quantity which may vary during program
execution is called a variable. Variable names are names given to locations in
memory of computer where different constants are stored. These locations can
contain integer, real or character constants.
Rules
for Constructing Variable Names
a)
A variable name
is any combination of 1 to 8 alphabets, digits or underscores. Some compilers
allow variable names whose length could be up to 40 characters. Still, it would
be safer to stick to the rule of 8 characters.
b)
The first character
in the variable name must be an alphabet.
c)
No commas or
blanks are allowed within a variable name.
d)
No special symbol
other than an underscore (as in gross_sal) can be used in variable name.
Ex.:
si_int
M_hra
Pop_e_89
C
Keywords
Keywords are the words whose meaning has already been
explained to the C compiler (or in a broad sense to the computer). The keywords
cannot be used as variable names because if we do so we are trying to assign a
new meaning to the key word, which is not allowed by the computer. There are 32
keywords available in C.
C
Instructions
There are four types of instructions in
C
A)
Type Declaration
Instruction
B)
Input/Output
Instruction
C)
Arithmetic
Instruction
D)
Control
Instruction
Type
Declaration Instruction
This instruction is used to declare the type of
variables being used in the program. Any variable used in the program must be
declared before using it in any statement. The type declaration statement is
usually written at the beginning of the C program.
Ex.: int
bas;
Float rs, grosssal;
Char
name, code;
Arithmetic Instructions
C Arithmetic instructions consists of a variable name
on the left hand side of = and variable name & constants on the right hand
side of =.
The variables and constants appearing on the right
hand side of = are connected by arithmetic operators like +, -, *, /.
Ex.: int
ad;
Float
kot, deta, alpha, beta, gamma;
ad=3200;
kot=0.0056;
deta=alpha*beta/gamma+3.2*2/5;
Here
*,
/, -, + are the arithmetic operators.
=
is the assignment operator.
2,5
and 3200 are ineger constants.
3.2
and 0.0056 are real constants.
ad
is an integer variable.
kot,
deta, alpha, beta, gamma are real variables.
First
C Program
/*
Caliculation of simple interest */
Main()
{
int p, n;
float r,si;
p=1000;
n=3;
r=8.5;
si=p*n*r/100;
printf(“%f”,si);
}
The printf Function
1.
printf
displays information on screen.
2.
printf
returns the number of characters printed.
3.
printf
displays the text you put inside the double quotes.
4.
printf
requires the backslash character - an escape sequence - to display some special
characters.
5.
printf
can display variables by using the % conversion character.
6.
printf
format: a string argument followed by any additional arguments.
#include <stdio.h>
main() { int i = 0; i=printf("abcde\n"); printf("total characters printed %d\n",i); }
Output:
|
abcde
total characters printed 6
The printf() Conversion Characters
and flags
Conversion Character
|
Displays Argument (Variable's
Contents) As
|
%c
|
Single character
|
%d
|
Signed decimal integer (int)
|
%e
|
Signed floating-point value in E
notation
|
%f
|
Signed floating-point value (float)
|
%g
|
Signed value in %e or %f format,
whichever is shorter
|
%i
|
Signed decimal integer (int)
|
%o
|
Unsigned octal (base 8) integer (int)
|
%s
|
String of text
|
%u
|
Unsigned decimal integer (int)
|
%x
|
Unsigned hexadecimal (base 16)
integer (int)
|
scanf function
The scanf function: read information
from a standard input device (keyboard).
scanf("conversion specifier", variable);
|
The conversion specifier argument tells
scanf how to convert the incoming data.
1. scanf starts with a string argument and may contain
additional arguments.
2. Additional arguments must be pointers.
3. scanf returns the number of successful inputs.
Common Conversion Specifiers Used with
Scanf
Conversion Specifier
|
Description
|
%d
|
Receives integer value
|
%f
|
Receives floating-point numbers
|
%c
|
Receives character
|
#include <stdio.h>
main() { int i = 0; int k,j=10; printf("Input three integers and press enter to confirm."); i=scanf("%d%d%d",&j,&k,&i); printf("total values inputted %d\n",i); printf("The input values %d %d\n",j,k); }
Output:
|
Input
three integers and press enter to confirm.1 2 3
total
values inputted 3
The
input values 1 2
No comments:
Post a Comment