0% found this document useful (0 votes)
0 views10 pages

C Language Assignment 2025

The document explains basic data types in C language, including character, integer, and floating-point types, along with their sizes and ranges. It also covers loops, detailing entry and exit controlled loops with examples of for, while, and do-while loops. Additionally, it introduces arrays, specifically single-dimensional arrays, their declaration, initialization, and accessing elements.

Uploaded by

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

C Language Assignment 2025

The document explains basic data types in C language, including character, integer, and floating-point types, along with their sizes and ranges. It also covers loops, detailing entry and exit controlled loops with examples of for, while, and do-while loops. Additionally, it introduces arrays, specifically single-dimensional arrays, their declaration, initialization, and accessing elements.

Uploaded by

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

Q- 1. Explain Basic Types in c language?

Basic Data Types

Character Integer Float

Char Signed Float


Unsigned
Signed Char Double
int int
Unsigned Char Long Double
Short int Short int
Long int Long int
Character type
Character types are used to store characters value. Size and range of Integer type
on 32-bit machine. The following table provides the details of standard character types
with their storage sizes and value ranges
Type Size(bytes) Range Format (% code)
char or signed char 1 -128 to 127 %c
unsigned char 1 0 to 255 %c

Declare variable like - char ans;


Assigning value to character type variable - ans=’y’;

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

Type Size(bytes Range Format (% code)


)
short int / 1 -128 to 127 %d
signed short int
int / signed int 2 -32,768 to 32767 %d
unsigned int 2 0 to 65535 %u
long / 4 -2,147,483,648 to %ld
signed long 2,147,483,647
unsigned long int 4 0 to 4,294,967,295 %lu

Declare variable like To assign or to store integer value in this


variable:
int rollno; rollno=10;
int accno; accno=100;
Floating type
Floating types are used to store real (decimal points value) numbers. Size and
range of Integer type on 32-bit machine. The following table provides the details of
standard floating-point types with storage sizes and value ranges and their precisions.
Type Size (bytes) Range Precision Format (% code)
Float 4 3.4E-38 to 3.4E+38 6 decimal places %f
double 8 1.7E-308 to 1.7E+308 15 decimal places %lf
long double 10 3.4E-4932 to 1.1E+4932 19 decimal places %Lf

Declare variable like To assign or to store some real value


Float per; per=70.20;
Float rate; rate=123.45

Q-2. What is LOOP? Explain Type of loop


with example
A loop is used for executing a block of statements repeatedly until a given
condition returns false
There are mainly two types of loops:
1. Entry Controlled loops: In this type loops, the test condition/expression is tested or
evaluated before entering the loop body. For Loop and While Loop are entry
controlled loops.
2. Exit Controlled Loops: In this type loops, the test condition/expression is tested or
evaluated at the end of loop body. Therefore, the loop body will execute atleast once,
irrespective of whether the test condition is true or false. Do–while loop is exit
controlled loop.
FOR Loop:
A for loop is a repetition control structure which allows us to write a loop that is
executed a specific number of times. The loop enables us to perform n number of steps
together in one line.
SYNTAX FLOWCHART
for (initialization;test-expr;update-expr)
{
// body of the loop
// statements we want to execute
}

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.

Q-3 What is array? Explain Single dimension array


We can use normal variables (v1, v2, v3...) when we have small number of
objects, but if we want to store large number of instances, it becomes difficult to manage
them with normal variables. The idea of array is to represent many instances in one
variable.
Array is a collection or group of similar data type elements stored in
contiguous memory.
Single-dimensional arrays
1. Numeric single Dimensional Array
Instead of declaring individual variables, such as number0, number1, ..., and
number99, you declare one array variable such as numbers and use numbers[0],
numbers[1], and ..., numbers[99] to represent individual variables. A specific element in
an array is accessed by an index.
All arrays consist of contiguous memory locations. The lowest address
corresponds to the first element and the highest address to the last element.

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 −

II. Run time Array initialization


An array can also be initialized at runtime using scanf () function. This approach
is usually used for initializing large array, or to initialize array with user specified values.
Example:
#include<stdio.h>
#include<conio.h>
void main() {
int arr[4];
int i, j;
printf("Enter array element:");
for(i=0; i<4; i++) {
scanf("%d",&arr[i]); //Run time array initialization
}
printf("Value in Array:");
for(j=0; j<4; j++) {
printf("%d \n",arr[j]);
}
getch();
}
Accessing Array Elements
An element is accessed by indexing the array name. This is done by placing the
index of the element within square brackets after the name of the array.
Example: int salary = balance[9];
The above statement will take the 10th element from the array and assign the
value to salary variable. The following example shows how to use all the three above
mentioned concepts viz. declaration, assignment, and accessing arrays
#include <stdio.h>
void main ()
{
int n[ 10 ]; /* n is an array of 10 integers */
int i,j;
for ( i = 0; i < 5; i++ ) /* initialize elements of array n to 0 */
{
n[ i ] = i + 100; /* set element at location i to i + 100 */
}
for (j = 0; j < 5; j++ ) /* output each array element's value */
{
printf("Element[%d] = %d\n", j, n[j] );
}
}
When the above code is compiled and executed, it produces the following result −
Element[0] = 100
Element[1] = 101
Element[2] = 102
Element[3] = 103
Element[4] = 104
STRING
String is a sequence of characters that is treated as a single data item and
terminated by null character '\0'. Remember that C language does not support strings as
a data type. A string is actually one-dimensional array of characters in C language
1. Single Dimension Array (String) Declaration
Declaring a string is as simple as declaring a one dimensional array. Below is the
basic syntax for declaring a string.
Syntax: char str_name[size];
In the above syntax str_name is any name given to the string variable and size is
used define the length of the string, i.e the number of characters strings will store. Please
keep in mind that there is an extra terminating character which is the Null character
(‘\0’) used to indicate termination of string which differ strings from normal
character arrays.
Initializing a String
A string can be initialized in different ways. We will explain this with the help of
an example. Below is an example to declare a string with name as str and initialize it with
“JJKCC”.

 char str[] = "JJKCC";


 char str[5] = "JJKCC";
 char str[] = {'J','J','K','C','C','\0'};
 char str[5] ={'J','J','K','C','C','\0'};

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

What are Pointers?


A pointer is a variable whose value is the
address of another variable,
i.e., direct address of the memory location. Like
any variable or constant, you must declare a pointer
before using it to store any variable address. The
general form of a pointer variable declaration is
type *var-name;
Here, type is the pointer's base type; it must be a valid C data type and var-name is
the name of the pointer variable. The asterisk * used to declare a pointer is the same
asterisk used for multiplication. However, in this statement the asterisk is being used to
designate a variable as a pointer. Take a look at some of the valid pointer declarations −

int *ip; // pointer to an integer


double *dp; // pointer to a double
float *fp; // pointer to a float
char *ch // pointer to a character
The actual data type of the value of all pointers, whether integer, float, character,
or otherwise, is the same, a long hexadecimal number that represents a memory address.
The only difference between pointers of different data types is the data type of the
variable or constant that the pointer points to.

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.

You might also like