Skip to main content

Posts

Showing posts from January, 2018

Enumerated Data Type

Enumeration (or enum) is a user defined data type in C. It is mainly used to assign names to integral constants, the names make a program easy to read and maintain.The symbolically declared members are integer constants. The keyword enum is used to declare an enumeration type. The general construct used to declare an enumeration type is enum tag_name {member1, member2, member3,..,member} variable1, variable2,..,variable n In this declaration, either tag_name or variable may be omitted or both may be present. But at least one of them must exist in this declaration construct. The members are integer constants. By default the first member member1 assigned value 0 member2 assigned value 1 and so on. Eg: enum week{Mon, Tue, Wed,Thu,Fri,Sat}; enum week day; // Or enum week{Mon, Tue, Wed,Thu,Fri,Sat}day; #include <stdio.h> enum day {sunday, monday, tuesday, wednesday, thursday, friday, saturday}; int main() { enum day d = thursday; printf("The day number

Structures and Unions

A structure is a user-defined data type available in C that allows to combining data items of different kinds. Structures are used to represent a record. Defining a structure: To define a structure, you must use the struct keyword followed by an optional struct tag followed by the body of the struct. The struct statement defines a new data type, with more than one member. The format of the struct statement is as follows: struct [structure name] { member definition; member definition; ... member definition; }; Eg: struct student { int rno; char name[50]; int mark; char sex; float height; } S; Here S is a structure variable. Structure member can be any valid data type(primitive, arrays) including other structures and pointers. A structure may not, for obvious reasons, contain instances of itself but may contain pointers to instances of itself. We can also create array of structures or pointer to a struct. Accessing the members of a structure: The

Strings in C

Strings in C Strings are defined as an array of characters. The difference between a character array and a string is that the string is terminated with a special character ‘\0’. The string can be defined as the one-dimensional array of  characters terminated by a null ('\0’).  The character array or the string is used to manipulate text such as word or sentences.  Each character in the array occupies one byte of memory. The termination character ('\0') is important in a string since it is the only way to identify where the string ends Declaration of strings : Declaring a string is as simple as declaring a one dimensional array. Below is the basic syntax for declaring a string. char str_name[size]; In the above syntax str_name is any name given to the string variable and size is used define the length of the string, i.e the number of characters strings will store. Please keep in mind that there is an extra terminating character which is the Null character ('\0&

Arrays in C-single and multi dimensional arrays- list and matrix

An array is a collection of data items, all of the same type, accessed using a common name. A one-dimensional array is like a list(vector); A two dimensional array is like a table(matrix). We can have more dimensions. Always, Contiguous (adjacent) memory locations are used to store array elements in memory. Elements of the array can be randomly accessed since we can calculate the address of each element of the array with the given base address and the size of the data element. Declaring  Single Dimensional Arrays Array variables are declared identically to variables of their data type, except that the variable name is followed by one pair of square [ ] brackets for mentioning dimension of the array.  Dimensions used when declaring arrays in C must be positive integral constants or constant expressions.  In C99, dimensions must still be positive integers, but variables can be used, so long as the variable has a positive value at the time the array is declared. ( Space is allo