You are on page 1of 20

1. What is structure? Write a program to readard display the information.( Name, roll no, DOB, and Ph no.

) Of all
the students in the class.

In programming, a structure is a composite data type that groups together variables of different data types under a
single name. These variables are called members of the structure. Here is an example program in C that reads and
displays the information of students in a class:
#include <stdio.h>

struct student {
char name[50];
int roll;

char dob[10];
char phone[10];
};

int main() {
int i;
int n = 3;
struct student s[n];

printf("Enter information of students:\n");

for (i = 0; i < n; ++i) {


s[i].roll = i + 1;
printf("\nFor roll number %d,\n", s[i].roll);
printf("Enter name: ");
scanf("%s", s[i].name);
printf("Enter date of birth: ");
scanf("%s", s[i].dob);
printf("Enter phone number: ");
scanf("%s", s[i].phone);
}

printf("\nDisplaying Information:\n");

for (i = 0; i < n; ++i) {


printf("\nRoll number: %d\n", i + 1);
printf("Name: ");
puts(s[i].name);
printf("Date of birth: %s\n", s[i].dob);
printf("Phone number: %s\n", s[i].phone);
}
return 0;
}
This program defines a structure named student with four members: name, roll, dob, and phone. The program
then reads the information of n students and stores it in an array of structures. Finally, the program displays the
information of all the students.

2. Find the difference between f puts and f printf and use this function in a program.

fputs and fprintf are both functions in the C programming language that can be used to write data to a
file. The main difference between the two is that fputs writes a string to a file, while fprintf can write
multiple data types to a file using formatting.

Here is an example program that demonstrates the use of both functions:


#include <stdio.h>

int main() {
FILE *fptr;
char str[100];
int i;

fptr = fopen("file.txt", "w");


if (fptr == NULL) {
printf("Error opening file!");
return 1;
}

printf("Enter a string: ");


fgets(str, sizeof(str), stdin);

fputs(str, fptr);

printf("Enter an integer: ");


scanf("%d", &i);
fprintf(fptr, "%d", i);
fclose(fptr);

return 0;
}

In this program, the user is prompted to enter a string and an integer. The string is written to the file
using fputs, while the integer is written to the file using fprintf. The fputs function takes two arguments:
the string to be written and the file pointer. The fprintf function takes at least two arguments: the file pointer
and a format string. Additional arguments may be required depending on the format string.

3. Find the difference between f gets and f scanf and use this function in program.

fgets and fscanf are both functions in the C programming language that can be used to read data from a file.
The main difference between the two is that fgets reads a string from a file, while fscanf can read multiple data
types from a file using formatting.

Here is an example program that demonstrates the use of both functions:


#include <stdio.h>
int main() {
FILE *fptr;
char str[100];
int i;

fptr = fopen("file.txt", "r");


if (fptr == NULL) {
printf("Error opening file!");
return 1;
}

fgets(str, sizeof(str), fptr);


printf("String: %s", str);

fscanf(fptr, "%d", &i);


printf("Integer: %d", i);

fclose(fptr);

return 0;
}
In this program, a string and an integer are read from a file. The string is read using fgets, while the integer is read
using fscanf. The fgets function takes three arguments: a character array to store the string, the maximum
number of characters to read, and the file pointer. The fscanf function takes at least two arguments: the file
pointer and a format string. Additional arguments may be required depending on the format string.

4. Mention different between pre increament and post increament with suitable examples.

In C programming, the increment operator ++ can be used in two ways: as a prefix (++x) or as a postfix (x++). The
difference between the two is in the order in which the increment operation is performed and the value of the
expression is evaluated.

Here is an example that demonstrates the difference between pre-increment and post-increment:
#include <stdio.h>

int main() {
int x = 5;
int y;

y = ++x;
printf("Pre-increment: x = %d, y = %d\n", x, y);

x = 5;
y = x++;
printf("Post-increment: x = %d, y = %d\n", x, y);

return 0;
}
In this program, the variable x is initialized to 5. In the first case, the pre-increment operator is used. This means that
the value of x is incremented first and then assigned to y. As a result, both x and y have the value 6.
In the second case, the post-increment operator is used. This means that the value of x is assigned to y first and
then incremented. As a result, y has the value 5, while x has the value 6.

5. Differentiate auto and static classes with example.

In C programming, auto and static are storage classes that determine the lifetime and visibility of a variable.
The auto storage class is the default for local variables and specifies that the variable has automatic storage
duration. This means that the variable is allocated on the stack and is deallocated when the block in which it is
defined exits.
The static storage class, on the other hand, specifies that the variable has static storage duration. This means that
the variable is allocated in static memory and is initialized only once, at compile time. The variable retains its value
between function calls.
Here is an example that demonstrates the difference between auto and static variables:
#include <stdio.h>

void func() {
auto int x = 0;
static int y = 0;

printf("x = %d, y = %d\n", x++, y++);


}

int main() {
int i;

for (i = 0; i < 5; ++i) {


func();
}

return 0;
}
In this program, the function func defines two local variables: x with the auto storage class and y with
the static storage class. The function is called five times in a loop. Each time the function is called, the value of x is
reset to 0, while the value of y is retained from the previous call.

6. Explain the basic string standard function with proper example.

In C programming, the string.h library provides several standard functions for manipulating strings. Here are some of
the most commonly used string functions with examples:

 strlen: This function returns the length of a string (not including the null character). For example:

#include <stdio.h>
#include <string.h>
int main() {
char str[] = "Hello, world!";
printf("Length: %lu\n", strlen(str));
return 0;
}

This program outputs the length of the string str, which is 13.

 strcpy: This function copies a string from one location to another. For example:

#include <stdio.h>
#include <string.h>

int main() {
char src[] = "Hello, world!";
char dest[20];

strcpy(dest, src);
printf("Destination: %s\n", dest);

return 0;
}

This program copies the string src to the array dest using the strcpy function.

 strcmp: This function compares two strings and returns an integer indicating their lexicographic order. For example:

#include <stdio.h>
#include <string.h>

int main() {
char str1[] = "abc";
char str2[] = "def";

int result = strcmp(str1, str2);

if (result < 0) {
printf("%s comes before %s\n", str1, str2);
} else if (result > 0) {
printf("%s comes after %s\n", str1, str2);
} else {
printf("%s and %s are equal\n", str1, str2);
}

return 0;
}
This program compares the strings str1 and str2 using the strcmp function and outputs their lexicographic
order.

7. Write a c program to insert same integer values in an array and then print the avarage of them.

Here is a C program that reads integer values from the user, stores them in an array, and then calculates and prints
the average of the values:
#include <stdio.h>

int main() {
int n, i;
int sum = 0;
double avg;

printf("Enter the number of elements: ");


scanf("%d", &n);

int arr[n];

printf("Enter %d integer values:\n", n);


for (i = 0; i < n; ++i) {
scanf("%d", &arr[i]);
sum += arr[i];
}

avg = (double)sum / n;
printf("Average: %.2f\n", avg);

return 0;
}
In this program, the user is prompted to enter the number of elements to be stored in the array. The program then
reads n integer values from the user and stores them in the array arr. The sum of the values is calculated using a
loop and then used to compute the average. Finally, the average is printed to the screen.

8. Write a C program to find out maximum and minimum number out of three integer values by using conditional
operator.

Here is a C program that finds the maximum and minimum out of three integer values using the conditional
operator:
#include <stdio.h>

int main()
{
int a, b, c, max, min;

printf("Enter three integers: ");


scanf("%d %d %d", &a, &b, &c);
max = (a > b) ? ((a > c) ? a : c) : ((b > c) ? b : c);
min = (a < b) ? ((a < c) ? a : c) : ((b < c) ? b : c);

printf("Maximum: %d\n", max);


printf("Minimum: %d\n", min);

return 0;
}

This program prompts the user to enter three integers. It then uses the conditional operator to find the maximum
and minimum values among the three integers and prints them to the screen.

9. Define Recursion. Write a C program to find out Greatest Common Divisor between two integer values by using
Recursion.

Recursion is a programming technique where a function calls itself repeatedly until a certain condition is met. It can
be used to solve problems that can be broken down into smaller sub-problems of the same type.

Here is a C program that finds the Greatest Common Divisor (GCD) between two integer values using recursion:
#include <stdio.h>

int gcd(int a, int b)


{
if (b == 0)
return a;
else
return gcd(b, a % b);
}

int main()
{
int a, b;

printf("Enter two integers: ");


scanf("%d %d", &a, &b);

printf("GCD: %d\n", gcd(a, b));

return 0;
}
This program defines a recursive function gcd that takes two integer arguments a and b. The function
returns a if b is 0. Otherwise, it calls itself with arguments b and a % b. The program prompts the user to enter two
integers and then uses the gcd function to find their GCD and prints it to the screen.

10. Write a C program to print the following pattern.


1
123
12345
Here is a C program that prints the pattern you specified:
#include <stdio.h>

int main()
{
int i, j;

for (i = 1; i <= 3; i++)


{
for (j = 1; j <= 3 - i; j++)
printf(" ");
for (j = 1; j <= 2 * i - 1; j++)
printf("%d", j);
printf("\n");
}

return 0;
}

This program uses two nested loops to print the pattern. The outer loop iterates over the rows of the pattern while
the inner loops iterate over the columns. The first inner loop prints spaces to align the numbers in each row. The
second inner loop prints the numbers in each row.

The output of this program is:


1
123
12345

11. Discuss output difference between ‘Call by Value’ & ‘Call by Reference’ for swapping two integer numbers.

In programming languages such as C++, there are two common methods for passing parameters to functions: “call
by value” and “call by reference.” The main difference between the two lies in how changes to the parameters inside
the function affect the original variables in the calling code. Let’s discuss the output difference between “call by
value” and “call by reference” for swapping two integer numbers.

Call by Value: In the “call by value” method, the values of the variables are passed to the function, creating local
copies of those values. Any modifications made to the parameters inside the function do not affect the original
variables in the calling code.
void swapByValue(int a, int b) {
int temp = a;
a = b;
b = temp;
}

int main() {
int x = 10;
int y = 20;
swapByValue(x, y);

cout << "x: " << x << endl; // Output: x: 10


cout << "y: " << y << endl; // Output: y: 20

return 0;
}
In this case, when the swapByValue function is called, it creates local copies of x and y. Swapping these values
inside the function doesn’t affect the original variables in main(). Therefore, the output remains unchanged, and
the values of x and y are still 10 and 20, respectively.

Call by Reference: In the “call by reference” method, the memory addresses of the variables are passed to the
function. This allows the function to directly access and modify the original variables in the calling code.
void swapByReference(int& a, int& b) {
int temp = a;
a = b;
b = temp;
}

int main() {
int x = 10;
int y = 20;

swapByReference(x, y);

cout << "x: " << x << endl; // Output: x: 20


cout << "y: " << y << endl; // Output: y: 10

return 0;
}
In this case, when the swapByReference function is called, it receives references to x and y. The values
of x and y can be modified directly within the function, resulting in a swap of their values. Consequently, the output
in main() shows that x is now 20 and y is 10.

In summary, when using “call by value,” the original variables remain unchanged as only copies of the values are
manipulated inside the function. On the other hand, “call by reference” allows modifications to the original variables
by passing their memory addresses, leading to the desired swap.

12. Define Pointer. Write a program in C to get reverse of a string using pointers.

A pointer is a variable that stores the memory address of another variable. Pointers allow for indirect access to the
value of a variable by using its memory address. In C, pointers are declared using the * operator and the memory
address of a variable can be obtained using the & operator.

Here is a C program that reverses a string using pointers:


#include <stdio.h>
#include <string.h>
void reverse(char *str)
{
char *end = str + strlen(str) - 1;
char temp;

while (end > str)


{
temp = *str;
*str = *end;
*end = temp;

str++;
end--;
}
}

int main()
{
char str[] = "Hello, World!";

printf("Original: %s\n", str);

reverse(str);

printf("Reversed: %s\n", str);

return 0;
}
This program defines a function reverse that takes a pointer to a char as an argument. The function uses two
pointers str and end to traverse the string from both ends, swapping characters until the middle of the string is
reached.
The program initializes a char array str with the string “Hello, World!”. It then calls the reverse function
with str as an argument and prints the original and reversed strings to the screen.

The output of this program is:


Original: Hello, World!
Reversed: !dlroW ,olleH

13. Write a short note on ‘Relational Operators’ & ‘Logical Operators’ in C.

Relational operators and logical operators are used in C to form conditional expressions that evaluate to either true
or false.

Relational Operators: Relational operators are used to compare two values and return a boolean result. The
relational operators in C are:

 == (equal to)
 != (not equal to)
 > (greater than)
 < (less than)
 >= (greater than or equal to)
 <= (less than or equal to)

For example, the expression x == y returns true if the value of x is equal to the value of y, and false otherwise.

Logical Operators: Logical operators are used to combine multiple conditional expressions and return a boolean
result. The logical operators in C are:

 && (logical AND)
 || (logical OR)
 ! (logical NOT)

For example, the expression (x > 0) && (y > 0) returns true if both x and y are greater than 0, and false
otherwise.

In summary, relational operators and logical operators are used in C to form conditional expressions that evaluate to
either true or false. Relational operators compare two values while logical operators combine multiple conditional
expressions.

14. Distinguish Program & Algorithm.

Sure! Here is a table that distinguishes between a program and an algorithm:

Program Algorithm

A concrete implementation of an A well-defined sequence of steps or


algorithm in a specific programming instructions for solving a specific problem
language. or performing a specific task.

Consists of a set of instructions written Can be expressed in natural language,


in a programming language that can be pseudocode, or any other form that clearly
executed by a computer. describes the steps to be followed.

Dependent on the programming An abstract concept that is independent of


language and the platform on which it any specific programming language or
is executed. implementation.

In summary, an algorithm is an abstract sequence of steps for solving a problem or performing a task, while a
program is a concrete implementation of an algorithm in a specific programming language.

15. List the Arithmetic Operators in C.

In C, arithmetic operators are used to perform mathematical operations on numeric values. The arithmetic operators
in C are:
 + (Addition): Adds two values. For example, x + y.
 - (Subtraction): Subtracts the second value from the first. For example, x - y.
 * (Multiplication): Multiplies two values. For example, x * y.
 / (Division): Divides the first value by the second. For example, x / y.
 % (Modulus): Returns the remainder of the division of the first value by the second. For example, x % y.
 ++ (Increment): Increases the value of a variable by 1. For example, x++.
 -- (Decrement): Decreases the value of a variable by 1. For example, x--.

These operators can be used to perform arithmetic operations on numeric values in C.


Received message.

16. Differentiate ‘struct’ and ‘union’. Explain with example.

Difference between struct and union in C:

struct union

A composite data type that groups


Similar to a struct in that it groups
together variables of different data
together variables of different data types
types under a single name.
under a single name.

Each member has its own memory All members share the same memory
location and can be accessed and location and at any given time, a union can
manipulated independently. only hold a value for one of its members.

Used to represent objects that have Used to represent objects that can have
multiple properties or attributes. multiple interpretations or representations.

Here are examples that demonstrate the use of struct and union in C:

struct: A struct (short for structure) is a composite data type that groups together variables of different data types
under a single name. Each member of a struct has its own memory location and can be accessed and manipulated
independently.

Here is an example that demonstrates the use of struct in C:


#include <stdio.h>

struct Point
{
int x;
int y;
};

int main()
{
struct Point p1 = {10, 20};

printf("p1.x = %d\n", p1.x);


printf("p1.y = %d\n", p1.y);

return 0;
}

This program defines a struct named Point that has two members: x and y of type int. The program initializes a
variable p1 of type struct Point with values 10 and 20 for its members x and y, respectively. It then prints the
values of the members of p1 to the screen.

The output of this program is:


p1.x = 10
p1.y = 20

union: A union is similar to a struct in that it groups together variables of different data types under a single
name. However, unlike a struct, where each member has its own memory location, all members of a union share
the same memory location. This means that at any given time, a union can only hold a value for one of its
members.

Here is an example that demonstrates the use of union in C:


#include <stdio.h>

union Data
{
int i;
float f;
char c;
};

int main()
{
union Data d;

d.i = 10;
printf("d.i = %d\n", d.i);

d.f = 20.5;
printf("d.f = %.1f\n", d.f);

d.c = 'A';
printf("d.c = %c\n", d.c);

return 0;
}

This program defines a union named Data that has three members: i of type int, f of type float, and c of
type char. The program initializes a variable d of type union Data and assigns values to its members one by one.
It then prints the values of the members of d to the screen.

The output of this program is:


d.i = 10
d.f = 20.5
d.c = A

17. What is Pointer? Give example.

A pointer is a variable that stores the memory address of another variable. Pointers allow for indirect access to the
value of a variable by using its memory address. In C, pointers are declared using the * operator and the memory
address of a variable can be obtained using the & operator.

Here is an example that demonstrates the use of pointers in C:


#include <stdio.h>

int main()
{
int x = 10;
int *p;

p = &x;

printf("Value of x: %d\n", x);


printf("Address of x: %p\n", &x);
printf("Value of p: %p\n", p);
printf("Value pointed by p: %d\n", *p);

return 0;
}
This program declares an int variable x and initializes it with the value 10. It also declares a pointer
to int named p. The program assigns the memory address of x to p using the address-of operator &. It then prints
the value and address of x, the value of p, and the value pointed by p to the screen.

The output of this program is:


Value of x: 10
Address of x: 0x7ffc2b8e2b3c
Value of p: 0x7ffc2b8e2b3c
Value pointed by p: 10
In this example, the pointer p stores the memory address of the variable x. The value of x can be accessed indirectly
using the pointer p by dereferencing it using the * operator.

18. Show the different if control statement with an example for each.

The response is derived from web search results.

In C, there are three types of if control statements:

 if statement: It executes a block of code if a condition is true, and skips it otherwise. The syntax is:

if (condition) {
// statements
}

For example:
if (x > 0) {
printf("x is positive\n");
}

 if-else statement: It executes a block of code if a condition is true, and another block of code if the condition is
false. The syntax is:

if (condition) {
// statements
} else {
// statements
}

For example:
if (x % 2 == 0) {
printf("x is even\n");
} else {
printf("x is odd\n");
}

 if-else-if ladder: It checks multiple conditions and executes a block of code for the first condition that is true. If
none of the conditions are true, it executes an optional else block at the end. The syntax is:

if (condition1) {
// statements
} else if (condition2) {
// statements
} else if (condition3) {
// statements
} ...
else {
// statements
}

For example:
if (x > 0) {
printf("x is positive\n");
} else if (x < 0) {
printf("x is negative\n");
} else {
printf("x is zero\n");
}

19. Explain entry control loop and exit control loop with an example.
An entry control loop checks the condition at the beginning of the loop before executing the loop body. If the
condition is true, the loop body is executed; otherwise, the loop is terminated. Examples of entry control loops
include for and while loops1.
On the other hand, an exit control loop first executes the loop body and then checks the condition at the end of the
loop. The loop body will be executed at least once, irrespective of whether the condition is true or false. An example
of an exit control loop is a do-while loop1.

Here’s an example in C:
#include <stdio.h>

int main() {
int i = 0;

// Entry control loop


while (i < 5) {
printf("Entry control loop: %d\n", i);
i++;
}

i = 0;

// Exit control loop


do {
printf("Exit control loop: %d\n", i);
i++;
} while (i < 5);

return 0;
}
In this example, the while loop is an entry control loop that checks if i is less than 5 before executing the loop
body. The do-while loop is an exit control loop that executes the loop body first and then checks if i is less than 5
at the end of the loop.

20. What is sizeof operator? Differentiate between getchar() and getch() functions.

The sizeof operator is a compile-time unary operator that can be used to compute the size of its operand. The
result of sizeof is of the unsigned integral type which is usually denoted by size_the sizeof can be applied to any
data type, including primitive types such as integer and floating-point types, pointer types, or compound datatypes
such as structures and unions.

Here’s a comparison between getchar() and getch() functions:

Function Description

A standard function that reads a single character from the standard input stream and returns its integer
getchar() value (typically the ASCII value of the read character) on success. It returns EOF on failure2.

getch() A nonstandard function present in the conio.h header file, which is mostly used by MS-DOS compilers
Function Description

like Turbo C. It reads a single character from the keyboard but does not use any buffer, so the entered
character is immediately returned without waiting for the enter key

The main difference between getchar() and getch() is that getchar() reads from the standard input stream
and waits for the enter key before returning the entered character, while getch() reads from the keyboard and
immediately returns the entered character without waiting for the enter key.

21. Discuss various logical operators in C with examples.

In C programming, logical operators are used to combine multiple conditions or constraints. Logical operators return
either 0 or 1, depending on whether the expression result is true or false. There are 4 major logical operators in the C
language: Logical AND (&&), Logical OR (||), Logical NOT (!), and Logical XOR (^) 1.

Here are some examples of logical operators in C:


#include <stdio.h>

int main() {
int x = 7, y = 3;

printf("Following are the logical operators in C\n");

printf("The value of this logical AND operator ((x==y) && (x<y)) is: %d\n", ((x
== y) && (x < y)));

printf("The value of this logical OR operator ((x==y) || (x<y)) is: %d\n", ((x ==
y) || (x < y)));

printf("The value of this logical NOT operator (!(x==y)) is: %d\n", (!(x == y)));

return 0;
}
In this example, we use the logical AND (&&), logical OR (||), and logical NOT (!) operators to combine multiple
conditions and constraints. The output of this program would be:
Following are the logical operators in C
The value of this logical AND operator ((x==y) && (x<y)) is: 0
The value of this logical OR operator ((x==y) || (x<y)) is: 0
The value of this logical NOT operator (!(x==y)) is: 1

22. What is the purpose of header file? Example.

A header file is a file that contains a set of predefined standard library functions, data types, macros, etc. that can be
imported into a program with the help of a preprocessor directive #include. The primary purpose of a header file
is to propagate declarations to code files.

Here’s an example of using a header file in C:


#include <stdio.h>
int main() {
printf("Hello World!");
return 0;
}
In this example, we include the stdio.h header file which contains the declaration for the printf() function. By
including this header file, we can use the printf() function in our program without having to manually declare it.

23. Write a C program to accept a number, check whether the number is prime or not and print the result.

Here’s a C program that accepts a number, checks whether the number is prime or not and prints the result:
#include <stdio.h>
#include <stdbool.h>

int main() {
int num;
bool isPrime = true;

printf("Enter a positive integer: ");


scanf("%d", &num);

for (int i = 2; i <= num / 2; i++) {


if (num % i == 0) {
isPrime = false;
break;
}
}

if (isPrime && num > 1) {


printf("%d is a prime number.\n", num);
} else {
printf("%d is not a prime number.\n", num);
}

return 0;
}
In this program, we first accept a positive integer from the user and store it in the num variable. We then use
a for loop to iterate from 2 to num / 2 and check if num is divisible by any of these numbers. If num is divisible by
any of these numbers, we set the isPrime flag to false and break out of the loop.
After the loop, we check if the isPrime flag is still true and if num is greater than 1. If both conditions are true, we
print that num is a prime number; otherwise, we print that it is not a prime number.

24. Write a simple calculator program in C using switch case.

Here’s a simple calculator program in C that uses a switch case to perform various arithmetic operations:
#include <stdio.h>

int main() {
char operator;
double num1, num2, result;

printf("Enter an operator (+, -, *, /): ");


scanf("%c", &operator);

printf("Enter two numbers: ");


scanf("%lf %lf", &num1, &num2);

switch (operator) {
case '+':
result = num1 + num2;
printf("Result: %.2lf\n", result);
break;
case '-':
result = num1 - num2;
printf("Result: %.2lf\n", result);
break;
case '*':
result = num1 * num2;
printf("Result: %.2lf\n", result);
break;
case '/':
if (num2 != 0) {
result = num1 / num2;
printf("Result: %.2lf\n", result);
} else {
printf("Error: Division by zero is not allowed.\n");
}
break;
default:
printf("Error: Invalid operator.\n");
break;
}

return 0;
}

In this program, the user is prompted to enter an operator (+, -, *, /) and two numbers. The switch case statement is
used to determine which operation to perform based on the entered operator. The result is then calculated and
displayed on the screen.

Note: The program also includes an error handling mechanism to prevent division by zero. If the user attempts to
divide by zero, an error message is displayed instead of performing the division.

25. What are primitive and non-primitive data type? Define array and explain declaration and initialization of one and two
dimensional array with example.
In programming languages, data types can be divided into two categories: primitive and non-primitive. Primitive
data types are basic data types that are built into the language and are not composed of other data types. Examples
of primitive data types include int, char, float, double, etc. Non-primitive data types, on the other hand, are
derived from primitive data types and can be composed of multiple primitive or non-primitive data types. Examples
of non-primitive data types include arrays, classes, interfaces, etc.

An array is a collection of elements of the same type that are stored in contiguous memory locations. The elements
of an array can be accessed by their index, which starts from 0.

Here’s an example of declaring and initializing a one-dimensional array in C:


#include <stdio.h>

int main() {
int arr[5] = {1, 2, 3, 4, 5};

for (int i = 0; i < 5; i++) {


printf("%d ", arr[i]);
}

return 0;
}
In this example, we declare an array arr of size 5 and initialize it with the values {1, 2, 3, 4, 5}. We then use
a for loop to iterate over the elements of the array and print them.

Here’s an example of declaring and initializing a two-dimensional array in C:


#include <stdio.h>

int main() {
int arr[2][3] = {{1, 2, 3}, {4, 5, 6}};

for (int i = 0; i < 2; i++) {


for (int j = 0; j < 3; j++) {
printf("%d ", arr[i][j]);
}
printf("\n");
}

return 0;
}
In this example, we declare a two-dimensional array arr of size 2x3 and initialize it with the values {{1, 2, 3},
{4, 5, 6}}. We then use nested for loops to iterate over the rows and columns of the array and print its
elements.

You might also like