Introduction:
C is a general purpose, procedural, structured programming language developed at Bell Laboratories of USA in the year 1972. It was developed by Dennis Ritchie and hence he is known as the father of C.
Use of ‘C’ Language
C was originally developed for writing system software. The applications of C is not only limited to the development of system software like Windows or Linux operating system, but also in the development of GUIs (Graphical User Interfaces) and, IDEs (Integrated Development Environments). It is widely used for writing application software including word processors, spreadsheets, games, robotics, databases and graphics applications.
Variables:
Variables are named storage locations in memory. The primary purpose of variable is to store data (value) for later use. Variable is like a container (storage area) with a name in which we can store data. To indicate the storage area, each variable should be given a unique name. Variable names are just the symbolic representation of a memory location. By using a variable name, we are referring to the data stored in the location.
Rules for Naming a variable in C:
There are some rules on choosing variable names. The following rules must be followed while naming variables:
- A variable name consists of letters (both uppercase and lowercase letters), digits, and the underscore.
- The first character must be a letter or an underscore. It can’t start with a digit (number).
- Variable names are case sensitive. The compiler treats the upper- and lower-case letters as different.
- No whitespace is allowed within the variable name.
- Special symbols such as period, semicolon, comma, slash etc. are not allowed other than underscore.
- Any reserved word (keyword) cannot be used as a variable name. e.g. int, float etc.
Data Types:
A data type refers to the type of data being used. The data type defines an attribute to the variable. It defines the set of legal values that the variable can store. A data type specifies the type of data that a variable can store such as integer, float, character etc.
Following are the primary (basic) data types used in C:
- Integer (int)
- Floating point type (float)
- Character data type (char)
Integer (int):
The data type ‘int’ represents whole numbers with a range of values supported by a particular machine. For instance, in a 16-bit word length machine, the integer values lie between -32768 to 32767.
Example:
int roll;
roll=1;
Floating point (float):
It is used to store floating point number. Floating point numbers are numbers that have a decimal point.
Example:
float a;
a=14.752;
Character data type (char):
It is usually used to store a single character. The char keyword defines a character data type.
Example:
char x;
x= ‘a’;
Type Specifier | Description | Format Specification | Storage (machine dependent) |
int long int | Integer data (whole numbers) | %d or %i %ld | 2 byte 4 byte |
float double | Floating point (Decimal value) | %f or %e %f or %e | 4 byte 8 byte |
char | Character representation String representation | %c %s | 1 byte No of character + 1 byte |
Header Files
Header files act like ‘headers’ to C programs. They are a type of file (with .h extension) that contains function declarations, macro definitions, and other entities which can be used across multiple C source files (.c files).
Here are some of the most frequently used standard library header files:
stdio.h: This is the standard input-output header in C. It contains functions for data input and output, such as printf() for output and scanf() for input.
conio.h: The conio.h header files contain a number of functions, some of which include, clrscr()-used to clear the output screen, getch()-used to hold the screen until the user presses a key.
string.h: This header file is used for manipulating strings. It includes functions like strcpy() (copy a string), strcat() (concatenate strings), and strlen() (calculate string length) etc.
math.h: This header file is used for mathematical computations. It includes a range of functions to perform complex mathematical operations like pow() (power), sqrt() (square root), sin() (sine), and cos() (cosine) etc.
Input and Output Operations:
All input and output operations are carried out through functions such as printf() and scanf(). These functions are collectively known as the standard input and output functions. The stdio.h header file provides functions scanf() for input and printf() for output.
Keywords:
Keywords are reserved words whose meaning has already been fixed to the C compiler. All keywords have a predefined meaning and these meanings cannot be changed. All keywords must be written in lowercase. Since keywords are referred names for a compiler, they cannot be used as variable names because if we do so, we are trying to assign a new meaning to the keyword, which is not allowed. We canoe redefine keywords.
For example:
int age;
Here, int is a keyword that indicates age is a variable of type int (integer).
There are 32 keywords available in C.
auto | break | case | char | const | continue |
default | do | double | else | enum | extern |
float | for | goto | if | int | long |
register | return | short | signed | sizeof | static |
struct | switch | typedef | union | unsigned | void |
volatile | while |
Identifiers:
Identifiers are user defined words. These are used to name the variables, functions, arrays, structures, etc. These names can be in uppercase letters, lowercase letter or a combination. Identifiers must be unique. They are created to give a unique name to an entity to identify it during the execution of the program.
For example:
int age;
int roll;
Here, age and roll are identifiers.
Operators:
An operator can be defined as a symbol that specifies an operation to be performed. Operators help the programmer to perform computation on values.
An operator is a symbol that tells the compiler to perform certain mathematical or logical computations. The data items on which the operators act upon are called operands. Some operators require a single operand to perform operation while others might require two operands.
Categories of operators:
Operators in C can be classified into a number of categories as follows:
Assignment Operator:
An assignment operator is used to assign a value to a variable or to assign the result of an expression to a variable. The = symbol is used as an assignment operator.
The syntax of an assignment is,
Variable name=expression;
For example,
a=5;
Sum=a+b;
Arithmetic Operators:
The arithmetic operators are used to perform mathematical calculations such as addition, subtraction etc. C supports all the common arithmetic operators. These operators can operate on any built –in data types such as int, char and float. However, the modulus operator requires an integral operand (must not be float).
Operator | Meaning |
+ | Addition |
– | Subtraction |
* | Multiplication |
/ | Division |
% | Modulus |
Relational Operators:
These are used to compare the values two variable or constants. The relational operators are symbols that are used to test the relationship between variables or between a variable and a constant.
For example,
(salary==5000)
Here == is the operator that is used to test equality.
There are six relational operators provided by C. They are:
Operator | Meaning |
== | equal to |
!= | not equal to |
> | greater than |
< | less than |
>= | greater than or equal to |
<= | less than or equal to |
Logical Operators:
Logical operators are symbols that are used to combine two or more expressions containing relational operators. The logical operators are used to combine conditions.
Example:
((x>5)&&(x<10))
(Salary>=10000&&salary<=20000)
C has three logical operators:
Operator |
Meaning |
&& |
AND |
|| |
OR |
! |
NOT |
Increment and Decrement Operators:
The increment and decrement operators are two very useful operator used in C.
Increment operators are used to increase the value of a variable by 1. This operator is represented by ++ symbol.
We use decrement operators in C to decrement the given value of a variable by 1. This operator is represented by – – symbol.
Decision making and Branching statement
If statement:
The if statement is one of the most simple decision making statement. It is used to decide whether a particular statement or block of statements will be executed or not. That is, if a certain condition is true then a block of statement is executed otherwise not.
Syntax:
if (test condition)
{
statement-block;
}
Example:
if(marks>=50)
{
printf(“Pass”);
}
If-else statements:
The if…else statement is an extension of the if statement. It is used to perform two operations for a single condition. When condition is true it performs one action by executing a statement or a set of statements. When condition is false it skips that part of statements and executes other statement or set of statements. That is when two choices of actions are required if..else statement is useful.
Syntax:
if (condition)
{
Statement-block 1;
}
else
{
Statement-block 2;
}
Example:
if(marks>=50)
{
printf(“Pass”);
}
else
{
printf(“Fail”);
}
If… else-if …else statements
The if…else-if…else statement is an extension of the if-else statement. It is useful when we need to check multiple conditions within the program. After the first if branch, the program can have many other else if branches depending upon the condition that need to be tested.
Syntax:
if (condition)
{
Statement1;
}
else if (condition)
{
statement2;
}
else if (condition)
{
statement3;
}
else
{
statement4;
}
Example:
if(marks>=80)
{
printf(“Passed: grade A”);
}
else if(marks>=60)
{
printf(“Passed: grade B”);
}
else if(marks>=50)
{
printf(“Passed: grade C”);
}
else
{
printf(“Fail”);
}
Switch…case Statements:
The switch…case statement is used for efficient multi-way branching. It is an alternate to if-else if-else statement. The switch statement tests the value of the expression or variable against a list of case value and when a match is found, a block of statements associated with that case is executed.
The Syntax is shown below:
switch (expression)
{
case value-1:
block-1
break;
case value-2:
block-2;
break;
…….. …..
…….. ….
case value-n:
block-n;
break;
default:
default-block;
}
Looping (C Loops):
Looping is a technique in which a set of statements are repeatedly executed until the condition specified becomes false. So looping is based on a condition. A loop is a repeated (iterated) sequence of statements. The repetition continues while a condition is true. When condition becomes false, the loop ends.
Categories of Loops:
There are three types of loops available in C:
- For loop
- While loop
- Do…While loop
Loops can be classified as Entry Controlled loop and Exit Controlled loop. The For Loop and While Loop entry-controlled loops. In case of entry controlled loop, the condition is tested first. The statements will be executed only if the condition specified is true.
In case of an exit controlled loops, the loop statements are executed first, then the condition will be tested. If the condition is true, again the statements will be executed until the condition becomes false. The loop Do…while is exit controlled loop.
Array:
An array is a collection of similar data elements stored at contiguous memory locations. This data type is useful when a group of elements are to be represented by a common name. An array is used to store a group of data items that belong to the same data type.
An array is a group of elements that share a common name, and is differentiated from one another by their positions within the array. An array is also defined as a collection of homogeneous data.
Structure:
A structure is a heterogeneous set of data items. That is, a structure is a set of data items belonging to different data types. It is a group of variables of different data types referenced by a single name. A structure is a convenient way of grouping several pieces of related information together.
In C programming, structure is a collection of different data items which are referenced by single name. It is also known as user-defined data-type in C. A struct (or structure) is a mixed data type in C. We can use it to store variables of different types.
Functions
A function is a block of codes or a group of statements that together perform a specific task. Every C program must have at least one function, which is the main() function. We can divide our code into separate functions. A program can have any number of functions. The main() function is the starting point of a program.