0% found this document useful (0 votes)
5 views8 pages

Unit 2

This document provides an overview of basic C programming concepts, focusing on input/output functions like printf() and scanf(), decision-making statements (if, switch), looping statements (while, do-while, for), and arrays (1D, 2D). It includes syntax examples and explanations for each concept, demonstrating how to implement them in C. Additionally, it covers string handling as a special case of character arrays.

Uploaded by

abhisekkoyal334
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views8 pages

Unit 2

This document provides an overview of basic C programming concepts, focusing on input/output functions like printf() and scanf(), decision-making statements (if, switch), looping statements (while, do-while, for), and arrays (1D, 2D). It includes syntax examples and explanations for each concept, demonstrating how to implement them in C. Additionally, it covers string handling as a special case of character arrays.

Uploaded by

abhisekkoyal334
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 8

‘C’ Programming Notes

Working With printf(), scanf()


Introduction
While developing a software it is always needed to send the output to the screen or to
receive or accept from the screen or keyboard. All these operations can be performed
by using the standard input output function like printf( ), scanf( ) etc. These functions
are present in the header file <stdio.h>

printf( ):-It is used to display the data items stored in a variable or a constant on the
screen in a formatted manner. It can also display an ordinary string or message on
the screen.
Syntax : printf (“<control string>”, < ordinary variable>);
“< Control string>”
Control string may be anyone or combination of the followings.
i) Ordinary string
ii) Escape sequence
iii) Format specifier
“ Ordinary string”
It is a collection of some character.
Example- printf (“U.N.College,Adaspur”);
printf (“ Sum=”);
printf (“Enter a no.”);

scanf( )
It is used to enter the data items from the keyboard to the specified variable in a
formatted manner.
Syntax : scanf(“<format specifier>”);
Example : scanf(“%d”, &x);
NOTE
While accepting the data for integer, character and float variable the symbol ‘&’
ampersand should be used before the variable name.

Decision Making Statements


Decision making statements allows the selective execution of statements depending
on the given condition. It enables to decide and to take several possible actions.
These statements are also known as conditional statements or selection statements.
C provides two types of decision making statements. They are –
i) if
ii) switch
if
If statement is a conditional branching statement. It executes a statement or a group
of statement according to the specified condition. This statement has following
structures.
(i) If (ii) if…else
(iii) if ………else if(iv) Nested if
if
It executes a statement or a group of statements if the specified condition returns true
other wise no statements are executed.

Syntax : if (< condition >)


{
< Statements>;
}

Example : if (x<=10)
{
printf (“\n pulsar “);
}
if. …else
In this case if the specified condition returns true then the statements in between if
and else are executed otherwise the statements written under else are executed.
Syntax : if (<condition>)
{
<Statements>;
}
else
{
<Statements>;
}
Example: if (m>100)
{
printf (“good”);
}
else
{
printf (“bad”);
}
If…. else if
In this case if the condition returns true than the statements associated with it are
executed otherwise another if condition is checked. If this conditions returns true than
the statements associated with it are executed otherwise another if condition checked
and so on. If none of the condition are true than the statements under last else
statement are executed.

Syntax: if (< condition>)


{
<Statements>;
}
else
if (< condition >)
{
<Statements>;
}
else
if (< condition >)
{
<Statements>;
}
else
{
<Statement>;
}
Example: int bs= 5000, da;
if (bs>=8000)
{
da = 0.6*bs;
}
else
if (bs>=5000 && <8000)
{
da = 0.45 *bs;
}
else
if (bs>=3000 && bs <5000)
{
da= 0.35*bs;
}
else
{
da=0.25*bs;
}

Nested if
When one if is written inside another if it is known as Nested if. In this if construct
when the condition returns true then another if condition is checked. Again if this
condition returns true than another if condition is checked are so on.

Syntax : if (< condition>)


{
if (<condition>)
{
if (<condition>)
{
if (< condition>)
{
<Statements>;
}
else
{
<Statements>;
}
}
}
}

Example : if (S =’T’)
{
if ( ag>50)
{
if (amt>2000)
{
d=300;
}
else
{
d=200;
}
}
else
{
d=100;
}
}
else
{
d=0;
}
switch
The switch is a multi way branch selection statement. It provides an easy way to
transfer the execution to different parts of the code based on the value of an
expression. It provides a better alternative for a large series of if –else-if statements.

Syntax : switch (<expression>)


{
case< value 1>:
<Statements>;
break;
case<value 2>:
<statements>;
break;
case <value n>:
<statements>;
break;
default:
<Statements>;
}

Example : switch (no)


{
case 1:
printf (“One”);
break;
case 2:
printf (“Two”);
break;
default:
printf (“Others”);
}

Looping Statements
Iteration statements
Iteration statements are those programming statements that allow a set of
instructions to be performed repeatedly until a certain condition is full-filled. The
iteration statements are also called loops or looping statements. “C” provides three
kinds of loops or iteration statements.
(i) While loop
(ii) Do while loop
(iii) For loop
While loop
While loop is C’s most fundamental looping statement. It is an entry-controlled loop. It
executes a statement or group of statements repeatedly until the specified condition
returns false. When the specified condition returns false the program control is
transferred to the statement just after the loop.

Syntax : < initialization>;


while(<condition>)
{
<statements>;
<increment /decrement>;
}
Example : x =1;
while(x<=5)
{
printf (“\n %d”, x);
x=x+1;
}

do … while( )
It is a looping statement. It is used to execute a block of statements between do and
while until the specified condition returns false. In this case first the block of
statements are executed and then the condition is checked. So this loop runs at
least once even though the condition is false.

Syntax : < initialization >;


do
{
< statements >;
< increament / decreament >;
}
while(<condition>);

Example : x =1;
do
{
printf(“\n I love India “);
x = x + 1;
}
while(x<=10);
for( )
It is a looping statement. It executes a block of statement as long as the specified
condition returns true. In this case initialization, condition and increament /
decreament is written in one line.

Syantax : for(<initialization>; <condition>; <increament/decreament>)


{
<statement>;
}

Example : for(x=1; x<=10; x++)


{
printf(“\n my india is best”);
}

ARRAY
Array is a collection of elements having similar data types and represented by a
common name. Each elements of the array is stored in a successive or continuous
location of the main memory. In C array can be divided into three types

1) Single dimensional Array or 1D Array


2) Double dimensional Array or 2D Array
3) Three dimensional Array or 3D Array

Array Elements
Each member of an array is known as array elements, which are assigned by a
unique integer known as index.

0 1 2 3 4 -----------------------> Index
45 6 781 9 48
101 102 104 106 108

Single dimensional array


It has a single subscript. It is otherwise known as vector.

Declaration
Syntax : <data types> <array name>[<no of elements>];

Example : int x [4];


char str[30];
Accessing the array elements
Syntax : <array name> [<element no.>];

Example : x[1]=43;
scanf(“%d”, &x[1]);
printf(“\n Number=%d”, x[1]);

Double Dimensional or 2D Array


A double dimensional or 2D Array is a collection of some 1D array. It has two
subscript values.
1) Row Subscript
2) Column Subscript

A 2D array is otherwise known as matrix.


Declaration
Syntax: <data type> <array name>[<row subscript>][<column subscript>];

Example : int n[3][4];

Accessing 2D array elements


Example: n[1][2];
n[2][3];
n[1][0];
2D array initialization
Like 1D array 2D array can also be initialized during its declaration.
Example : int n[3][3]={
{44, 5, 6},
{2,103,71},
{68, 3,55}
};
int n[3][3]={44,51,6,2,103,71,68,3,55};
int n[ ][3]={44,51,6,2,103,71,68,3,55};
Working with Strings
String is a collection of some characters. In other words it is a single dimensional
array of characters. In C a string always ends with ‘\0’. This character is automatically
put at the end of each string.
String variable Declaration
Syntax : <data type> <array name> [<no. of elements>];
Example : char n[30];

01 2 3 4 5 6
P U L S A R \0

You might also like