Documente Academic
Documente Profesional
Documente Cultură
Objectives
Ver. 1.0
Slide 1 of 53
Programming in C
Identifying the Benefits and Features of C Language
Ken 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.
Ver. 1.0
Slide 2 of 53
Programming in C
C as a Second and Third Generation Language
C language:
Possesses powerful low-level features of second generation languages. Provides loops and constructs available in third generation languages. Is very powerful and flexible.
Ver. 1.0
Slide 3 of 53
Programming in C
Block Structured Language - An Advantage for Modular Programming
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.
Ver. 1.0
Slide 4 of 53
Programming in C
Features of the C Language
Ver. 1.0
Slide 5 of 53
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
Ver. 1.0
Slide 6 of 53
Programming in C
Fundamental Data Types
Ver. 1.0
Slide 7 of 53
Programming in C
Fundamental Data Types (Contd.)
The storage requirement for fundamental data types can be represented with the help of the following table.
Data char Number of bytes on a 32-byte machine 1 Minimum -128 Maximum 127
int
float
4
4
-2^31
6 digits of precision
(2^31) - 1
6 digits of precision
Ver. 1.0
Slide 8 of 53
Programming in C
Derived Data Types
Ver. 1.0
Slide 9 of 53
Programming in C
Derived Data Types (Contd.)
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 2 Minimum Maximum
short int
-2^15
(2^15) - 1
long int
double float
4
8
-2^31
12 digits of precision
(2^31) - 1
6 digits of precision
Ver. 1.0
Slide 10 of 53
Programming in C
Defining Data
Declaration is done in the beginning of a function. Definition for various data types is shown in the following table.
Data definition char a, c; char a = 'Z'; Data type char char Memory defined a c a Size (bytes) 1 1 1 Value assigned Z
int count;
int a, count =10; float fnum; float fnum1, fnum2 = 93.63;
int
int float float
count
a count fnum fnum1 fnum2
4
4 4 4 4 4
10 93.63
Ver. 1.0
Slide 11 of 53
Programming in C
Practice: 1.1
Ver. 1.0
Slide 12 of 53
Programming in C
Practice: 1.1 (Contd.)
Solution:
1. int num; 2. char chr=Z; 3. float num = 8.93; 4. int i, j=0;
Ver. 1.0
Slide 13 of 53
Programming in C
Defining Data (Contd.)
Defining Strings:
Syntax: char (variable) [(number of bytes)]; 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 string [10];
Ver. 1.0
Slide 14 of 53
Programming in C
Practice: 1.2
Ver. 1.0
Slide 15 of 53
Programming in C
Practice: 1.2 (Contd.)
Solution:
1. char addrs[31]; 2. char head[15];
Ver. 1.0
Slide 16 of 53
Programming in C
Identifying the Structure of C Functions
Ver. 1.0
Slide 17 of 53
Programming in C
Single Level Functions
Ver. 1.0
Slide 18 of 53
Programming in C
Single Level Functions (Contd.)
Semicolon (;): Is used for marking the end of an executable line. printf(): Is a C function for printing (displaying) constant or variable data.
Ver. 1.0
Slide 19 of 53
Programming in C
Practice: 1.3
Ver. 1.0
Slide 20 of 53
Programming in C
Practice: 1.3 (Contd.)
3. main() } printf("This has got to be right"); { 4. main() { This is a perfect comment line printf("Is it okay?"); }
Ver. 1.0
Slide 21 of 53
Programming in C
Practice: 1.3 (Contd.)
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 */.
Ver. 1.0
Slide 22 of 53
Programming in C
Multiple Level Functions
The following example shows functions at multiple levels - one being called by another:
main () { /* print a message */ printf ("Welcome to C."); disp_msg (); printf ("for good learning"); } disp_msg () { /* print another message */ printf ("All the best"); }
Ver. 1.0
Slide 23 of 53
Programming in C
Multiple Level Functions (Contd.)
Ver. 1.0
Slide 24 of 53
Programming in C
Practice: 1.4
Slide 25 of 53
Programming in C
Practice: 1.4 (Contd.)
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_msg is not invoked, hence it is not executed. b. The two functions, dis_error() and disp_error, are not the same because the function names are different.
Ver. 1.0
Slide 26 of 53
Programming in C
Using the Input-Output Functions
The C environment and the input and output operations are shown in the following figure.
C Environment
Ver. 1.0
Slide 27 of 53
Programming in C
Using the Input-Output Functions (Contd.)
Input 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.
Ver. 1.0
Slide 28 of 53
Programming in C
Character-Based Input-Output Functions
Slide 29 of 53
Programming in C
Character-Based Input-Output Functions (Contd.) The following example uses the getchar() and putchar() functions:
# include < stdio.h > /* function to input and display a character using the function getchar() */ main () { char c; c = getchar (); fflush (stdin); /* clear the buffer */ putchar (c); }
Ver. 1.0
Slide 30 of 53
Programming in C
Practice: 1.5
1.
Write a function to input a character and display the character input twice.
Ver. 1.0
Slide 31 of 53
Programming in C
Practice: 1.5 (Contd.)
Solution:
Ver. 1.0
Slide 32 of 53
Programming in C
Practice: 1.6
1.
Write 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().
Ver. 1.0
Slide 33 of 53
Programming in C
Practice: 1.6 (Contd.)
Solution:
/* function to accept and display two characters*/ #include<stdio.h> main() { char a, b; a=getchar(); fflush(stdin); b=getchar(); fflush(stdin); putchar(a); putchar(b); }
Ver. 1.0
Slide 34 of 53
Programming in C
String-Based Input-Output Functions
Slide 35 of 53
Programming in C
Practice: 1.7
1. Write 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. Write 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)
Ver. 1.0
Slide 36 of 53
Programming in C
Practice: 1.7 (Contd.)
Solution:
Ver. 1.0
Slide 37 of 53
Programming in C
Using Constructs
Ver. 1.0
Slide 38 of 53
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..else construct switchcase construct
Ver. 1.0
Slide 39 of 53
Programming in C
Conditional Constructs (Contd.)
The Syntax of the if..else construct is as follows:
if (condition) { statement 1 ; statement 2 ; : } else { statement 1 ; statement 2 ; : }
Ver. 1.0
Slide 40 of 53
Programming in C
Practice: 1.8
1. Write a function that accepts one-character grade code, and depending on what grade is input, display the HRA percentage according to the following table.
Grade
HRA %
A
B C D
45%
40% 30% 25%
Ver. 1.0
Slide 41 of 53
Programming in C
Practice: 1.8 (Contd.)
Slide 42 of 53
Programming in C
Practice: 1.8 (Contd.)
Solution:
Ver. 1.0
Slide 43 of 53
Programming in C
Conditional Constructs (Contd.) Syntax of switchcase construct:
switch (variable) { case 1 : statement1 ; break ; case 2 : statement 2 ; : : break; default : statement }
Ver. 1.0
Slide 44 of 53
Programming in C
Practice: 1.9
Write a function to display the following menu and accept a choice number. If 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:
Ver. 1.0
Slide 45 of 53
Programming in C
Practice: 1.9 (Contd.)
Solution:
Ver. 1.0
Slide 46 of 53
Programming in C
Loop Constructs
loop body
Used to iterate a set of instructions (the loop body) as long as the specified condition is true.
Ver. 1.0
Slide 47 of 53
Programming in C
Loop Constructs (Contd.)
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.
while do while Execute Body of Loop
False
False
Ver. 1.0
Slide 48 of 53
Programming in C
Practice: 1.10
1. Write 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.
Ver. 1.0
Slide 49 of 53
Programming in C
Practice: 1.10 (Contd.)
Solution:
Ver. 1.0
Slide 50 of 53
Programming in C
Summary
Ver. 1.0
Slide 51 of 53
Programming in C
Summary (Contd.)
The types of data structures provided by C can be classified under the following categories:
Fundamental data types: Include the data types, which are used for actual data representation in the memory. Derived data types: Are based on fundamental data types.
Definition of memory for any data, both fundamental and derived data types, is done in the following format:
[data type] [variable name],...;
Ver. 1.0
Slide 52 of 53
Programming in C
Summary (Contd.)
For standard input-output operations, the C environment uses stdin, stdout, and stderr as references for accessing the devices. There are two types of constructs in C language:
Conditional constructs Loop constructs
Ver. 1.0
Slide 53 of 53