UNIT I Basics of C Programming
UNIT I Basics of C Programming
The programming language „C‟ was developed in the early 1970s by Dennis Ritchie at Bell
Laboratories. Although C was initially developed for writing system software, today it has
become such a popular language that a variety of software programs are written using this
language. The greatest advantage of using C for programming is that it can be easily used on
different types of computers. Many other programming languages such as C++ and Java are also
based on C which means that you will be able to learn them easily in the future. Today, C is
widely used with the UNIX operating system.
Programming Languages
The purpose of using computers is to solve problems, which are difficult to solve manually. So
the computer user must concentrate on the development of good algorithms for solving
problems. After preparing the algorithms, the programmer or user has to concentrate the
programming language which is understandable by the computer.
Computer programming languages are developed with the primary objectives of facilitating a
large number of people to use computer without the need to know the details of internal structure
of the computer.
csenotescorner.blogspot.com Page 1
CS3251 Programming in C – UNIT I
function is the most important function and is a part of every C program. Rather, the execution of
a C program begins with this function.
From the structure given in Fig. 1.1, we can conclude that a C program can have any number of
functions depending on the tasks that have to be performed, and each function can have any
number of statements arranged according to specific meaningful sequence. Note that
programmers can choose any name for functions. It is not mandatory to write Function1,
Function2, etc., with an exception that every program must contain one function that has its
name as main().
csenotescorner.blogspot.com Page 2
CS3251 Programming in C – UNIT I
1.4 Constants
A constant is an entity whose value can‟t be changed during the execution of a program.
Constants are classified as:
1. Literal constants
csenotescorner.blogspot.com Page 3
CS3251 Programming in C – UNIT I
2. Qualified constants
3. Symbolic constants
csenotescorner.blogspot.com Page 4
CS3251 Programming in C – UNIT I
The rules for writing Floating point Literal constants in an exponential form:
A Floating point Literal constants in an exponential form has two parts: the mantissa
part and the exponent part. Both are separated by e or E.
The mantissa part can be either positive or negative. The default sign is positive.
The mantissa part should have at least one digit.
The mantissa part can have a decimal point.
The exponent part should have at least one digit
The exponent part cannot have a decimal point.
No special characters and blank spaces are allowed.
csenotescorner.blogspot.com Page 5
CS3251 Programming in C – UNIT I
Non-Printable character literal constants are represented with the help of escape sequences. An
escape sequence consists of a backward slash (i.e. \) followed by a character and both enclosed
within single quotes.
1.5 Keywords
Keyword is a reserved word that has a particular meaning in the programming language. The
meaning of a keyword is predefined. It can‟t be used as an identifier.
csenotescorner.blogspot.com Page 6
CS3251 Programming in C – UNIT I
csenotescorner.blogspot.com Page 7
CS3251 Programming in C – UNIT I
sub expression.
1.7 Expressions
An expression is a sequence of operators and operands that specifies computation of a value.
For e.g, a=2+3 is an expression with three operands a,2,3 and 2 operators = & +
csenotescorner.blogspot.com Page 8
CS3251 Programming in C – UNIT I
Classification of Operators
The operators in C are classified on the basis of
1) The number of operands on which an operator operates
2) The role of an operator
Operator Meaning
- Minus
++ Increment
-- Decrement
& Address- of operator
sizeof sizeof operator
2. Binary Operator: A binary operator operates on two operands. Some of the binary
operators are,
Operator Meaning
+ Addition
- Subtraction
* Multiplication
csenotescorner.blogspot.com Page 9
CS3251 Programming in C – UNIT I
/ Division
% Modular
Division
&& Logical AND
3. Ternary Operator
A ternary operator operates on 3 operands. Conditional operator (i.e. ?:) is the ternary operator.
Increment operator
csenotescorner.blogspot.com Page 10
CS3251 Programming in C – UNIT I
Decrement operator
The operator – subtracts one from its operand.
--a or a-- is equivalent to a=a+1
Prefix decrement (--a) operator will decrement the variable BEFORE the
expression is evaluated.
Postfix decrement operator (a--) will decrement AFTER the expression
evaluation.
E.g.
1. c=--a. Assume a=2 so c=1 because the value of a is decremented and then
it is assigned to c.
2. d=b-- Assume b=2 so d=2 because the value of b is assigned to d before it
is decremented.
2. Relational Operators
Relational operators are used to compare two operands. There are 6 relational
operators in C, they are
csenotescorner.blogspot.com Page 11
CS3251 Programming in C – UNIT I
If the relation is true, it returns value 1 and if the relation is false, it returns value
0.
An expression that involves a relational operator is called as a condition. For e.g
a<b is a condition.
3. Logical Operators
Logical operators are used to logically relate the sub-expressions. There are 3
logical operators in C, they are
If the relation is true, it returns value 1 and if the relation is false, it returns value
0.
Operator Meaning of Example
Operator Description
&& Logial AND If c=5 and d=2 then,((c==5) && (d>5)) It returns true when
returns false. both conditions are
true
|| Logical OR If c=5 and d=2 then, ((c==5) || (d>5)) It returns true when
returns true. at-least one of the
condition is true
! Logical NOT If c=5 then, !(c==5) returns false. It reverses the state
of the operand
csenotescorner.blogspot.com Page 12
CS3251 Programming in C – UNIT I
4. Bitwise Operators
C language provides 6 operators for bit manipulation. Bitwise operator operates on the
individual bits of the operands. They are used for bit manipulation.
Operators Meaning of operators
& Bitwise AND
| Bitwise OR
^ Bitwise exclusive OR
~ Bitwise complement
<< Shift left
>> Shift right
Truth tables
csenotescorner.blogspot.com Page 13
CS3251 Programming in C – UNIT I
00001100
& 00011001
5. Assignment Operators
To assign a value to the variable assignment operator is used.
Operators Example Explanation
Simple assignment operator = sum=10 10 is assigned to variable sum
+= sum+=10 This is same as sum=sum+10
-= sum-=10 sum = sum-10
csenotescorner.blogspot.com Page 14
CS3251 Programming in C – UNIT I
E.g.
var=5 //5 is assigned to var
a=c; //value of c is assigned to a
6. Miscellaneous Operators
Other operators available in C are
a. Function Call Operator(i.e. ( ) )
b. Array subscript Operator(i.e [ ])
c. Member Select Operator
i. Direct member access operator (i.e. . dot operator)
ii. Indirect member access operator (i.e. -> arrow operator)
d. Conditional operator (?:)
e. Comma operator (,)
f. sizeof operator
g. Address-of operator (&)
a) Comma operator
It is used to join multiple expressions together.
E.g.
int i , j;
i=(j=10,j+20);
In the above declaration, right hand side consists of two expressions separated by
comma. The first expression is j=10 and second is j+20. These expressions are evaluated from
left to right. ie first the value 10 is assigned to j and then expression j+20 is evaluated so the final
value of i will be 30.
b) sizeof Operator
The sizeof operator returns the size of its operand in bytes.
Example : size of (char) will give us 1.
sizeof(a), where a is integer, will return 2.
c) Conditional Operator
csenotescorner.blogspot.com Page 15
CS3251 Programming in C – UNIT I
Syntax :
(Condition? true_value:
false_value); For example:
c=(c>0)?10:-10;
If c is greater than 0, value of c will be 10 but, if c is less than 0, value of c will be -10.
d) Address-of Operator
It is used to find the address of a data object.
Syntax
&operand
E.g.
&a will give address of a.
csenotescorner.blogspot.com Page 16
CS3251 Programming in C – UNIT I
printf() getch()
getche()
getchar()
scanf()
gets()
putch()
putchar()
puts()
eg:
char c;
c=getchar();
csenotescorner.blogspot.com Page 17
CS3251 Programming in C – UNIT I
eg
char c=„C‟;
putchar(c);
Example Program
#include <stdio.h>
main()
{
int i;
char ch;
ch = getchar();
putchar(ch);
}
Output:
A
A
3. getch() and getche() : getch() accepts only a single character from keyboard. The character
entered through getch() is not displayed in the screen (monitor).
Syntax
variable_name = getch();
Like getch(), getche() also accepts only single character, but getche() displays the entered
character in the screen.
Syntax
variable_name = getche();
4 putch(): putch displays any alphanumeric characters to the standard output device. It displays
only one character at a time.
Syntax
putch(variable_name);
b) String I/O
csenotescorner.blogspot.com Page 18
CS3251 Programming in C – UNIT I
1. gets() – This function is used for accepting any string through stdin (keyboard) until enter key
is pressed.
Syntax
gets(variable_name);
puts(variable_name);
cputs(char *st);
Example Program
void main()
{
char ch[30];
clrscr();
printf(“Enter the String : “);
gets(ch);
puts(“\n Entered String : %s”,ch);
puts(ch);
}
Output:
Enter the String : WELCOME
csenotescorner.blogspot.com Page 19
CS3251 Programming in C – UNIT I
Width Modifier: It specifies the total number of characters used to display the value.
Precision: It specifies the number of characters used after the decimal point.
E.g: printf(“ Number=%7.2\n”,5.4321);
Width=7
Precesion=2
Output: Number = 5.43(3 spaces added in front of 5)
Flag: It is used to specify one or more print modifications.
csenotescorner.blogspot.com Page 20
CS3251 Programming in C – UNIT I
Flag Meaning
- Left justify the display
+ Display +Ve or –Ve sign of value
E.g:
Space Display space if there is no sign
0 Pad with leading 0s
printf(“Number=%07.2\n”,5.4321)
Output: Number=0005.43
3. Control Codes
They are also known as Escape Sequences.
E.g:
csenotescorner.blogspot.com Page 21
CS3251 Programming in C – UNIT I
csenotescorner.blogspot.com Page 22
CS3251 Programming in C – UNIT I
Flow of control: The order in which the program statements are executed is known as flow of
control. By default, statements in a c program are executed in a sequential order
Branching statements
Branching statements are used to transfer program control from one point to another.
2 types
a) Conditional Branching:- Program control is transferred from one point to another based
on the result of some condition
Eg) if, if-else, switch
b) Unconditional Branching:- Pprogram control is transferred from one point to another
without checking any condition
Eg) goto, break, continue, return
Statement
T
csenotescorner.blogspot.com Page 23
CS3251 Programming in C – UNIT I
Example:
#include <stdio.h>
void main()
{
int n;
clrscr();
printf(“enter the number:”);
scanf(“%d”,&n);
if(n>0)
printf(“the number is positive”);
getch();
}
Output:
enter the number:50
the number is positive
if (test expression)
Statement T; Stateme Stateme
else T F
Statement F;
csenotescorner.blogspot.com Page 24
CS3251 Programming in C – UNIT I
#include <stdio.h>
void main(){
int n,r;
clrscr();
printf(“Enter a number:”);
scanf(“%d”,&n);
r=n%2;
if(r==0)
printf(“The number is even”);
else
printf(“The number is odd”);
getch();
}
Output:
Enter a number:15
The number is odd
csenotescorner.blogspot.com Page 25
CS3251 Programming in C – UNIT I
Enter a and b: 2 4
a and b are not equal
(iii) Nested if statement
If the body the if statement contains another if statement, then it is known as nested if
statement
Syntax:
csenotescorner.blogspot.com Page 26
CS3251 Programming in C – UNIT I
{
statement 3;
if (condition)
statementnest;
statement 4;
}
Example:
Program for finding greatest of 3 numbers
#include<stdio.h>
void main()
{
int a,b,c;
printf(“Enter three numbers\n”);
scanf(“%d%d%d”,&a,&b,&c);
if(a>b)
{
if(a>c)
{
printf(“a is greatest”);
}
}
else
{ if(b>c)
{
printf(“b is greatest”);
}
else
{
printf(“C is greatest”);
}
}
csenotescorner.blogspot.com Page 27
CS8251 Programming in C – UNIT I
}
Output
Enter three numbers 2 4 6
C is greatest
v) Switch statement
It is a multi way branch statement.
It provides an easy & organized way to select among multiple operations depending upon
some condition.
Execution
1. Switch expression is evaluated.
2. The result is compared with all the cases.
3. If one of the cases is matched, then all the statements after that matched case gets executed.
4. If no match occurs, then all the statements after the default statement get executed.
Switch ,case, break and default are keywords
Break statement is used to exit from the current case structure
Flowchart
switch(expression)
case : break
statement
constant
1
csenotescorner.blogspot.com Page 32
CS8251 Programming in C – UNIT I
End of switch
Syntax
switch(expression)
{
case value 1:
program
statement;
program
statement;
case value 2: …… break;
program
statement;
Program
statement;
… …… break;
case value n:
program
Exampl statement;
program
void maie1 statement;
default: ……
{ n() program
break;
statement;
char ch;
} program
statement;
printf(“Enter a character\n”);
scanf(“%c”,&ch);
switch(ch)
{
case „A‟:
printf(“you entered an A\n”);
break;
case „B‟:
printf(“you entered a B\n”);
break;
default:
printf(“Illegal entry”);
break;
}
getch();
csenotescorner.blogspot.com Page 33
CS8251 Programming in C – UNIT I
}
Output
Enter a character
A
You entered an A
Example2
void main()
{
int n;
printf(“Enter a number\n”);
scanf(“%d”,&n); switch(n
%2)
{
case 0:printf(“EVEN\n”);
break;
case 1:printf(“ODD\n”);
break;
}
getch();
}
Output:
Enter a number
5
ODD
b)Unconditional branching
statements i)The goto Statement
This statement does not require any condition. Here program control is transferred to
another part of the program without testing any condition.
csenotescorner.blogspot.com Page 34
CS8251 Programming in C – UNIT I
Syntax:
goto label;
Example:1
#include<stdio.h>
void main()
{
csenotescorner.blogspot.com Page 35
CS8251 Programming in C – UNIT I
int c=1;
while(c<=5)
{
if (c==3)
break;
printf(“\t %d”,c);
c++;
}
}
Output : 1 2
Example :2
#include<stdio.h>
void main()
{
int i;
for(i=0;i<=10;i++)
{
if (i==5)
break;
printf(“ %d”,i);
}
}
Output :1 2 3 4
iii) continue statement
A continue statement can appear only inside a loop.
A continue statement terminates the current iteration of the nearest enclosing
loop.
Syntax:
continue;
csenotescorner.blogspot.com Page 36
CS8251 Programming in C – UNIT I
Example :1
#include<stdio.h>
void main()
{
int c=1;
while(c<=5)
{
if (c==3)
continue;
printf(“\t %d”,c);
c++;
}
}
Output : 1 2 4 5
Example :2
#include<stdio.h>
main()
{
int i;
for(i=0;i<=10;i++)
{
if (i==5)
continue;
printf(“ %d”,i);
}
}
Output :1 2 3 4 6 7 8 9 10
iv) return statement:
A return statement terminates the execution of a function and returns the control to the
calling function.
csenotescorner.blogspot.com Page 37
CS8251 Programming in C – UNIT I
Syntax:
return;
(or)
return expression;
for(initialization;condition2;incrementing/updating)
{
csenotescorner.blogspot.com Page 38
Statements;
}
CS8251 Programming in C – UNIT I
csenotescorner.blogspot.com Page 39
CS8251 Programming in C – UNIT I
2. while statement
They are also known as Entry controlled loops because here the condition is checked
before the execution of loop body.
Syntax:
while (expression)
{
statements;
}
csenotescorner.blogspot.com Page 40
CS8251 Programming in C – UNIT I
csenotescorner.blogspot.com Page 41
CS8251 Programming in C – UNIT I
i=i+1;
}
printf(“Sum=%d”, sum);
getch();
}
Output:
Enter the value for n
4
Sum=10
3. do while statement
They are also known as Exit controlled loops because here the condition is checked after
the execution of loop body.
Syntax:
do
{
statements;
}
while(expression);
The body of do-while is executed once, even when the condition is initially false.
csenotescorner.blogspot.com Page 42
CS8251 Programming in C – UNIT I
{
int i=1;
clrscr();
do
{
printf (“%d”,i);
i=i+1;
} while (num<=10);
getch();
}
Output:
1 2 3 4 5 6 7 8 9 10
csenotescorner.blogspot.com Page 43
CS8251 Programming in C – UNIT I
Sum=10
Nested loops
If the body of a loop contains another iteration statement, then we say that the loops are
nested.
Loops within a loop are known as nested loop.
Syntax
while(condition)
{
while(condition)
{
Statements;
}
Statements;
}
csenotescorner.blogspot.com Page 44