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

REVIEW QUIZ

# 1 Point out (syntax) errors, if any, in the following functions : a. # include < stdio.h> main () { \* this function has an error *\ puts ("Hope you have prepared the SPL session"); puts ("and will be able to solve this one"); }

NIIT

SEM Q/CPR/CR/SESSION 1/1/VER06/95

REVIEW QUIZ (Contd.)


b. # include < stdio.h> main () { /* this is a comment line **/ puts ("This seems to be perfect"); }

SEM Q/CPR/CR/SESSION 1/1/VER 06/95 NIIT SEM Q/CPR/CR/SESSION 1/2/VER06/95

REVIEW QUIZ (Contd.)


c. # include < stdio.h> main () { char inp; puts ("Enter y/n"); inp = getc (stdin); fflush (stdin); switch (inp) case 'y' : puts (Input was y"); break; case 'n' : puts ("Input was n"); break; default : puts ("Invalid input"); }
NIIT SEM Q/CPR/CR/SESSION 1/3/VER06/95

REVIEW QUIZ (Contd.)


# 2 What is the output from the following functions? a. /* # include < stdio.h> main () { char inp; puts ("This is a valid output"); inp = getchar (); fflush (stdin); putchar (inp); */
SEM Q/CPR/CR/SESSION 1/4/VER06/95

}
NIIT

REVIEW QUIZ (Contd.)


b. # include < stdio.h> main () { char inp; puts ("Enter Y/N for Yes/No"); inp = getchar (); fflush (stdin); while (inp = 'Y') puts ("Character Y was input"); }
NIIT SEM Q/CPR/CR/SESSION 1/5/VER06/95

REVIEW QUIZ (Contd.)


c. # include < stdio.h> main () { char a, b; puts ("Enter first character"); a = getchar (); puts ("Enter second character"); b = getchar (); puts ("End of the function"); }

NIIT

SEM Q/CPR/CR/SESSION 1/6/VER06/95

REVIEW QUIZ (Contd.)


d. # include < stdio.h> main () { int I = 5; if (I > 5); puts ("value of I is more than 5"); }

NIIT

SEM Q/CPR/CR/SESSION 1/7/VER06/95

REVIEW QUIZ (Contd.)


e. # include < stdio.h> main () { char inp = 'B'; if (inp = 'A') puts ("The variable inp"); puts ("contains A"); }

SEM Q/CPR/CR/SESSION 1/3/VER 06/95 NIIT SEM Q/CPR/CR/SESSION 1/8/VER06/95

REVIEW QUIZ (Contd.)


# 3 State whether true or false: a. The data types float and char are derived data types. b. The following statement defines a memory location of size 10 bytes : Char str {10}; c. The following definition is invalid : Float float; d. The function fflush() is used to clear any buffer associated with a device.

NIIT

SEM Q/CPR/CR/SESSION 1/9/VER06/95

SOLUTIONS TO QUIZ
# 1 a. \* and \ instead of / and */ b. No error c. The set of case statements should be enclosed in braces # 2 a. No output since the whole function is commented b. The function will endlessly display : "Character Y was input" c. "Enter first character" "Enter second character" "End of the function"

NIIT

SEM Q/CPR/CR/SESSION 1/10/VER06/95

SOLUTIONS TO QUIZ (Contd.)


(Since the buffer is not cleared after the first getchar (), the second getchar () does not wait for the input) d. "value of I is more than 5" (The ; after the if clause causes the statement to be terminated.) e. "contains A" # 3 a. False b. True c. False d. True

NIIT

SEM Q/CPR/CR/SESSION 1/11/VER06/95

SPL SESSION
Objectives At the end of this session, you will be able to : Appreciate the evolution of C as a programming language State why C can be classified as a second and third generation language Identify the various components of a C function State the data types available in C language in terms of Fundamental data types and their storage requirement - char, int and float Derived data types and their storage requirement short int, long int and double float
NIIT SEM Q/CPR/CR/SESSION 1/12/VER06/95

SPL SESSION (Contd.)


Write simple C functions for input and output using : Character-based input-output functions String-based input-output functions Write simple C functions using constructs for : Conditional statements ifelse switch.case Loops while dowhile Enter, compile and execute simple C functions

NIIT

SEM Q/CPR/CR/SESSION 1/13/VER06/95

NATURE OF C LANGUAGE
C is a second and third generation language Possesses low level features of second generation languages Provides loops and constructs available in third generation languages C language compilers are written in C Most of the 13,000 lines of code of the UNIX operating system are written in C Block structured language Offers all essentials of structured programming C language functions along with other user-developed functions can be used as building blocks for advanced functions
NIIT SEM Q/CPR/CR/SESSION 1/14/VER06/95

FEATURES OF C LANGUAGE
Pointers Memory allocation Recursion Bit-manipulation

NIIT

SEM Q/CPR/CR/SESSION 1/15/VER06/95

BIT-MANIPULATION
Example Multiplication by 2 by left-shift of bits Value BIT representation BIT representation Value in m in m of value in memory of value in memory (after BIT (after BIT shift) shift) 1 2 3
NIIT

0000 0001 0000 0010 0000 0011

0000 0010 0000 0100 0000 0110

2 4 6

SEM Q/CPR/CR/SESSION 1/16/VER06/95

STRUCTURE OF C FUNCTIONS
C functions may be : Single level Multiple level Single level function main () { /* print a message */ printf ("Welcome to C"); } main () - the first function to be executed () - used for passing parameters to a function { } - mark the beginning and end of a function are mandatory in all functions
NIIT SEM Q/CPR/CR/SESSION 1/17/VER06/95

STRUCTURE OF C FUNCTIONS (Contd.)


/* */ ; print() - used for documenting various parts of a function - used for marking end of an executable line - a C function for printing (displaying) constant or variable data

NIIT

SEM Q/CPR/CR/SESSION 1/18/VER06/95

STRUCTURE OF C FUNCTIONS (Contd.)


Multiple level function main () { /* print a message */ printf ("Welcome to C."); disp_msg (); printf ("for good learning"); } disp_msg () { /* print another message */ printf ("All the best"); }

NIIT

SEM Q/CPR/CR/SESSION 1/19/VER06/95

DATA TYPES
Fundamental data types Derived data types

NIIT

SEM Q/CPR/CR/SESSION 1/20/VER06/95

FUNDAMENTAL DATA TYPES

Data types at the lowest level Used for actual data representation in the memory Other data types are based on the fundamental data types Storage requirement is machine dependent

NIIT

SEM Q/CPR/CR/SESSION 1/21/VER06/95

FUNDAMENTAL DATA TYPES (Contd.)


3 types : char int float - characters and strings - integers - numbers with decimals - approximately 6 digits of precision (e.g. amounts, quotients, etc.) Storage requirement for fundamental data types Data type char int float Number of bytes on a Minimum 32-bit (machine) Value 1 -128 4 -2^ 31 4 6 digits of precision
NIIT

Maximum Value 127 (2^ 31)-1 6 digits of precision

SEM Q/CPR/CR/SESSION 1/22/VER06/95

DERIVED DATA TYPES


Represented in memory as fundamental data Some derived data types are : Short int - Integers from -32768 to 32767 Long int - Integers from 21474483648 to 21474233647 Double float - numbers with approximately 12 digits of precision Storage requirement for derived data types Data Number of bytes Minimum Maximum type on a 32-bit (machine) Value Value Short int 2 -2^ 15 (2^ 15) - 1 Long int 4 -2^ 31 (2^ 31) - 1 Double float 8 12 digits of 12 digits
NIIT SEM Q/CPR/CR/SESSION 1/23/VER06/95 precision of precision

DEFINING DATA
Syntax (data type) (variable); Data definition Data Memory type char char int int float fnum1 Size Value

char a, c ; char a = 'Z'; int count; int a, count = 10; float fnum; Float fnum1, float NIIT fnum2 = 93.63;

defined (bytes) assigned a 1 c 1 a 1 Z count 4 a 4 count 4 1 fnum 4 4 SEM Q/CPR/CR/SESSION 1/24/VER06/95 fnum2 4 93.63

DEFINING STRINGS
Syntax char (variable) [(number of bytes)]; Where : Number of bytes is one more than the number of characters to store Example To define a memory location of 10 bytes, to store 9 valid characters char string [10];

NIIT

SEM Q/CPR/CR/SESSION 1/25/VER06/95

STANDARD INPUT AND STANDARD OUTPUT


KEYBOARD Standard Input Device (stdin) VDU C Environment Standard Output Device (stdout) Standard Error

Device (stderr) These are assumed to be always linked to the C environment stdin refers to keyboard stdout refers to VDU NIIT stderr SEM Q/CPR/CR/SESSION 1/26/VER06/95 refers to VDU

STANDARD INPUT AND STANDARD OUTPUT (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

NIIT

SEM Q/CPR/CR/SESSION 1/27/VER06/95

CHARACTER-BASED INPUT/OUTPUT FUNCTIONS


Syntax Getc () : Variable = getc (stdin); Putc () : Putc (variable, stdout); Example # include < stdio.h> /* function to accept and display a character*/ main () { char alph; alph = getc (stdin); /* accept a character fflush (stdin); /* clear the stdin buffer putc (alph, stdout); /* display a character }
NIIT

*/ */ */

SEM Q/CPR/CR/SESSION 1/28/VER06/95

CHARACTER-BASED INPUT/OUTPUT FUNCTIONS (contd.)


Syntax getchar () : variable = getchar (); putchar () : Putchar (variable); Example # include < stdio.h > /* function to input and display a character using the */ /* function getchar () */ main () { char c; c = getchar (); fflush (stdin); /* clea the buffer */ putchar (c); }
NIIT SEM Q/CPR/CR/SESSION 1/29/VER06/95

STRING-BASED INPUT/OUTPUT FUNCTIONS


Syntax gets () : gets (variable); puts () : puts (variable); Example # include < stdio.h > /* function to accept and displaying */ main () { char in_str {21}; /* display prompt */ puts ("Enter a String of max 20 characters"); gets (in_str); /* accept string */ fflush (stdin); /* clear the buffer */ puts (in_str); /* display input string */ }
NIIT SEM Q/CPR/CR/SESSION 1/30/VER06/95

THE ifelse CONSTRUCT


Syntax If (condition) { statement 1 ; statement 2 ; : } else { statement 1 ; statement 2 ; : }
NIIT SEM Q/CPR/CR/SESSION 1/31/VER06/95

THE ifelse CONSTRUCT (Contd.)


Example Use of ifelse construct to determine whether input character is a lower-case/upper-case alphabet # include < stdio.h > main () { char inp; puts ("Enter a character"); inp =getchar (); fflush (stdin); if (inp > = 'A') { if (inp < = 'Z') puts ("Upper Case");
NIIT SEM Q/CPR/CR/SESSION 1/32/VER06/95

THE ifelse CONSTRUCT (Contd.)


Example (Contd.) else if (inp > = 'a') { if (inp < = 'z') puts ("Lower Case"); else puts ("Input character > z"); } else puts ("Input character > Z but less than a"); } else puts ("Input character less than A"); }
NIIT SEM Q/CPR/CR/SESSION 1/33/VER06/95

THE switch case CONSTRUCT


Syntax Switch (variable) { case 1 : case 2 : : default :

statement1 ; break ; statement 2 ; : statement

} Action is taken based on value of a variable Statements following default are executed if none of the cases is true Cases and default may occur in any order
NIIT SEM Q/CPR/CR/SESSION 1/34/VER06/95

Example

THE switch case CONSTRUCT (Contd.)

Determining whether a character is a vowel or not # include <stdio.h > /* function to determine if input character is a vowel */ /* using switchcase statement */ main () { char in_chr; puts ("Enter a character in lower case:"); in_chr = getchar (); fflush (stdin);

NIIT

SEM Q/CPR/CR/SESSION 1/35/VER06/95

THE switch case CONSTRUCT (Contd.)


Example (contd.) switch (in_chr) /* beginning of switch statement */ { case 'a' : puts ("vowel a"); break; /* break out of switch stmnt */ case 'e' : puts ("vowel e"); break; case 'l" : puts ("vowel l"); break; case 'o' : puts ("vowel o"); break; case 'u' : puts ("vowel u"); break; default : puts ("The character is not a vowel"); } /* end of switch statement */ }
NIIT SEM Q/CPR/CR/SESSION 1/36/VER06/95

THE WHILE LOOP CONSTRUCT


Syntax While (condition in true) { statement 1 ; loop statement 2 ; } Used to iterate a set of instructions (the loop body) as long as the specified condition is true body

NIIT

SEM Q/CPR/CR/SESSION 1/37/VER06/95

THE WHILE LOOP CONSTRUCT (contd.)


Example Generating and accepting choice # include < stdio.h> /* function to generate a menu, to accept and display choice */ /* using while loop construct */ main ()t { char chc, ok ; ok = 'n' ;

NIIT

SEM Q/CPR/CR/SESSION 1/38/VER06/95

THE WHILE LOOP CONSTRUCT (contd.)


Example (Contd.) while (ok = = 'n') { /* beginning of while block */ ok = ; /* initialise ok */ puts ("Menu"); puts ("1. Create a Directory"); puts ("2. Delete a Directory"); puts ("3. Show a Directory"); puts ("4. Exit"); puts (" "); puts ("Your choice :"); chc = getchar (); fflush (stdin);
NIIT SEM Q/CPR/CR/SESSION 1/39/VER06/95

THE WHILE LOOP CONSTRUCT (contd.)


Example (Contd.) Switch (chc) { case '1' case '2' case '3' case '4' default

puts ("Choice is 1"); break; puts ("Choice is 2"); break; puts ("Choice is 3"); break; puts ("Choice is 4"); break; puts ("Invalid Choice"); ok = 'n'; } /* end of switch block */ } /* end of while block */ }
NIIT SEM Q/CPR/CR/SESSION 1/40/VER06/95

: : : : :

THE DOWHILE LOOP CONSTRUCT


Similar to the while loop, except that the condition is checked after execution of the body. The loop is executed at least once.

NIIT

SEM Q/CPR/CR/SESSION 1/41/VER06/95

MULTIPLE ASSIGNMENTS
More than one variable may be initialized by a single assignment statement Example nv = v = o; This actually takes place in two steps : v = o; nv = v;

NIIT

SEM Q/CPR/CR/SESSION 1/42/VER06/95

CLASSROOM EXERCISE
# 1 Write a function to accept a character and display it 40 times. SOLUTION TO CLASSROOM EXERCISE # 1 # include < stdio.h> main () { char ch; int i = 0; puts ("Enter a character :"); ch = getchar (); fflush (stdin); do { putchar (ch); i = i + 1; } while (i < 40); }
NIIT SEM Q/CPR/CR/SESSION 1/43/VER06/95

CLASSROOM EXERCISE
# 2 Write a function that accepts a number from 0 to 9, along with a string. The string should then be displayed the number of times specified. [Hint : Use the switchcase construct to find an integerequivalent of the input]. SOLUTION TO CLASSROOM EXERCISE # 2 # include < stdio.h> main () { char str [15], inp; puts ("Enter number of times to display a string (0 to 9)"); inp = getchar (); fflush (stdin); puts ("Enter string to display");
NIIT SEM Q/CPR/CR/SESSION 1/44/VER06/95

SOLUTION TO CLASSROOM EXERCISE (contd.)


gets (str); Switch (inp) { case '9' case '8' case '7' case '6' case '5' case '4' case '3' case '2' case '1' case '0' } }
NIIT SEM Q/CPR/CR/SESSION 1/45/VER06/95

: : : : : : : : : :

puts (str); puts (str); puts (str); puts (str); puts (str); puts (str); puts (str); puts (str); puts (str); break;

SOLUTION TO CLASSROOM EXERCISE (contd.)


# 2 Alternate solution using ASCII codes of characters 0 to 9 to find an integer-equivalent of the input character : ASCII Code Character 48 0 49 1 50 2 51 3 52 4 53 5 54 6 55 7 56 8 57 9
NIIT SEM Q/CPR/CR/SESSION 1/46/VER06/95

SOLUTION TO CLASSROOM EXERCISE (contd.)


# include < stdio.h> main () { char str [15], inp; int repeat, i = 0; puts ("Enter number of times to display a string (0 to 9)"); inp = getchar (); fflush (stdin); puts ("Enter string to display"); gets (str); repeat =inp - 0 ; /* find difference in ASCII codes */ If (repeat > = 0)

NIIT

SEM Q/CPR/CR/SESSION 1/47/VER06/95

SOLUTION TO CLASSROOM EXERCISE (contd.)


if (repeat < = 9) { while (i < repeat) { puts (str); i = i + 1; } } }

NIIT

SEM Q/CPR/CR/SESSION 1/48/VER06/95