Вы находитесь на странице: 1из 35

12/12/2011 Programming With C 1

CLASS 1
C Programming
12/12/2011 Programming With C 2
Programming Language
Classification
A Programming language (PL) is a set
of instructions used to communicate
with the computer
The set of instruction written in a PL
is known as program"
All PL language can be divided into
three categories
12/12/2011 Programming With C 3
Programming Languages
Problem Oriented or High Level
Languages
Nachine Oriented or Low Level
Languages
Niddle Level Language
12/12/2011 Programming With C 4
Language Translators
These are special programs, which
accepts the user program and convert
to corresponding machine language
instructions
There are two types of translators
Compilers
!nterpreters
12/12/2011 Programming With C 5
History of C Language
eveloped at ATST's Bell Labs of
USA in 1372
esigned and written by Nr. ennis
Ritchie
American National Standards
!nstitute (ANS!) standardized the
definition of the C language as ANS!
C in 1388
12/12/2011 Programming With C 6
Structure of C program
GIobaI variabIe and
function decIaration
IncIude fiIes
Function subprogram
Main functions
12/12/2011 Programming With C 7
C Programming An
!ntroduction
A 8MPLE C PROGRAM
The following program is written in the
C programming language
#include<stdio.h>
main( )
{
printf("Programming in C is easy.\n"),
}
Sample Program Output
Programming in C is easy.
12/12/2011 Programming With C 8
A NOTE ABOUT C PROCRANS
C is Case sensitive
The C programs starting point is
identified by the word main( )
The two braces, { and }, signify the
begin and end segments of the
program
12/12/2011 Programming With C 9
A NOTE ABOUT C PROCRANS
#include <stdio.h> is to allow the
use of the printf statement to
provide program output
printf() is actually a function in C
that is used for printing variables
and text
All C statements must be terminated
by a semicolon ( , )
12/12/2011 Programming With C 10
Structure of a C Program
Every C program consists of one or
more functions.
The program will always begin by
executing main function.
Each function must contain
Funct|on head|ng.
0ec|arat|ons.
compound statement.
12/12/2011 Programming With C 11
Comment
Comment should be enclosed
between /* */
!t is used to increase the
readability of the program.
Any number of comments can be
given at any place in the program.
Comment cannot be nested
!t can be split over more than one
line
12/12/2011 Programming With C 12
A sample C Program.
#include<stdio.h> /*include information about
standard library */
/* program to print a message*/
main( ) /* define a function named main that receives no
argument values */
{
printf(Aryans Technologies welcomes you \n"),
/*main calls library function printf to print this sequence of
characters , \n represents the newline character*/
}
12/12/2011 Programming With C 13
Communicating with a computer
involves speaking the language the
computer understands.
Steps in learning English language
Steps in learning C
Iphabets
Digits
SpeciaI-symboIs
Constants
VariabIes
Keywords
Instruction Program
Cetting started with C
Iphabets
Sentences
Paragraph
Words
12/12/2011 Programming With C 14
The C character Set
W character denotes any aIphabet, digit
or speciaI symboI used to represent
information.
Iphabets ,B, .. ,Y, Z
a,b, ... ,y, z
Digits 0,1,2,3,4,5,6,7,8,9
SpeciaI SymboIs ~ ' ! @ # % ^ & * ( ) _ - + =
| \ { } [ ] : ; " ' < > , . ? /
12/12/2011 Programing With CC Programming 15
Constants, variable and
keywords
The alphabets, numbers and special
symbol when properly combined form
constants, variables and keywords
A constant is a quantity that doesn't
change
A variable is a name given to the
location in memory where the
constant is stored
12/12/2011 Programing With CC Programming 16
Types of C Constants
C constants can be divided into two
major categories
Primary Constants
Secondary Constants
C Constants
Primary Constants Secondary constants
Integer Constant
ReaI Constant
Character Constant
Array, Pointer
Structure, Union
Enum
12/12/2011 Programing With CC Programming 17
!nteger Constants
An integer constant must have at
least one digit
!t must not have a decimal point
!t could be either positive or
negative
!f no sign precedes an integer
constant, it is assumed to be
positive
No commas or blanks are allowed
within an integer constant
12/12/2011 Programing With CC Programming 18
Real Constants
Real constants(RC) must have
atleast one digit
!t must have a decimal point
!t could be either positive or
negative
efault sign is positive
No commas or blank are allowed
within a RC
12/12/2011 Programing With CC Programming 19
Real Constants in
exponential form
The mantissa part and exponential part
should be separated by a letter e
The mantissa part may have a positive or
negative sign
efault sign of mantissa and exponent is
positive
The exponent mush have at least one
digit which must be a positive or
negative integer. efault sign is positive
12/12/2011 Programing With CC Programming 20
Character Constants
A character constant is either a
single alphabet, a single digit or a
single special symbol enclosed
within single inverted commas
The maximum length of a character
constant can be 1 character
Eg 'a', '1', 'S', '=' (valid)
'asd', '12' (!nvalid)
12/12/2011 Programing With CC Programming 21
!dentifiers
!dentifiers are names given to
various program elements, such as
variables, functions and arrays
A variable name is any combination
of alphabets, digits or underscores
The first character in the variable
name must be an alphabet
12/12/2011 Programing With CC Programming 22
eywords
W Keywords are the words whose meaning
has aIready been expIained to the C
compiIer
W They cannot be used as variabIe names.
W There are onIy 32 keywords avaiIabIe in c
auto doubIe if static do
break eIse int struct goto
case enum Iong switch signed
char extern near typedef whiIe
const fIoat register union defauIt
continue far return unsigned for
short void
12/12/2011 Programing With CC Programming 23
C !nstructions
There are basically four types of
instructions in C
Type declaration instruction
Eg int sum,
float ave,
char name,code,
!nput/Output instruction
Eg scanf(),
printf()
12/12/2011 Programing With CC Programming 24
C !nstructions
Arithmetic instruction
Eg :
x = sum +2,
Control instruction
Eg :
while do statement
for statement
if statement
12/12/2011 Programing With CC Programming 25
ata Types
W C Supports severaI different types of
data, each of which may be represented
differentIy within the computers memory.
W Basic data types are Iisted beIow
ata Type escription Typical Memory
int integer quantity 2 bytes
char singIe character 1 bytes
fIoat fIoating point number 4 bytes
doubIe doubIe-precision 8 bytes
fIoating point number
12/12/2011 Programing With CC Programming 26
Escape Sequences in C
Certain non printing characters can be
expressed in terms of escape sequences
Character Escape Sequence ASCII Value
bell \a 007
backspace \b 008
horizontal tab \t 009
vertical tab \v 001
newline \n 010
formfeed \f 012
carriage return \r 013
quotation mark (~) \ 034
question mark(?) \? 063
backslash (\) \\ 092
null \0 000
12/12/2011 Programing With CC Programming 27
String Constants
A String Constant consists of any
number consecutive characters
enclosed in double quotation marks
Escape sequence can be embedded
within the string.
12/12/2011 Programing With CC Programming 28
irst C Program
/* library function for input and output */
#include<stdio.h>
main()
{
int a,b,sum, /*variable declaration*/
printf(enter the first number::\n"),
scanf(d",Sa), /*receiving data from user */
printf(enter the second no: \n ),
scanf(d",Sb),
sum =a+b, /* calculating the sum*/
printf(the sum of two numbers: d\n,sum),
}
12/12/2011 Programing With CC Programming 29
Preparing and Running a
Complete C Program.
Planning a program
esign the algorithm for the given program
Statements must be selected and sequenced in
the most effective manner
Writing a C program
Translate the algorithm into equivalent C
instructions
Comments should be included within a C
program
A well written program should generate clear,
legible output. The program should be user
interactive
12/12/2011 Programing With CC Programming 30
Entering,Compiling and
Executing
The program can be entered into the
computer using an E!TOR. (!n unix
vi editor is used)
Eg . vi filename.c
Compiling and executing the program
cc filename.c
'he object code a.out is generated
'he program can be executed by entering a.out in the
prompt
#rogram asks for required input and generates the
output
12/12/2011 Programing With CC Programming 31
Assignment 01
Q1 : Which of the following are invalid
variable names and why ?
interestpaid
siint
AvERACE
Percent.
123
dist in km
ot pay
Name
LOAT
12/12/2011 Programing With CC Programming 32
Assignment 01
Q2 : Point out the errors, if any, in the
following C statements:
int =314.S62 *1S0 ,
name = 'Ajay',
3.14 * r*r = area,
k=a*b+c(2.Sa+b),
m_inst =rate of interest * numberofyears
/100,
area = 3.14 * r **2,
12/12/2011 Programing With CC Programming 33
Assignment 01
Q3 : A C program contains the following
statements:
#include<stdio.h>
int i,j,k,
write appropriate scanf and printf
functions to enter and print the
numerical values for i,j and k
Q4 : Each new c instruction has to be
written on a separate line. (True/false)
12/12/2011 Programing With CC Programming 34
Assignment 01
QS : Write C programs for the following :
(a) Rajesh's basic salary is input through the
keyboard. His dearness allowance is 40 of
basic salary, and house rent allowance is 20
of basic salary. Write a program to calculate his
gross salary.
(b) Two numbers are input through the
keyboard into two locations C and . Write a
program to interchange the contents of C and .
using temporary variable.
without using temporary variable
12/12/2011 Programing With CC Programming 35
Assignment 01
(c) !f the marks obtained by a student in five
different subjects are input through the keyboard,
find out the aggregate marks and percentage
marks obtained by the student. Assume that the
maximum marks that can be obtained by a
student in each subject is 100.
(d) The length and breadth of a rectangle and
radius of a circle are input through the keyboard.
Write a program to calculate the area and
perimeter of the rectangle, and the area and
circumference of the circle.

Вам также может понравиться