This chapter was a good refresher on structures in C. Anyways a little introduction to compound data types in C.There are three types of compund data types in C: array,strings, and structures. Strings are just a special type of character array where the last element is a null character.
We can read as well as write data incore instead of the regular console by using sscanf and sprintf.
eg: sscanf(str, “%d %f”, &num, &flo); In this example the data will be read from the buffer reffered to by “str” and not standard input.
eg: sprintf(str, “%d %f”, num, flo); In this example the data will be written to a buffer reffered to by “str” instead of standard output.
A structure is a data type that consists of primitive as well as other compund (including structures) data types. For eg: this is how we will represent an Employee record using structures.
Exercise: Make an Employee structure which will contain last_name, first_name, title, and salary. Accept details from the user and populate an array of Employees till the user submits “enter” for the last_name. The program then prints the list of employees.
Did the exercise.
When the program accepts a decimal, the newline that the user presses to submit the number lingers in the input stream and must be flushed or eaten. Otherwise it will be accepted in the next scanf.
- What happens if the number of characters we enter are more than what the character array accepts.
- What is the difference between gets() and scanf().