Skip to main content


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 stored in d is %d"…

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 mem…

Strings in C

Strings in C Strings are defined as an array of characters. The difference between a character array and a string is the string is terminated with a special character ‘\0’. Declaration of strings: Declaring a string is as simple as declaring a one dimensional array. Below is the basic syntax for declaring a string. charstr_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’) used to indicate termination of string which differs strings from normal character arrays. Initializing a String: A string can be initialized in different ways. We will explain this with the help of an example. Below is an example to declare a string with name as str and initialize it with “cek”. 1. charstr[] = "cek"; 2. charstr[50] = "cek"; 3. charstr[] = {'c','…

Arrays in C

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. Declaring 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 each dimension of the array. Uninitialized arrays must have the dimensions of their rows, columns, etc. listed within the square brackets. 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 allocated only once, at the time the array is declared. The array does NOT…

Control Statements in C

Control statements enable us to specify the flow of program control; ie, the order in which the instructions in a program must be executed. They make it possible to make decisions, to perform tasks repeatedly or to jump from one section of code to another. There are four types of control statements in C: Decision making statements Selection statements Iteration statements Jump statements
1.Decision Making Statement: the if-else Statement The if-else statement is used to carry out a logical test and then take one of two possible actions depending on the outcome of the test (ie, whether the outcome is true or false). Syntax: if (condition) { statements } else { statements } If the condition specified in the if statement evaluates to true, the statements inside the if-block are executed and then the control gets transferred to the statement immediately after the if-block. Even if the condition is false and no else-block is present, control gets transferred to the statement immediately after t…

Input Output-printf() and scanf()

printf()  and scanf() function in C: printf() and scanf() functions are inbuilt library functions in C programming language which are available in C library by default.These functions are declared and related macros are defined in “stdio.h” which is a header file in C language. We have to include “stdio.h” file as shown in below C program to make use of these printf() and scanf() library functions in C language. printf() function in C: In C programming language, printf() function is used to print the “character, string, float, integer, octal and hexadecimal values” onto the output screen. Here’s a quick summary of the available printf format specifiers: %c character %d decimal (integer) number (base 10) %ld Long integer %e exponential floating-point number %f floating-point number %i integer (base 10) %o octal number (base 8) %s a string of characters %u unsigned decimal (integer) number %x number in hexadecimal (base 16) %% print a percent sign \% print a percent s…

C Operators and Expressions

C – Operators and Expressions The symbols which are used to perform logical and mathematical operations in a C program are called C operators. These C operators join individual constants and variables to form expressions. Operators, functions, constants and variables are combined together to form expressions. Consider the expression A + B * 5. where, +, * are operators, A, B are variables, 5 is constant and A + B * 5 is an expression. Types of C operators: C language offers many types of operators. They are, Types of Operators Description Arithmetic_operatorsThese are used to perform mathematical calculations like addition, subtraction, multiplication, division and modulusAssignment_operatorsThese are used to assign the values for the variables in C programs.Relational operatorsThese operators are used to compare the value of two variables.Logical operatorsThese operators are used to perform logical operations on the given two variables.Bit wise operat…