# 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
}
NIIT
NIIT
NIIT
NIIT
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
NIIT
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
NIIT
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
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
2 4 6
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
NIIT
NIIT
DATA TYPES
Fundamental data types Derived data types
NIIT
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
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
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
NIIT
*/ */ */
} 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
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
NIIT
NIIT
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
: : : : :
NIIT
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
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
: : : : : : : : : :
puts (str); puts (str); puts (str); puts (str); puts (str); puts (str); puts (str); puts (str); puts (str); break;
NIIT
NIIT