C Language Assignment 2025
C Language Assignment 2025
Integer type
Integers are used to store whole numbers. Size and range of Integer type on 32-
bit machine. The following table provides the details of standard integer types with their
storage sizes and value ranges
A loop variable is used to control the loop. First initialize this loop variable to some
value, then check whether this variable is less than or greater than counter value. If
statement is true, then loop body is executed and loop variable gets updated. Steps are
repeated till exit condition comes.
Initialization Expression: In this expression we have to initialize the loop
counter to some value. for example: int i=1;
Test Expression: In this expression we have to test the condition. If the condition
evaluates to true then we will execute the body of loop and go to update
expression otherwise we will exit from the for loop. For example: i <= 10;
Update Expression: After executing loop body this expression increments /
decrements the loop variable by some value. for example: i++ , i--;
EXAMPLE :
int i; int i;
for(i=1; i<=10;i++) for(i=10; i>=1;i--)
printf(“%d”,i); printf(“%d”,i);
Output Output
12345678910 10987654321
That the initialization, testing and incrementation of loop counter is done in the
for statement itself. Instead of i=i+1, the statement i++ or i+=1 can also be used. Since
there is only one statement in the body of the for loop, the pair of braces have been
dropped. The for statement allows for negative increments. This loop is also executed 10
times. But the output would be from 10 to 1 instead of 1 to 10. Note that, braces are
optional when the body of the loop contains only one statement.
We can also write for loop this way.
int i; int i=1;
for(i=1; i<=10;) for(; i<=10;)
{ {
printf(“%d”,i); printf(“%d”,i);
i=i+1; i=i+1;
} }
Output is:12345678910 Output is:12345678910
Here the increment is done within the body of the for loop and not in the for
statement. Note that inspite of this the semicolon after the condition is necessary. In
another example neither initialization, nor the incrementation is done in the for
statement, but still two semicolon are required.
WHILE Loop:
A loop we have seen that the number of iterations is known beforehand, the
number of times the loop body is needed to be executed is known to us.
While loops are used in situations where we do not know the exact number of
iterations of loop beforehand. The loop execution is terminated on the basis of test
condition.
We have already stated that a loop is mainly consisted of three statements-
initialization, test expression, update expression. The syntax of the while
SYNTAX FLOWCHART
Initialization;
while(test expression/condition)
{
Statement 1;
Statement 2;
increment/decrement;
}
EXAMPLE:
Suppose we want to display the consecutive digits 1,2,3,…10. This can be
accomplished with the following program.
#include<stdio.h>
main()
{
int i=1;
while(i<=10)
{
printf(“%d”,i);
i++;
}
}
Output:12345678910
Initially, digit is assigned a value
of 1. The while loop then displays the current value of digit, increases its value by 1 and
then repeats the cycle, until the value of the loop will be repeated by 10 times, resulting
in 10 consecutive numbers of output.
DO WHILE Loop:
In do while loop first the statements in the body are executed then the condition is
checked. If the condition is true then once again statements in the body are executed. This
process keeps repeating until the condition becomes false. As usual, if the body of do
while loop contains only one statement, then braces ({}) can be omitted. Notice that
unlike the while loop, in do while a semicolon (;) is placed after the condition.
The do while loop differs significantly from the while loop because in do while loop
statements in the body are executed at least once even if the condition is false. In the case
of while loop the condition is checked first and if it true only then the statements in the
body of the loop are executed
SYNTAX FLOWCHART
Initialization;
do
{
Statement 1;
Statement 2;
increment/decrement;
} while(test condition);
Example:
Suppose we want to display the consecutive digits 1,2,3,…10. This can be
accomplished with the following program.
#include<stdio.h>
main()
{
int i=1;
do
{
printf(“%d”,i);
i++;
} while(i<=10);
}
Output:12345678910
The digit is initially assigned a value of 1. The do while loop displays the current
value of digit. Increases its value by 1, and then tests to see if the current value of digit
exceeds 10. If so loop terminates; otherwise the loop continues, using the new value of
digit.
Declaring Arrays
To declare an array in C, a programmer specifies the type of the elements and the
number of elements required by an array as follows −
SYNTEX: Data_type ArrayName [ArraySize];
This is called a single-dimensional array. The arraySize must be an integer
constant greater than zero and type can be any valid C data type.
For example, to declare a 10-element array called balance of type int, use this
statement
Example: int balance [10];
ARRAY[INDE
X] a[0] a[1] a[2] a[3] a[4] a[5] a[6] a[7] a[8] a[9]
MEMORY OF 2 2 2 2 2 2 2 2 2 2
ELEMENT Byte Byte Byte Byte Byte Byte Byte Byte Byte Byte
MEMORY 200 200 200 200 200 201 201 201 201 201
ADDDRESS 1 3 5 7 9 1 3 5 7 9
Here balance is a variable array which is sufficient to hold up to 10 integer
numbers.
Initializing Arrays
I. Compile Time Array
Size is Specified Directly
Size is Specified Indirectly
You can initialize an array in C either one by one or using a single statement as
follows
Example: int balance[5] = {100, 200, 300, 400, 500}; // Size is Specified
Directly
The number of values between braces { } cannot be larger than the number of
elements that we declare for the array between square brackets [ ].
If you omit the size of the array, an array just big enough to hold the initialization
is created. Therefore, if you write
Example: int balance[] = {100, 200, 300, 400, 500}; // Size is Specified
Indirectly
You will create exactly the same array as you did in the previous example.
Following is an example to assign a single element of the array
balance[4] = 500;
The above statement assigns the 5th element in the array with a value of 500. All
arrays have 0 as the index of their first element which is also called the base index
and the last index of an array will be total size of the array minus 1. Shown below is the
pictorial representation of the array we discussed above −
Let us now look at a sample program to get a clear understanding of declaring and
initializing a string in C and also how to print a string.
EXAMPLE OUTPUT
#include<stdio.h> JJKCC
void main() {
char str[] = "JJKCC"; // declare and initialize string
printf("%s",str); // print string
}
We can see in the above program that strings can be printed using a normal printf
statements just like we print any other variable. Unlike arrays we do not need to print a
string, character by character. The C language does not provide an inbuilt data type for
strings but it has an access specifier “%s” which can be used to directly print and read
strings. Sample program to read a string from user
EXAMPLE OUTPUT
#include<stdio.h>
void main() {
char str[50]; // declaring string
scanf("%s",str); // reading string
printf("%s",str); // print string
}
You can see in the above program that string can also be read using a single scanf
statement. We know that the ‘&’ sign is used to provide the address of the variable to the
scanf () function to store the value read in memory. As str[] is a character array so using
str without braces ‘[‘ and ‘]’ will give the base address of this string. That’s why we have
not used ‘&’ in this case as we are already providing the base address of the string to
scanf.
Q-4 What is pointer? Explain pointer with array?
Pointers in C are easy and fun to learn. Some C programming tasks are
performed more easily with pointers, and other tasks, such as dynamic memory
allocation, cannot be performed without using pointers. So it becomes necessary to
learn pointers to become a perfect C programmer.
As you know, every variable is a memory location and every memory location
has its address defined which can be accessed using ampersand (&) operator, which
denotes an address in memory
Pointer to Array
When an array is declared, compiler allocates sufficient amount of memory to
contain all the elements of the array. Base address which gives location of the first
element is also allocated by the compiler. Suppose we declare an array arr,
int arr[5]={ 1, 2, 3, 4, 5 };
Assuming that the base address of arr is 1000 and each integer requires two byte,
the five elements will be stored as follows
Here variable arr will give the base address, which is a constant pointer pointing
to the element, arr[0]. Therefore arr is containing the address of arr[0] i.e 1000.
arr is equal to &arr[0] // by default
We can declare a pointer of type int to point to the array arr.
int *p;
p = arr;
or p = &arr[0]; //both the statements are equivalent.
Now we can access every element of array arr using p++ to move from one
element to another. As studied above, we can use a pointer to point to an Array, and then
we can use that pointer to access the array. Let’s have an example,
#include<stdio.h>
void main(){
int i;
int a[5] = {1, 2, 3, 4, 5};
int *p = a; // same as int*p = &a[0]
for (i=0; i<5; i++) {
printf("%d", *p);
p++;
}
getch();}
In the above program, the pointer *p will print all the values stored in the array one by
one. We can also use the Base address (a in above case) to act as pointer and print all the
values.