Sie sind auf Seite 1von 53

SIide 1 of 53 Ver. 1.

0
Programming in C
n this session, you will learn to:
dentify the benefits and features of C language
Use the data types available in C language
dentify the structure of C functions
Use input-output functions
Use constructs
Objectives
SIide 2 of 53 Ver. 1.0
Programming in C
!dentifying the Benefits and Features of C Language
en Thompson developed a new language called B.
B language was interpreter-based, hence it was slow.
Dennis Ritchie modified B language and made it a
compiler-based language.
The modified compiler-based B language is named as C.
SIide 3 of 53 Ver. 1.0
Programming in C
C language:
Possesses powerful low-level features of second generation
languages.
Provides loops and constructs available in third generation
languages.
s very powerful and flexible.
C as a Second and Third Ceneration Language
SIide 4 of 53 Ver. 1.0
Programming in C
C language:
Offers all essentials of structured programming.
Has functions that work along with other user-developed
functions and can be used as building blocks for advanced
functions.
Offers only a handful of functions, which form the core of the
language.
Has rest of the functions available in libraries. These functions
are developed using the core functions.
Block Structured Language An Advantage for Nodular Programming
SIide 5 of 53 Ver. 1.0
Programming in C
Features of the C Language
The features that make C a widely-used language are:
Pointers: Allows reference to a memory location by a name.
Memory Allocation: Allows static as well as dynamic memory
allocation.
Recursion: s a process in which a functions calls itself.
Bit Manipulation: Allows manipulation of data in its lowest form
of storage.
SIide 6 of 53 Ver. 1.0
Programming in C
Using the Data Types Available in C language
The types of data structures provided by C can be classified
under the following categories:
Fundamental data types
Derived data types
SIide 7 of 53 Ver. 1.0
Programming in C
Fundamental Data Types
Fundamental Data Types:
Are the data types at the lowest level.
Are used for actual data representation in the memory.
Are the base for other data types.
Have machine dependent storage requirement.
Are of the following three types:
char
int
float
SIide 8 of 53 Ver. 1.0
Programming in C
The storage requirement for fundamental data types can be
represented with the help of the following table.
Data Number of bytes on a
32-byte machine
Minimum Maximum
char 1 -128 127
int 4 -2^31 (2^31) - 1
float 4 6 digits of precision 6 digits of precision
Fundamental Data Types (Contd.)
SIide 9 of 53 Ver. 1.0
Programming in C
Derived Data Types
Derived Data Types:
Are represented in memory as fundamental data type.
Some derived data types are:
8hort int
long int
double float
SIide 10 of 53 Ver. 1.0
Programming in C
The storage requirement for derived data types can be
represented with the help of the following table.
Data Number of bytes
on a 32-byte
machine
Minimum Maximum
short int 2 -2^15 (2^15) - 1
long int 4 -2^31 (2^31) - 1
double float 8 12 digits of precision 6 digits of
precision
Derived Data Types (Contd.)
SIide 11 of 53 Ver. 1.0
Programming in C
Defining Data
The syntax for defining data is:
data type, ;ariable name,,...;
Declaration is done in the beginning of a function.
Definition for various data types is shown in the following
table.
Data definition Data type Memory defined Size (bytes) Value assigned
char a, c; char a
c
1
1
-
-
char a = 'Z'; char a 1 Z
int count; int count 4 -
int a, count =10; int a
count
4
4
-
10
float fnum; float fnum 4 -
float fnum1,
fnum2 = 93.63;
float fnum1
fnum2
4
4
-
93.63
SIide 12 of 53 Ver. 1.0
Programming in C
Practice: 1.1
rite the appropriate definitions for defining the following
variables:
. num to store integers.
2. chr to store a character and assign the character Z to it.
3. num to store a number and assign the value 8.93 to it.
4. i, j to store integers and assign the value 0 to j.
SIide 13 of 53 Ver. 1.0
Programming in C
Practice: 1.1 (Contd.)
Solution:
. int num;
2. char chr='Z';
3. float num = 8.93;
4. int i, j=0;
SIide 14 of 53 Ver. 1.0
Programming in C
Defining Strings:
Syntax:
char (;ariable) (number of byte8),;
Here number of bytes is one more than the number of
characters to store.
To define a memory location of 10 bytes or to store 9 valid
characters, the string will be defined as follows:
char 8tring 0,;
Defining Data (Contd.)
SIide 15 of 53 Ver. 1.0
Programming in C
Practice: 1.2
rite the appropriate definitions for defining the following
strings:
. addr8 to store 30 characters.
2. head to store 14 characters.
SIide 16 of 53 Ver. 1.0
Programming in C
Practice: 1.2 (Contd.)
Solution:
. char addr83,;
2. char head,;
SIide 17 of 53 Ver. 1.0
Programming in C
!dentifying the Structure of C Functions
n C language, the functions can be categorized in the
following categories:
Single-level functions
Multiple-level functions
SIide 18 of 53 Ver. 1.0
Programming in C
Single Level Functions
Single Level Functions:
Consider the following single-level function:
main()
,
/print a me88age/
printf("Welcome to C");
,
n the preceding function:
main(): s the first function to be executed.
(): Are used for passing parameters to a function.
{}: Are used to mark the beginning and end of a function. These
are mandatory in all functions.
/* */: s used for documenting various parts of a function.
SIide 19 of 53 Ver. 1.0
Programming in C
Semicolon (;): s used for marking the end of an executable
line.
printf(): s a C function for printing (displaying) constant or
variable data.
Single Level Functions (Contd.)
SIide 20 of 53 Ver. 1.0
Programming in C
Practice: 1.3
dentify any erroneous or missing component(s) in the
following functions:
. man()
,
printf("Thi8 function 8eem8 to be okay")
,
2. man()
,
/print a line/
printf("Thi8 function i8 perfect;
,
SIide 21 of 53 Ver. 1.0
Programming in C
Practice: 1.3 (Contd.)
3. main()
,
printf("Thi8 ha8 got to be right");
,
4. main()
,
Thi8 i8 a perfect comment line
printf("I8 it okay.");
,
SIide 22 of 53 Ver. 1.0
Programming in C
Solution:
1. man instead of main() and semi-colon missing at the end of
the printf() function.
2. mam instead of main() and ')' missing at the end of the
printf() function.
3. '}' instead of '{' for marking the beginning of the function and
'{' instead of '}' for marking the end of the function.
4. Comment line should be enclose between /* and */.
Practice: 1.3 (Contd.)
SIide 23 of 53 Ver. 1.0
Programming in C
Nultiple Level Functions
The following example shows functions at multiple
levels - one being called by another:
main ()
,
/ print a me88age /
printf ("Welcome to C.");
di8p_m8g ();
printf ("for good learning");
,
di8p_m8g ()
,
/ print another me88age /
printf ("All the be8t");
,
SIide 24 of 53 Ver. 1.0
Programming in C
The output of the preceding program is:
Welcome to C. All the be8t for good learning.
n the preceding program:
main(): s the first function to be executed.
di8p_m8g(): s a programmer-defined function that can be
independently called by any other function.
(): Are used for passing values to functions, depending on
whether the receiving function is expecting any parameter.
Semicolon (;): s used to terminate executable lines.
Nultiple Level Functions (Contd.)
SIide 25 of 53 Ver. 1.0
Programming in C
Practice: 1.4
dentify any erroneous or missing component(s) in the
following functions:
a. print_m8g()
, main();
printf(bye);
,
main()
, printf(Thi8 i8 the main function);,
b. main()
, /call another function/
di8_error();
,
di8p_err();
, printf(Error in function);,
SIide 26 of 53 Ver. 1.0
Programming in C
Solution:
a. main() is always the first function to be executed. Further
execution of the program depends on functions invoked from
main(). Here, after executing printf(), the program
terminates as no other function is invoked. The function
print_m8g is not invoked, hence it is not executed.
b. The two functions, di8_error() and di8p_error, are not
the same because the function names are different.
Practice: 1.4 (Contd.)
SIide 27 of 53 Ver. 1.0
Programming in C
Using the !nputOutput Functions
The C environment and the input and output operations are
shown in the following figure.
C Environment
Standard Error Device (stderr)
Standard nput Device (stdin) Standard Output Device (stdout)
SIide 28 of 53 Ver. 1.0
Programming in C
Using the !nputOutput Functions (Contd.)
These are assumed to be always linked to the C
environment:
8tdin - refers to keyboard
8tdin - refers to keyboard
8tdout - refers to VDU
8tderr - refers to VDU
nput and output takes place as a stream of characters.
Each device is linked to a buffer through which the flow of
characters takes place.
After an input operation from the standard input device, care
must be taken to clear input buffer.
SIide 29 of 53 Ver. 1.0
Programming in C
CharacterBased !nputOutput Functions
Character-Based nput-Output Functions are:
getc()
putc()
getchar()
putchar()
The following example uses the getc() and putc()
functions:
# include < 8tdio.h
/ function to accept and di8play a character/
main ()
,char alph;
alph = getc (8tdin); / accept a character /
fflu8h (8tdin); / clear the 8tdin buffer/
putc (alph, 8tdout); / di8play a character/
,
SIide 30 of 53 Ver. 1.0
Programming in C
The following example uses the getchar() and
putchar() functions:
# include < 8tdio.h
/ function to input and di8play a character u8ing the
function getchar() /
main () ,
char c;
c = getchar ();
fflu8h (8tdin); / clear the buffer /
putchar (c);
,
CharacterBased !nputOutput Functions (Contd.)
SIide 31 of 53 Ver. 1.0
Programming in C
Practice: 1.S
1. rite a function to input a character and display the
character input twice.
SIide 32 of 53 Ver. 1.0
Programming in C
Practice: 1.S (Contd.)
Solution:
Nicrosoft Word
Document
SIide 33 of 53 Ver. 1.0
Programming in C
Practice: 1.6
1. rite a function to accept and store two characters in
different memory locations, and to display them one after
the other using the functions getchar() and
putchar().
SIide 34 of 53 Ver. 1.0
Programming in C
Practice: 1.6 (Contd.)
Solution:
/ function to accept and di8play two character8/
#include<8tdio.h
main()
,
char a, b;
a=getchar();
fflu8h(8tdin);
b=getchar();
fflu8h(8tdin);
putchar(a);
putchar(b);
,
SIide 35 of 53 Ver. 1.0
Programming in C
StringBased !nputOutput Functions
String-based input-output functions are:
get8()
put8()
The following example uses the get8() and put8()
functions:
# include < 8tdio.h
/ function to accept and di8playing /
main ()
, char in_8tr ,2,; / di8play prompt /
put8 ("Enter a String of max 20 character8");
get8 (in_8tr); / accept 8tring /
fflu8h (8tdin); / clear the buffer /
put8 (in_8tr); / di8play input 8tring /
,
SIide 36 of 53 Ver. 1.0
Programming in C
Practice: 1.7
1. rite a function that prompts for and accepts a name with a
maximum of 25 characters, and displays the following
message.
Hello. How are you?
(name)
2. rite a function that prompts for a name (up to 20
characters) and address (up to 30 characters) and accepts
them one at a time. Finally, the name and address are
displayed in the following way.
Your name is:
(name)
Your address is:
(address)
SIide 37 of 53 Ver. 1.0
Programming in C
Solution:
Practice: 1.7 (Contd.)
Nicrosoft Word
Document
SIide 38 of 53 Ver. 1.0
Programming in C
Using Constructs
There are two types of constructs in C language:
Conditional constructs
Loop constructs
SIide 39 of 53 Ver. 1.0
Programming in C
Conditional Constructs
Conditional Constructs:
Requires relation operators as in other programming language
with a slight change in symbols used for relational operators.
The two types of conditional constructs in C are:
if..el8e construct
8witch.ca8e construct
SIide 40 of 53 Ver. 1.0
Programming in C
The Syntax of the if..el8e construct is as follows:
if (condition)
,
8tatement ;
8tatement 2 ;
:
,
el8e
,
8tatement ;
8tatement 2 ;
:
,
Conditional Constructs (Contd.)
SIide 41 of 53 Ver. 1.0
Programming in C
Practice: 1.8
1. rite a function that accepts one-character grade code, and
depending on what grade is input, display the HRA
percentage according to the following table.
rade HRA %
45%
B 40%
C 30%
D 25%
SIide 42 of 53 Ver. 1.0
Programming in C
Practice: 1.8 (Contd.)
dentify errors, if any, in the following function:
#include<8tdio.h
/function to check if y or n i8 input/
main()
,
char yn;
put8("Enter y or n for ye8/no");
yn = getchar();
fflu8h(8tdin);
if(yn='y')
put8("You entered y");
el8e if(yn=`n')
put8("You entered n");
el8e
put8("In;alid input");
,
SIide 43 of 53 Ver. 1.0
Programming in C
Solution:
Practice: 1.8 (Contd.)
Nicrosoft Word
Document
SIide 44 of 53 Ver. 1.0
Programming in C
Syntax of 8witch.ca8e construct:
8witch (;ariable)
,
ca8e :
8tatement ;
break ;
ca8e 2 :
8tatement 2 ;
:
:
break;
default :
8tatement
,
Conditional Constructs (Contd.)
SIide 45 of 53 Ver. 1.0
Programming in C
Practice: 1.3
rite a function to display the following menu and accept a
choice number. f an invalid choice is entered then an
appropriate error message must be displayed, else the
choice number entered must be displayed.
Menu
1. Create a directory
2. Delete a directory
3. Show a directory
4. Exit
Your choice:
SIide 46 of 53 Ver. 1.0
Programming in C
Solution:
Practice: 1.3 (Contd.)
Nicrosoft Word
Document
SIide 47 of 53 Ver. 1.0
Programming in C
Loop Constructs
The two types of conditional constructs in C are:
while loop construct.
do..while construct.
The while loop construct has the following syntax:
while (condition in true)
,
8tatement ; loop
8tatement 2 ; body
,
Used to iterate a set of instructions (the loop body) as long as
the specified condition is true.
SIide 48 of 53 Ver. 1.0
Programming in C
The do..while loop construct:
The do..while loop is similar to the while loop, except that the
condition is checked after execution of the body.
The do..while loop is executed at least once.
The following figure shows the difference between the while loop
and the do...while loop.
Loop Constructs (Contd.)
while
Evaluate
Condition
Execute
Body of
Loop
True
False
do while
Evaluate
Condition
Execute
Body of
Loop
True
False
SIide 49 of 53 Ver. 1.0
Programming in C
Practice: 1.10
1. rite a function to accept characters from the keyboard until
the character '!' is input, and to display whether the total
number of non-vowel characters entered is more than, less
than, or equal to the total number of vowels entered.
SIide 50 of 53 Ver. 1.0
Programming in C
Practice: 1.10 (Contd.)
Solution:
Nicrosoft Word
Document
SIide 51 of 53 Ver. 1.0
Programming in C
Summary
n this session, you learned that:
C language was developed by en Thompson and Dennis
Ritchie.
C language combines the features of second and third
generation languages.
C language is a block structured language.
C language has various features that make it a widely-used
language. Some of the important features are:
Pointers
Memory Allocation
Recursion
Bit-manipulation
SIide 52 of 53 Ver. 1.0
Programming in C
Summary (Contd.)
The types of data structures provided by C can be classified
under the following categories:
Fundamental data types: nclude the data types, which are used
for actual data representation in the memory.
Derived data types: Are based on fundamental data types.
Fundamental data types:
char, int, and float
Some of the derived data types are:
8hort int, long int, and double float
Definition of memory for any data, both fundamental and
derived data types, is done in the following format:
data type, ;ariable name,,...;
SIide 53 of 53 Ver. 1.0
Programming in C
Summary (Contd.)
n C language, the functions can be categorized in the
following categories:
Single-level functions
Multiple-level functions
For standard input-output operations, the C environment uses
8tdin, 8tdout, and 8tderr as references for accessing the
devices.
There are two types of constructs in C language:
Conditional constructs
Loop constructs

Das könnte Ihnen auch gefallen