Notes For C - Unit-1
Notes For C - Unit-1
What is C?
C language is a general purpose and structured pragramming langauge developed by “Dennis Ritchie” at AT &T's Bell
Laboratories in the 1972s in USA.
It is also called as “Procedure oriented programming language.”
C is not specially designed for specific applications areas like COBOL (Common Business Oriented Language) or FORTRAN
(Formula Translation). It is well suited for business and scietific applications. It has some various features like control structures,
looping statements, arrays, macros required for these applications.
The C language has following numorous features as:
1. Portability
2. Flexiblility
3. effectiveness and efficiency
4. Reliability
5. interactivity
Execution of C program:
1. Creating a program :
An editor like notepad or wordpad is used to create a C program. This file contains a source code which consists of executable
code.The file should be saved as “filename.c” extension only.
2. Compiling the program :
The next step is to compile the program. The code is compiled by using compiler. Compiler converts executable code to binary
code i.e. Object code. (cc filename.c or gcc filename.c or gcc -o filename filename.c or F9 dependence on plotform)
3. Linking a program to library :
The object code of a program is linked with libraries that are needed for execution of a program. The linker is used to link
the program with libraries. It creates a file with '*.exe' extension.
4. Execution of program :
The final executable file is then run by dos command prompt or by any other software.
History of C:
The development of C was a cause of evolution of programming languages like Algol 60, CPL (Combined Programming Langauge),
BCPL (Basic Combined Programming Language) and B.
Algol-60 : (1963) :
ALGOL is an acronym for Algorithmic Language. It was the first structured Pointer procedural programming language,
developed in the late 1950s and once widely used in Europe. But it was too abstract and too general structured langauage.
CPL : (1963) :
CPL is an acronym for Combined Programming Language.It was developed at Cambridge University.
BCPL : (1967) :
BCPL is an acronym for Basic Combined Programming Language. It was developed by Martin Richards at Cambridge
University in 1967. BCPL was not so powerful. So, it was failed.
B : (1970) :
B language was developed by Ken Thompson at AT & T Bell Laboratories in 1970. It was machine dependent. So, it leads to
specific problems.
C : (1972) :
'C' Programming Langauage was developed by Dennis Ritchie at AT & T Bell Laboratories in 1972. This is general purpose,
compiled, structured programming langauage. Dennis Ritchie studied the BCPL, then improved and named it as 'C' which is the
second letter of BCPL .
Structure of C Program:
Links Section(File)
Definition Section
void main( ){
executable statements
}
Function definition 1
Function definition 2
-------------------------
-------------------------
-------------------------
Function definition n
Where,
Document Section : It consists of set of comment lines which include name of a program, author name, creation date and other
information.
Links Section (File) : It is used to link the required system libraries or header files to excute a program.
void main() : Used to start of actual C program. It includes two parts as declaration part and executable part.
Function declaration section : Used to declare functions of program from which we get required output. Then, executable
statements are placed for execution.
Function definition section : Used to define functions which are to be called from main().
Character Set:
A character refers to the digit, alphabet or special symbol used to data represetation.
1. Alphabets : A-Z, a-z
2. Digits : 0-9
3. Special Characters :~!@#$%^&*()_+{}[]-<>,./?\|:;"'
4. White Spaces : Horizontal tab, Carriage return, New line, form feed
Identifier :
Identifier is the name of a variable that is made up from combination of alphabets, digits and underscore.
Variable :
It is a data name which is used to store data and may change during program execution. It is opposite to constant. Variable name
is a name given to memory cells location of a computer where data is stored.
1.
First character should be letter or alphabet.
2.
Keywords are not allowed to use as a variable name.
3.
White space is not allowed.
4.
C is case sensitive i.e. UPPER and lower case are significant.
5.
Only underscore, special symbol is allowed between two characters.
6.
The length of indentifier may be upto 31 characters but only the first 8 characters are significant by compiler.
7.
(Note: Some compilers allow variable names whose length may be upto 247 characters.
But, it is recommended to use maximum 31 characters in variable name. Large variable name leads to occur errors.)
Keywords :
Enumeration: Enumeration allows you to define a list of aliases which represent integer numbers. Key word is enum.
Example:
#include <stdio.h>
enum boolean {FALSE=0, TRUE };
enum months {Jan=5, Feb, Mar, Apr, May, Jun, Jul, Aug, Sep, Oct, Nov, Dec};
void main() {
enum months month;
enum boolean mybool;
printf("Month %d\n", month=Aug);
printf("Bool %d\n", mybool=TRUE);
}
Constants in C :
A constant is an entity that doesn't change during the execution of a program.
Followings are the different types of constants :
Real Constant :
1. It must have at least one digit.
2. It must have a decimal point which may be positive or negative.
3. Use of blank space and comma is not allowed between real constants.
4. Example:
+194.143, -416.41
Integer Constant :
1. It must have at least one digit.
2. It should not contain a decimal place.
3. It can be positive or negative.
4. Use of blank space and comma is not allowed between real constants.
5. Example:
1990, 194, -394
Character Constant :
1. It is a single alphabet or a digit or a special symbol enclosed in a single quote.
2. Maximum length of a character constant is 1.
3. Example:
'T', '9', '$'
String Constant :
1. It is collection of characters enclosed in double quotes.
2. It may contain letters, digits, special characters and blank space.
3. Example:
"Technowell Web Solutions, Sangli"
Data Types in C :
"Data type can be defined as the type of data of variable or constant store."
When we use a variable in a program then we have to mention the type of data. This can be handled using data type in C.
Followings are the most commonly used data types in C.
Qualifier :
When qualifier is applied to the data type then it changes its size or its size.
Size qualifiers : short, long
Sign qualifiers : signed, unsigned
Enum Data Type :
This is an user defined data type having finite set of enumeration constants. The keyword 'enum' is used to create enumerated
data type.
Syntax:
enum [data_type] {const1, const2, ...., const n};
Example:
enum mca(software, web, seo);
Typedef :
It is used to create new data type. But it is commonly used to change existing data type with another name.
Syntax:
typedef [data_type] synonym; OR typedef [data_type] new_data_type;
Example:
typedef int integer;
integer rno;
Operators in C :
"Operator is a symbol that is used to perform mathematical operations."
When we use a variable in a program then we have to mention the type of data. This can be handled using data type in C.
Followings are the most commonly used data types in C.
2.Arithemetic Operator:
It is also called as 'Binary operators'. It is used to perform arithmetical operations. These operators operate on two operands.
Example:
#include <stdio.h>
void main()
{
int a,b,c,d,e,f,g;
clrscr();
printf("Enter First Number :"); // 5
scanf("%d",&a);
printf("Enter Second Number :"); // 2
scanf("%d",&b);
c = a + b;
printf("Addition is : %d\n",c); // 7
d = a - b;
printf("Subtraction is : %d\n",d); // 3
e = a * b;
printf("Multiplication is : %d\n",e); // 10
f = a / b;
printf("Division is : %d\n",f); // 2
g = a % b;
printf("Modulus is : %d\n",g); // 1
}
3.Logical Operator :
Sometimes, we have to check more than one condition at a time then it is operator which is primarily used to check more than two
conditions. This operator returns 1 if condition is true otherwise 0. (In C, Every non-zero value is treated as TRUE and zero as
FALSE while in conditions)
Example:
#include <stdio.h>
void main()
{
int no1=2, no2=5;
system(“clear”);
printf("\n %d",(no1 && no2)); // returns 1
printf("\n%d",(no1 || no2)); // returns 1
}
4.Relational Operators:
It is also used to check conditions. These operators return 1 if condition is true otherwise 0.
Example:
#include <stdio.h>
void main()
{
int a=6, b=2;
system(“clear”);
printf("A<=B : %d\n",(a<=b)); // 0 - False
printf("A>B : %d\n",(a>b)); // 1 - True
printf("A!=B : %d\n",(a!=b)); // 1 - True
}
6.Unary Operators:
It operates on a single operand. Therefore, this operator is called as 'unary operator.' It is used to increase or decrease the value of
variable by 1.
Example:
#include <stdio.h>
void main()
{
int a=4, b;
printf("Value of A : %d\n",a); // 4
a++; // Post increment of a
printf("Value of A : %d\n",a); // 5
++a; // Pre increment of a
printf("Value of A : %d\n",a); // 6
b=--a; //Pre decrement of a
printf("Value of A : %d\n",a); // 5
printf("Value of B : %d\n",b); // 5
b=a++;
printf("Value of A : %d\n",a); // 6
printf("Value of B : %d\n",b); // 5
b++;
printf("Value of B : %d\n",b); // 6
}
Note:
Pre increment:First increments the value by 1 and then assigned that value for that operator.
Ex notation: ++a;
Post increment:First assigns the value for that operand and then increments the value by 1.
Ex notation: a++;
Pre decrement:First decremts the value by 1 and then assigned that value for that operand.
Ex notation: --a;
Post decrement:First assigns the value for that operand and then decrements the value by 1.
Ex notation: a--;
Bitwise operators are used for manipulation of data at a bit level. They can be directly applied to char, short int and long.
We have some bitwise operators:
1. Bitwise complement Operator
2. Bitwise OR operator
3. Bitwise XOR operator
4. Bitwise AND operator
5. Bitwise left shift operator
6. Bitwise right shift operator
1.Bitwise complement operator:
This operator gives the complement form of a given number.This operator symble is ~(pronounced as tild).Bitwise
complement operator is nothing but 1's complement or unary bitwise operator.Complement form of a positive number can be
obtained by changing 0's as 1's and vice-versa.
Example:
x=10 ==> ~x=~(00001010)
==> ~x=11110101
2.Bitwise OR operator:
Bitwise OR operator or Bitwise inclusive OR operator performs OR operation on the bits of the numbers.This symble is |
(pronounced as pipe).We know that the truth table for OR gate,the result is false when the both statements are false otherwise
true.
Ex: 9|12...................means 9 Bitwise OR 12
9....................... 0 0 0 0 1 0 0 1
12..................... 0 0 0 0 1 1 0 0
---------------------------
9|12................ 0 0 0 0 1 1 0 1
9....................... 0 0 0 0 1 0 0 1
12..................... 0 0 0 0 1 1 0 0
---------------------------
9^12................ 0 0 0 0 0 1 0 1
9....................... 0 0 0 0 1 0 0 1
12..................... 0 0 0 0 1 1 0 0
---------------------------
9&12................ 0 0 0 0 1 0 0 0
Bitwise left shift operator performs left shift operation on the bits of the given number.This symble is <<(pronounced as
double less than).
If x=9.......................
0 0 0 0 1 0 0 1
x<<3........................
0 1 0 0 1 0 0 0
(fill with zeros)
which is equal to 72.
Note: you can simply calculate the answer like x*pow(2,n).
here is 9<<3....................9*pow(2,3)=9*8=72.
Bitwise right shift operator performs right shift operation on the bits of the given number.This symble is >>(pronounced as
double greater than).
If x=9.......................
0 0 0 0 1 0 0 1
x>>3........................
0 0 0 0 0 0 0 1
(fill with zeros)
which is equal to 1.
Note: you can simply calculate the answer like x/pow(2,n). (i.e.,quotient)
here is 9<<3....................9/pow(2,3)=9/8=1.
If Statement :
This is a conditional statement used in C to check condition or to control the flow of execution of statements. This is also called as
'decision making statement or control statement.' The execution of a whole program is done in one direction only.
Syntax:
if(condition)
{
statements;
}
In above syntax, the condition is checked first. If it is true, then the program control flow goes inside the braces and executes the
block of statements associated with it. If it returns false, then program skips the braces. If there are more than 1 (one) statements
in if statement then use { } braces else it is not necessary to use.
Program:
#include <stdio.h>
#include <conio.h>
void main()
{
int a;
a=5;
if(a>4)
printf("\nValue of A is greater than 4 !");
if(a==4)
printf("\n Value of A is 4 !");
}
Output :
Value of A is greater than 4 !
If-Else Statement :
This is also one of the most useful conditional statement used in C to check conditions.
Syntax:
if(condition)
{
true statements;
}
else
{
false statements;
}
In above syntax, the condition is checked first. If it is true, then the program control flow goes inside the braces and executes the
block of statements associated with it. If it returns false, then it executes the else part of a program.
Program:
#include <stdio.h>
#include <conio.h>
void main()
{
int no;
printf("\n Enter Number :");
scanf("%d",&no);
if(no%2==0)
printf("Number is even !\n");
else
printf("Number is odd !\n");
}
Output :
Enter Number : 11
Number is odd !
While loop :
This is an entry controlled looping statement. It is used to repeat a block of statements until condition becomes true.
Syntax:
while(condition)
{
statements;
increment/decrement;
}
In above syntax, the condition is checked first. If it is true, then the program control flow goes inside the loop and executes the
block of statements associated with it. At the end of loop increment or decrement is done to change in variable value. This process
continues until test condition satisfies.
Program:
#include <stdio.h>
#include <conio.h>
void main()
{
int a;
a=1;
while(a<=5)
{
printf(“\n Hi”);
a+=1; // i.e. a = a + 1
}
}
Output :
Hi
Hi
Hi
Hi
Hi
For loop :
This is an entry controlled looping statement.
In this loop structure, more than one variable can be initilized. One of the most important feature of this loop is that the three
actions can be taken at a time like variable initilisation, condition checking and increment/decrement. The for loop can be more
concise and flexible than that of while and do-while loops.
Syntax:
for(initialisation; test-condition; incre/decre)
{
statements;
}
In above syntax, the given three expressions are seperated by ';' (Semicolon)
Features :
More concise
Easy to use
Highly flexible
More than one variable can be initilized.
More than one increments can be applied.
More than two conditions can be used.
Program:
#include <stdio.h>
#include <conio.h>
void main()
{
int a;
for(i=0; i<5; i++)
{
printf("\nHi");
}
}
Output :
Hi
Hi
Hi
Hi
Hi
Do-While loop :
This is an exit controlled looping statement.
Sometimes, there is need to execute a block of statements first then to check condition. At that time such type of a loop is used. In
this, block of statements are executed first and then condition is checked.
Syntax:
do
{
statements;
(increment/decrement);
}while(condition);
In above syntax, the first the block of statements are executed. At the end of loop, while statement is executed. If the resultant
condition is true then program control goes to evaluate the body of a loop once again. This process continues till condition becomes
true. When it becomes false, then the loop terminates.
Syntax:
break;
Figure:
Program:
#include <stdio.h>
#include <conio.h>
void main()
{
int i;
for(i=1; ; i++)
{
if(i>5)
break;
printf("%d\t",i);
}
}
Output:
1 2 3 4 5
Continue Statement :
Sometimes, it is required to skip a part of a body of loop under specific conditions. So, C supports 'continue' statement to overcome
this anomaly.
The working structure of 'continue' is similar as that of that break statement but difference is that it cannot terminate the loop. It
causes the loop to be continued with next iteration after skipping statements in between. Continue statement simply skipps
statements and continues next iteration.
Syntax :
continue;
Figure:
Program:
#include <stdio.h>
void main()
{
int i;
for(i=1; i<=10; i++)
{
if(i==6)
continue;
printf("\n %d",i);
}
}
Output :
1
2
3
4
5
7
8
9
10
Goto Statement :
It is a well known as 'jumping statement.' It is primarily used to transfer the control of execution to any place in a program. It is
useful to provide branching within a loop.
When the loops are deeply nested at that if an error occurs then it is difficult to get exited from such loops. Simple break statement
cannot work here properly. In this situations, goto statement is used.
Syntax :
goto [expr];
Figure :