C programming session 01

2,672
-1

Published on

Published in: Technology
1 Comment
4 Likes
Statistics
Notes
No Downloads
Views
Total Views
2,672
On Slideshare
0
From Embeds
0
Number of Embeds
2
Actions
Shares
0
Downloads
196
Comments
1
Likes
4
Embeds 0
No embeds

No notes for slide
  • Begin the session by explaining the objectives of the session.
  • Discuss the history of the C language.
  • The storage requirements for different data types are machine-dependent. There is a subtle difference between declaring and defining variables. The statement: char a; Is used to declare a variable a. Here, only the attributes of the variable are specified, not the contents. In contrast, the statement: char a = ‘A’ ; is used to define the contents of the variable a.
  • Derived data types modify the data according to the specific requirements. For example, if you need to store a integer value above 32,767 or below -32,767, you can use long int instead of int . the derived data type long increases the range of the int data type.
  • Use Slide 12 to test the student’s understanding on defining the variables.
  • Use Slide 15 to test the student’s understanding on defining the variables.
  • A C function is typically made up of blocks. A block is a set of C statements enclosed within braces. Variables are generally declared at the beginning of a function, but may also be declared at the beginning of a block. A C program is modular and structured. This allows for reusability of code.
  • Use Slide 20 to test the student’s understanding on functions.
  • Multiple-level functions are those functions where a function calls another function. Like in the slide, the main() function calls another function disp_msg() .
  • Use Slide 25 to test the student’s understanding on functions.
  • The getc() and getchar() functions are used to take a character from a stream. The getc() functions takes a stream as a parameter while the getchar() function does not take any parameter. Similarly, the putc() and putchar() function outputs a character to a stream. The putc() functions takes two parameters, the character to be output and the stream, while the putchar() function takes only one parameter i.e. the character to be output.
  • Use Slide 31 to test the student’s understanding on character-based input-output functions.
  • Use Slide 33 to test the student’s understanding on the getchar() and putchar() functions.
  • The gets() function is used to get a string from stdin . It collects a string terminated by a new line character from the input stream. It takes a constant string as a parameter. The puts() function copies the null-terminated string, which is passed as a parameter, to the standard output stream.
  • Use Slide 36 to test the student’s understanding on the gets() and puts() functions.
  • Explain the need for conditional constructs and loop constructs by taking appropriate examples.
  • Use Slide 41 to test the student’s understanding on the conditional constructs.
  • Explain the limitations of the switch…case construct. Tell the students that the switch…case construct cannot work with float variable. Also, you cannot specify an expression in the switch…case construct.
  • Use Slide 45 to test the student’s understanding on the conditional constructs.
  • The condition associated with the while loop is checked before entering the loop. Hence, there is a possibility that the loop is not executed at all. In the other case, the condition is checked after executing the loop once and for every interaction thereafter.
  • Use Slide 49 to test the student’s understanding on the loop constructs.
  • Use Slides 51, 52, and 53 to summarize the session.
  • C programming session 01

    1. 1. <ul><li>In this session, you will learn to: </li></ul><ul><ul><li>Identify the benefits and features of C language </li></ul></ul><ul><ul><li>Use the data types available in C language </li></ul></ul><ul><ul><li>Identify the structure of C functions </li></ul></ul><ul><ul><li>Use input-output functions </li></ul></ul><ul><ul><li>Use constructs </li></ul></ul>Objectives
    2. 2. <ul><li>Ken Thompson developed a new language called B. </li></ul><ul><li>B language was interpreter-based, hence it was slow. </li></ul><ul><li>Dennis Ritchie modified B language and made it a compiler-based language. </li></ul><ul><li>The modified compiler-based B language is named as C. </li></ul>Identifying the Benefits and Features of C Language
    3. 3. <ul><li>C language: </li></ul><ul><ul><li>Possesses powerful low-level features of second generation languages. </li></ul></ul><ul><ul><li>Provides loops and constructs available in third generation languages. </li></ul></ul><ul><ul><li>Is very powerful and flexible. </li></ul></ul>C as a Second and Third Generation Language
    4. 4. <ul><li>C language: </li></ul><ul><ul><li>Offers all essentials of structured programming. </li></ul></ul><ul><ul><li>Has functions that work along with other user-developed functions and can be used as building blocks for advanced functions. </li></ul></ul><ul><ul><li>Offers only a handful of functions, which form the core of the language. </li></ul></ul><ul><ul><li>Has rest of the functions available in libraries. These functions are developed using the core functions. </li></ul></ul>Block Structured Language - An Advantage for Modular Programming
    5. 5. <ul><li>The features that make C a widely-used language are: </li></ul><ul><ul><li>Pointers: Allows reference to a memory location by a name. </li></ul></ul><ul><ul><li>Memory Allocation: Allows static as well as dynamic memory allocation. </li></ul></ul><ul><ul><li>Recursion: Is a process in which a functions calls itself. </li></ul></ul><ul><ul><li>Bit Manipulation: Allows manipulation of data in its lowest form of storage. </li></ul></ul>Features of the C Language
    6. 6. <ul><li>The types of data structures provided by C can be classified under the following categories: </li></ul><ul><ul><li>Fundamental data types </li></ul></ul><ul><ul><li>Derived data types </li></ul></ul>Using the Data Types Available in C language
    7. 7. <ul><li>Fundamental Data Types: </li></ul><ul><ul><li>Are the data types at the lowest level. </li></ul></ul><ul><ul><li>Are used for actual data representation in the memory. </li></ul></ul><ul><ul><li>Are the base for other data types. </li></ul></ul><ul><ul><li>Have machine dependent storage requirement. </li></ul></ul><ul><ul><li>Are of the following three types: </li></ul></ul><ul><ul><ul><li>char </li></ul></ul></ul><ul><ul><ul><li>int </li></ul></ul></ul><ul><ul><ul><li>float </li></ul></ul></ul>Fundamental Data Types
    8. 8. <ul><li>The storage requirement for fundamental data types can be represented with the help of the following table. </li></ul>Fundamental Data Types (Contd.) 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
    9. 9. <ul><li>Derived Data Types: </li></ul><ul><ul><li>Are represented in memory as fundamental data type. </li></ul></ul><ul><li>Some derived data types are: </li></ul><ul><ul><li>short int </li></ul></ul><ul><ul><li>long int </li></ul></ul><ul><ul><li>double float </li></ul></ul>Derived Data Types
    10. 10. <ul><li>The storage requirement for derived data types can be represented with the help of the following table. </li></ul>Derived Data Types (Contd.) 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
    11. 11. <ul><li>The syntax for defining data is: </li></ul><ul><li> [data type] [variable name],...; </li></ul><ul><li>Declaration is done in the beginning of a function. </li></ul><ul><li>Definition for various data types is shown in the following table. </li></ul>Defining Data 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
    12. 12. <ul><li>Write the appropriate definitions for defining the following variables: </li></ul><ul><ul><li>num to store integers. </li></ul></ul><ul><ul><li>chr to store a character and assign the character Z to it. </li></ul></ul><ul><ul><li>num to store a number and assign the value 8.93 to it. </li></ul></ul><ul><ul><li>i, j to store integers and assign the value 0 to j . </li></ul></ul>Practice: 1.1
    13. 13. <ul><li>Solution: </li></ul><ul><ul><li>int num; </li></ul></ul><ul><ul><li>char chr=’Z’; </li></ul></ul><ul><ul><li>float num = 8.93; </li></ul></ul><ul><ul><li>int i, j=0; </li></ul></ul>Practice: 1.1 (Contd.)
    14. 14. <ul><li>Defining Strings: </li></ul><ul><ul><li>Syntax: </li></ul></ul><ul><ul><ul><li>char (variable) [(number of bytes)]; </li></ul></ul></ul><ul><li> Here number of bytes is one more than the number of characters to store. </li></ul><ul><ul><li>To define a memory location of 10 bytes or to store 9 valid characters, the string will be defined as follows: </li></ul></ul><ul><ul><li> char string [10]; </li></ul></ul>Defining Data (Contd.)
    15. 15. <ul><li>Write the appropriate definitions for defining the following strings: </li></ul><ul><ul><li>addrs to store 30 characters. </li></ul></ul><ul><ul><li>head to store 14 characters. </li></ul></ul>Practice: 1.2
    16. 16. <ul><li>Solution: </li></ul><ul><ul><li>char addrs[31]; </li></ul></ul><ul><ul><li>char head[15]; </li></ul></ul>Practice: 1.2 (Contd.)
    17. 17. <ul><li>In C language, the functions can be categorized in the following categories: </li></ul><ul><ul><li>Single-level functions </li></ul></ul><ul><ul><li>Multiple-level functions </li></ul></ul>Identifying the Structure of C Functions
    18. 18. <ul><li>Single Level Functions: </li></ul><ul><ul><li>Consider the following single-level function: </li></ul></ul><ul><ul><ul><li>main() </li></ul></ul></ul><ul><ul><ul><li>{ </li></ul></ul></ul><ul><ul><ul><ul><li>/*print a message*/ </li></ul></ul></ul></ul><ul><ul><ul><ul><li>printf(&quot;Welcome to C&quot;); </li></ul></ul></ul></ul><ul><ul><ul><li>} </li></ul></ul></ul><ul><li>In the preceding function: </li></ul><ul><ul><li>main() : Is the first function to be executed. </li></ul></ul><ul><ul><li>(): Are used for passing parameters to a function. </li></ul></ul><ul><ul><li>{}: Are used to mark the beginning and end of a function. These are mandatory in all functions. </li></ul></ul><ul><ul><li>/* */: Is used for documenting various parts of a function. </li></ul></ul>Single Level Functions
    19. 19. <ul><ul><li>Semicolon (;): Is used for marking the end of an executable line. </li></ul></ul><ul><ul><li>printf() : Is a C function for printing (displaying) constant or variable data. </li></ul></ul>Single Level Functions (Contd.)
    20. 20. <ul><li>Identify any erroneous or missing component(s) in the following functions: </li></ul><ul><ul><li>1. man() </li></ul></ul><ul><ul><li>{ </li></ul></ul><ul><li> printf(&quot;This function seems to be okay&quot;) </li></ul><ul><ul><li>} </li></ul></ul><ul><ul><li>2. man() </li></ul></ul><ul><ul><li>{ </li></ul></ul><ul><li> /*print a line*/ </li></ul><ul><li> printf(&quot;This function is perfect“; </li></ul><ul><li>} </li></ul>Practice: 1.3
    21. 21. <ul><ul><li>3. main() </li></ul></ul><ul><ul><li> } </li></ul></ul><ul><ul><li> printf(&quot;This has got to be right&quot;); </li></ul></ul><ul><ul><li> { </li></ul></ul><ul><ul><li>4. main() </li></ul></ul><ul><ul><li> { </li></ul></ul><ul><ul><li> This is a perfect comment line </li></ul></ul><ul><ul><li> printf(&quot;Is it okay?&quot;); </li></ul></ul><ul><ul><li> } </li></ul></ul>Practice: 1.3 (Contd.)
    22. 22. <ul><li>Solution: </li></ul><ul><ul><li>1. man instead of main() and semi-colon missing at the end of the printf() function. </li></ul></ul><ul><ul><li>2. mam instead of main() and ‘)’ missing at the end of the printf() function. </li></ul></ul><ul><ul><li>3. ‘}’ instead of ‘{‘ for marking the beginning of the function and ‘{’ instead of ‘}‘ for marking the end of the function. </li></ul></ul><ul><ul><li>4. Comment line should be enclose between /* and */. </li></ul></ul>Practice: 1.3 (Contd.)
    23. 23. <ul><li>The following example shows functions at multiple levels - one being called by another : </li></ul><ul><ul><li>main () </li></ul></ul><ul><ul><li>{ </li></ul></ul><ul><ul><ul><li>/* print a message */ </li></ul></ul></ul><ul><ul><ul><li>printf (&quot;Welcome to C.&quot;); </li></ul></ul></ul><ul><ul><ul><li>disp_msg (); </li></ul></ul></ul><ul><ul><ul><li>printf (&quot;for good learning&quot;); </li></ul></ul></ul><ul><ul><ul><li>} </li></ul></ul></ul><ul><ul><ul><li>disp_msg () </li></ul></ul></ul><ul><ul><ul><li>{ </li></ul></ul></ul><ul><ul><ul><li>/* print another message */ </li></ul></ul></ul><ul><ul><ul><li>printf (&quot;All the best&quot;); </li></ul></ul></ul><ul><ul><ul><li>} </li></ul></ul></ul>Multiple Level Functions
    24. 24. <ul><li>The output of the preceding program is: </li></ul><ul><ul><li>Welcome to C. All the best for good learning. </li></ul></ul><ul><li>In the preceding program: </li></ul><ul><ul><li>main() : Is the first function to be executed. </li></ul></ul><ul><ul><li>disp_msg() : Is a programmer-defined function that can be independently called by any other function. </li></ul></ul><ul><ul><li>(): Are used for passing values to functions, depending on whether the receiving function is expecting any parameter. </li></ul></ul><ul><ul><li>Semicolon (;): Is used to terminate executable lines. </li></ul></ul>Multiple Level Functions (Contd.)
    25. 25. <ul><li>Identify any erroneous or missing component(s) in the following functions: </li></ul><ul><ul><li>a. print_msg() </li></ul></ul><ul><ul><ul><li>{ main(); </li></ul></ul></ul><ul><ul><ul><li>printf(“bye”); </li></ul></ul></ul><ul><ul><ul><li>} </li></ul></ul></ul><ul><ul><ul><li>main() </li></ul></ul></ul><ul><ul><ul><li>{ printf(“This is the main function”);} </li></ul></ul></ul><ul><ul><li>b. main() </li></ul></ul><ul><ul><ul><li>{ /*call another function*/ </li></ul></ul></ul><ul><ul><ul><li>dis_error(); </li></ul></ul></ul><ul><ul><ul><li>} </li></ul></ul></ul><ul><ul><ul><li>disp_err(); </li></ul></ul></ul><ul><ul><ul><li>{ printf(“Error in function”);} </li></ul></ul></ul>Practice: 1.4
    26. 26. <ul><li>Solution: </li></ul><ul><ul><li>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. </li></ul></ul><ul><ul><li>b. The two functions, dis_error() and disp_error , are not the same because the function names are different. </li></ul></ul>Practice: 1.4 (Contd.)
    27. 27. Using the Input-Output Functions <ul><li>The C environment and the input and output operations are shown in the following figure. </li></ul>C Environment Standard Error Device (stderr) Standard Input Device (stdin) Standard Output Device (stdout)
    28. 28. <ul><li>These are assumed to be always linked to the C environment: </li></ul><ul><ul><li>stdin - refers to keyboard </li></ul></ul><ul><ul><li>stdin - refers to keyboard </li></ul></ul><ul><ul><li>stdout - refers to VDU </li></ul></ul><ul><ul><li>stderr - refers to VDU </li></ul></ul><ul><li>Input and output takes place as a stream of characters. </li></ul><ul><li>Each device is linked to a buffer through which the flow of characters takes place. </li></ul><ul><li>After an input operation from the standard input device, care must be taken to clear input buffer. </li></ul>Using the Input-Output Functions (Contd.)
    29. 29. <ul><li>Character-Based Input-Output Functions are: </li></ul><ul><ul><li>getc() </li></ul></ul><ul><ul><li>putc() </li></ul></ul><ul><ul><li>getchar() </li></ul></ul><ul><ul><li>putchar() </li></ul></ul><ul><li>The following example uses the getc() and putc() functions: </li></ul><ul><ul><li># include < stdio.h> </li></ul></ul><ul><ul><li>/* function to accept and display a character*/ </li></ul></ul><ul><ul><li>main () </li></ul></ul><ul><ul><li>{char alph; </li></ul></ul><ul><ul><li>alph = getc (stdin); /* accept a character */ </li></ul></ul><ul><ul><li>fflush (stdin); /* clear the stdin buffer*/ </li></ul></ul><ul><ul><li>putc (alph, stdout); /* display a character*/ </li></ul></ul><ul><ul><li>} </li></ul></ul>Character-Based Input-Output Functions
    30. 30. <ul><li>The following example uses the getchar() and putchar() functions : </li></ul><ul><ul><li># include < stdio.h > </li></ul></ul><ul><ul><li>/* function to input and display a character using the function getchar() */ </li></ul></ul><ul><ul><li>main () { </li></ul></ul><ul><ul><li>char c; </li></ul></ul><ul><ul><li>c = getchar (); </li></ul></ul><ul><ul><li>fflush (stdin); /* clear the buffer */ </li></ul></ul><ul><ul><li>putchar (c); </li></ul></ul><ul><li> } </li></ul>Character-Based Input-Output Functions (Contd.)
    31. 31. <ul><li>Write a function to input a character and display the character input twice. </li></ul>Practice: 1.5
    32. 32. <ul><li>Solution: </li></ul>Practice: 1.5 (Contd.)
    33. 33. <ul><li>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() . </li></ul>Practice: 1.6
    34. 34. <ul><li>Solution: </li></ul><ul><ul><li>/* function to accept and display two characters*/ </li></ul></ul><ul><ul><ul><li>#include<stdio.h> </li></ul></ul></ul><ul><ul><ul><li>main() </li></ul></ul></ul><ul><ul><ul><li>{ </li></ul></ul></ul><ul><ul><ul><li>char a, b; </li></ul></ul></ul><ul><ul><ul><li>a=getchar(); </li></ul></ul></ul><ul><ul><ul><li>fflush(stdin); </li></ul></ul></ul><ul><ul><ul><li>b=getchar(); </li></ul></ul></ul><ul><ul><ul><li>fflush(stdin); </li></ul></ul></ul><ul><ul><ul><li>putchar(a); </li></ul></ul></ul><ul><ul><ul><li>putchar(b); </li></ul></ul></ul><ul><ul><ul><li>} </li></ul></ul></ul>Practice: 1.6 (Contd.)
    35. 35. <ul><li>String-based input-output functions are: </li></ul><ul><ul><li>gets() </li></ul></ul><ul><ul><li>puts() </li></ul></ul><ul><li>The following example uses the gets() and puts() functions: </li></ul><ul><ul><li># include < stdio.h > </li></ul></ul><ul><ul><li>/* function to accept and displaying */ </li></ul></ul><ul><ul><li>main () { char in_str {21}; /* display prompt */ </li></ul></ul><ul><ul><li>puts (&quot;Enter a String of max 20 characters&quot;); </li></ul></ul><ul><ul><li>gets (in_str); /* accept string */ </li></ul></ul><ul><ul><li>fflush (stdin); /* clear the buffer */ </li></ul></ul><ul><ul><li>puts (in_str); /* display input string */ </li></ul></ul><ul><ul><li>} </li></ul></ul>String-Based Input-Output Functions
    36. 36. <ul><li>1. Write a function that prompts for and accepts a name with a maximum of 25 characters, and displays the following message. </li></ul><ul><ul><li>Hello. How are you? </li></ul></ul><ul><ul><li>(name) </li></ul></ul><ul><li>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. </li></ul><ul><ul><li>Your name is: </li></ul></ul><ul><ul><li>(name) </li></ul></ul><ul><ul><li>Your address is: </li></ul></ul><ul><ul><li>(address) </li></ul></ul>Practice: 1.7
    37. 37. <ul><li>Solution: </li></ul>Practice: 1.7 (Contd.)
    38. 38. <ul><li>There are two types of constructs in C language: </li></ul><ul><ul><li>Conditional constructs </li></ul></ul><ul><ul><li>Loop constructs </li></ul></ul>Using Constructs
    39. 39. <ul><li>Conditional Constructs: </li></ul><ul><ul><li>Requires relation operators as in other programming language with a slight change in symbols used for relational operators. </li></ul></ul><ul><ul><li>The two types of conditional constructs in C are: </li></ul></ul><ul><ul><ul><li>if..else construct </li></ul></ul></ul><ul><ul><ul><li>switch…case construct </li></ul></ul></ul>Conditional Constructs
    40. 40. <ul><ul><li>The Syntax of the if..else construct is as follows: </li></ul></ul><ul><ul><ul><li>if (condition) </li></ul></ul></ul><ul><ul><ul><li>{ </li></ul></ul></ul><ul><ul><ul><li>statement 1 ; </li></ul></ul></ul><ul><ul><ul><li>statement 2 ; </li></ul></ul></ul><ul><ul><ul><li> : </li></ul></ul></ul><ul><ul><ul><li>} </li></ul></ul></ul><ul><ul><ul><li>else </li></ul></ul></ul><ul><ul><ul><li>{ </li></ul></ul></ul><ul><ul><ul><li>statement 1 ; </li></ul></ul></ul><ul><ul><ul><li>statement 2 ; </li></ul></ul></ul><ul><ul><ul><li> : </li></ul></ul></ul><ul><ul><ul><li>} </li></ul></ul></ul>Conditional Constructs (Contd.)
    41. 41. <ul><li>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. </li></ul>Practice: 1.8 Grade HRA % A 45% B 40% C 30% D 25%
    42. 42. <ul><li>Identify errors, if any, in the following function: </li></ul><ul><ul><li>#include<stdio.h> </li></ul></ul><ul><ul><li>/*function to check if y or n is input*/ </li></ul></ul><ul><ul><li>main() </li></ul></ul><ul><ul><li>{ </li></ul></ul><ul><ul><li>char yn; </li></ul></ul><ul><ul><li>puts(&quot;Enter y or n for yes/no&quot;); </li></ul></ul><ul><ul><li>yn = getchar(); </li></ul></ul><ul><ul><li>fflush(stdin); </li></ul></ul><ul><ul><li>if(yn=’y’) </li></ul></ul><ul><ul><li>puts(&quot;You entered y&quot;); </li></ul></ul><ul><ul><li>else if(yn=‘n') </li></ul></ul><ul><ul><li>puts(&quot;You entered n&quot;); </li></ul></ul><ul><ul><li>else </li></ul></ul><ul><ul><li>puts(&quot;Invalid input&quot;); </li></ul></ul><ul><ul><li>} </li></ul></ul>Practice: 1.8 (Contd.)
    43. 43. <ul><li>Solution: </li></ul>Practice: 1.8 (Contd.)
    44. 44. <ul><li>Syntax of switch…case construct: </li></ul><ul><ul><li>switch (variable) </li></ul></ul><ul><ul><li>{ </li></ul></ul><ul><ul><li> case 1 : </li></ul></ul><ul><ul><li>statement1 ; </li></ul></ul><ul><ul><li>break ; </li></ul></ul><ul><ul><li>case 2 : </li></ul></ul><ul><ul><li>statement 2 ; </li></ul></ul><ul><ul><li> : </li></ul></ul><ul><ul><li>: </li></ul></ul><ul><ul><li>break; </li></ul></ul><ul><ul><li>default : </li></ul></ul><ul><ul><li>statement </li></ul></ul><ul><ul><li>} </li></ul></ul>Conditional Constructs (Contd.)
    45. 45. <ul><li>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. </li></ul><ul><li>Menu </li></ul><ul><li>1. Create a directory </li></ul><ul><li>2. Delete a directory </li></ul><ul><li>3. Show a directory </li></ul><ul><li>4. Exit </li></ul><ul><li>Your choice: </li></ul>Practice: 1.9
    46. 46. <ul><li>Solution: </li></ul>Practice: 1.9 (Contd.)
    47. 47. <ul><li>The two types of conditional constructs in C are: </li></ul><ul><ul><li>while loop construct. </li></ul></ul><ul><ul><li>do..while construct. </li></ul></ul><ul><ul><li>The while loop construct has the following syntax: </li></ul></ul><ul><ul><ul><li>while (condition in true) </li></ul></ul></ul><ul><ul><ul><li>{ </li></ul></ul></ul><ul><ul><ul><li>statement 1 ; loop </li></ul></ul></ul><ul><ul><ul><li>statement 2 ; body </li></ul></ul></ul><ul><ul><ul><li>} </li></ul></ul></ul><ul><ul><li>Used to iterate a set of instructions (the loop body) as long as the specified condition is true. </li></ul></ul>Loop Constructs
    48. 48. <ul><ul><li>The do..while loop construct: </li></ul></ul><ul><ul><ul><li>The do..while loop is similar to the while loop, except that the condition is checked after execution of the body. </li></ul></ul></ul><ul><ul><ul><li>The do..while loop is executed at least once. </li></ul></ul></ul><ul><ul><ul><li>The following figure shows the difference between the while loop and the do...while loop. </li></ul></ul></ul>Loop Constructs (Contd.) while Evaluate Condition Execute Body of Loop True False do while Evaluate Condition Execute Body of Loop True False
    49. 49. <ul><li>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. </li></ul>Practice: 1.10
    50. 50. <ul><li>Solution: </li></ul>Practice: 1.10 (Contd.)
    51. 51. <ul><li>In this session, you learned that: </li></ul><ul><ul><li>C language was developed by Ken Thompson and Dennis Ritchie. </li></ul></ul><ul><ul><li>C language combines the features of second and third generation languages. </li></ul></ul><ul><ul><li>C language is a block structured language. </li></ul></ul><ul><ul><li>C language has various features that make it a widely-used language. Some of the important features are: </li></ul></ul><ul><ul><ul><li>Pointers </li></ul></ul></ul><ul><ul><ul><li>Memory Allocation </li></ul></ul></ul><ul><ul><ul><li>Recursion </li></ul></ul></ul><ul><ul><ul><li>Bit-manipulation </li></ul></ul></ul>Summary
    52. 52. <ul><ul><li>The types of data structures provided by C can be classified under the following categories: </li></ul></ul><ul><ul><ul><li>Fundamental data types: Include the data types, which are used for actual data representation in the memory. </li></ul></ul></ul><ul><ul><ul><li>Derived data types: Are based on fundamental data types. </li></ul></ul></ul><ul><ul><li>Fundamental data types: </li></ul></ul><ul><ul><ul><li>char , int , and float </li></ul></ul></ul><ul><ul><li>Some of the derived data types are: </li></ul></ul><ul><ul><ul><li>short int , long int , and double float </li></ul></ul></ul><ul><ul><li>Definition of memory for any data, both fundamental and derived data types, is done in the following format: </li></ul></ul><ul><ul><ul><li>[data type] [variable name],...; </li></ul></ul></ul>Summary (Contd.)
    53. 53. <ul><ul><li>In C language, the functions can be categorized in the following categories: </li></ul></ul><ul><ul><ul><li>Single-level functions </li></ul></ul></ul><ul><ul><ul><li>Multiple-level functions </li></ul></ul></ul><ul><ul><li>For standard input-output operations, the C environment uses stdin , stdout , and stderr as references for accessing the devices. </li></ul></ul><ul><ul><li>There are two types of constructs in C language: </li></ul></ul><ul><ul><ul><li>Conditional constructs </li></ul></ul></ul><ul><ul><ul><li>Loop constructs </li></ul></ul></ul>Summary (Contd.)

    ×