Programming in C C
Programming in C C
Ans. Branching statements are used in C to control the flow of execution in a program by
allowing the programmer to specify which statements should be executed under certain
conditions. There are several types of branching statements in C, including:
if (x > 0) {
printf("x is positive\n");
}
In this example, the block of code inside the curly braces will be executed if x is
greater than 0. If x is not greater than 0, the block of code will be skipped.
2. if-else statement: The if-else statement is an extension of the if statement that allows
the programmer to specify a block of code to be executed if the condition is false. For
example:
if (x > 0) {
printf("x is positive\n");
} else {
printf("x is not positive\n");
}
In this example, the block of code inside the first set of curly braces will be executed
if x is greater than 0, and the block of code inside the second set of curly braces will
be executed if x is not greater than 0.
3. switch statement: The switch statement is a branching statement that allows the
programmer to specify a large number of different conditions and the corresponding
blocks of code to be executed for each condition. For example:
switch (x) {
case 1:
printf("x is 1\n");
break;
case 2:
printf("x is 2\n");
break;
default:
printf("x is not 1 or 2\n");
}
In this example, the block of code inside the first case statement will be executed if x
is 1, the block of code inside the second case statement will be executed if x is 2, and
the block of code inside the default statement will be executed if x is neither 1 nor 2.
The break statement is used to exit the switch statement once the appropriate block of
code has been executed.
4. goto statement: The goto statement is a branching statement that allows the
programmer to jump to a specific label in the program. For example:
int i = 0;
loop:
printf("i = %d\n", i);
i++;
if (i < 10) {
goto loop;
}
In this example, the goto statement causes the program to jump back to the loop label
and execute the block of code again. This will be repeated until i is greater than or
equal to 10, at which point the goto statement will no longer be executed and the
program will continue to the next line of code.
Q.3 Define a function. List and explain the categories of user defined functions.
Ans. A function in C is a block of code that performs a specific task and can be called from
other parts of the program. Functions are a useful way to modularize code and make it easier
to read and maintain.
To define a function in C, you must specify the function's return type, name, and parameters.
The return type is the data type of the value that the function returns. The name is the
identifier that you will use to call the function. The parameters are the variables that are
passed to the function when it is called. For example:
return x + y;
}
In this example, the function is named add and it takes two integers, x and y, as parameters.
The function returns an integer, which is the sum of x and y.
1. Functions with a return value: These are functions that return a value of a specific
data type, such as an integer, float, or char. The value is returned using the return
statement.
2. Functions without a return value: These are functions that do not return a value.
They are typically used for tasks that do not require a return value, such as printing to
the screen or performing some other action.
3. Functions with a fixed number of parameters: These are functions that expect a
specific number of parameters when they are called. If the wrong number of
parameters is passed, the program will produce an error.
4. Functions with a variable number of parameters: These are functions that can
accept a variable number of parameters. They are defined using the ... notation, which
indicates that the function can take any number of parameters of a specific type.
5. Recursive functions: These are functions that call themselves in order to perform a
task. Recursive functions are often used for tasks that can be divided into smaller,
similar subproblems, such as calculating the factorial of a number.
6. Inline functions: These are functions that are expanded in place at the point of their
call, rather than being called as a separate function. Inline functions are typically used
to improve performance by reducing the overhead of function calls, but they can also
increase the size of the program.
7. Static functions: These are functions that are only visible within the file in which
they are defined. They are typically used to hide implementation details and prevent
them from being accessed by other parts of the program.
8. Extern functions: These are functions that are defined in one file and used in another
file. They are typically used to share code between files or to use code that is defined
in a library.
It is important to note that functions in C must be declared before they are used, and they
must be defined before they are called. This can be done using a function prototype, which
specifies the function's return type, name, and parameters.
----------------------------------------------------------------------------------------------------------------
-----
SET – II
Q.4 Define an array. How to initialize a one-dimensional array? Explain with suitable
examples.
Ans. In C programming, an array is a collection of elements of the same data type, stored
contiguously in memory and accessed using an index. An array is a way to store a collection
of variables of the same data type in a single place, so that they can be easily accessed and
manipulated.
data_type array_name[array_size];
Here, data_type is the type of data that the array will store (e.g. int, float, char, etc.),
array_name is the name of the array, and array_size is the number of elements that the array
can hold. For example, to declare an array of 10 integers, you can use the following syntax:
int my_array[10];
Here, element1, element2, ..., elementN are the values that you want to assign to the elements
of the array. For example, to initialize an array of 10 integers with the values 1, 2, 3, 4, 5, 6,
7, 8, 9, and 10, you can use the following syntax:
You can also initialize an array with a smaller number of elements, and the remaining
elements will be initialized with their default values. For example, to initialize an array of 10
integers with the values 1, 2, and 3, you can use the following syntax:
This will initialize the first three elements of the array with the values 1, 2, and 3, and the
remaining elements with their default values (i.e. 0 for integers).
You can also initialize an array using a loop. For example, to initialize an array of 10 integers
with the values 1, 2, 3, ..., 10, you can use a for loop as follows:
int my_array[10];
my_array[i] = i + 1;}
This will initialize the elements of the array with the values 1, 2, 3, ..., 10.
You can also use the memset function to initialize an array with a specific value. For
example, to initialize an array of 10 integers with the value 0, you can use the following
syntax:
int my_array[10];
memset(my_array, 0, sizeof(my_array));
This will initialize all elements of the array with the value 0.It's important to note that arrays
in C are 0-indexed, which means that the first element of the array is located at index 0, the
second element is located at index 1, and so on. To access a specific element of an array, you
can use the following syntax: array_name[index];
Here, array_name is the name of the array and index is the index of the element that you want
to access
Q.5 (a) Define structure and write the general syntax for declaring and accessing
members.
struct structure_name {
data_type member1;
data_type member2;
data_type member3;};
Here, structure_name is the name of the structure, and member1, member2, ..., memberN are
the members of the structure, which can be variables of different data types. For example, to
declare a structure called student with three members called name, age, and marks, you can
use the following syntax:
struct student {
char name[50];
int age;
float marks;
};
To declare a variable of a structure type, you can use the following syntax:
For example, to declare a variable called s1 of type student, you can use the following syntax:
To access the members of a structure, you can use the dot operator (.). For example, to access
the name member of the s1 variable, you can use the following syntax:
s1.name;
It's also possible to initialize a structure at the time of declaration. For example, to declare a
variable called s2 of type student and initialize its members with the values "John", 20, and
85.5, you can use the following syntax:
You can also use the typedef keyword to give a structure a new name. For example, to give
the student structure the new name student_t, you can use the following syntax:
char name[50];
int age;
float marks;
} student_t;
This allows you to declare variables of type student_t using the following syntax: student_t
s1;
Ans. In C programming, unions and structures are both user-defined data types that allow you
to group together related variables under a single name. However, there are some key
differences between unions and structures:
Size: The main difference between unions and structures is the amount of memory they
occupy. A structure is a collection of variables of different data types, and each member of
the structure occupies a separate memory location. As a result, the size of a structure is equal
to the sum of the sizes of its members. On the other hand, a union is a collection of variables
of different data types, but all the members of the union share the same memory location. As
a result, the size of a union is equal to the size of its largest member.
Accessing members: Both unions and structures allow you to access their members using the
dot operator (.) or the arrow operator (->). However, when you access a member of a
structure, you are accessing a separate memory location for that member. On the other hand,
when you access a member of a union, you are accessing the same memory location as all the
other members of the union.
Use cases: Unions are useful when you need to save memory and you only need to access
one member of the union at a time. This is because the size of a union is equal to the size of
its largest member, which means that you can use a union to store different types of data
without consuming more memory than necessary. On the other hand, structures are useful
when you need to store and access multiple pieces of data at the same time.
Q.6 What is preprocessor directive? Explain #define and #include preprocessor directives.
Ans. In C programming, preprocessor directives are commands that are executed by the
preprocessor before the compilation of a program. Preprocessor directives are recognized by
the compiler because they begin with a # symbol.
There are several types of preprocessor directives, including #define, #include, and #ifdef.
1. #define: The #define directive is used to define a constant value or a macro. When
you define a constant value using #define, you can use it in your program as a
symbolic name for the value. For example:
#define PI 3.14
Here, PI is defined as a constant value of 3.14. You can use PI in your program as
follows: double area = PI * r * r;
The #define directive can also be used to define a macro, which is a block of code that
is replaced by the preprocessor with the expansion of the macro. For example:
Here, MAX is defined as a macro that returns the maximum of a and b. You can use
MAX in your program as follows:
The preprocessor will replace the MAX macro with the expansion of the macro,
resulting in the following code:
int c = ((a) > (b) ? (a) : (b));
2. #include: The #include directive is used to include a header file in your program. A
header file is a file that contains declarations and macros that can be used in your
program. For example:
#include <stdio.h>
This directive includes the stdio.h header file, which contains declarations and macros
for input and output functions such as printf and scanf. You can use the functions and
macros declared in the stdio.h header file in your program as follows:
int main() {
printf("Hello, World!\n");
return 0;
}
You can also include a header file that you have created yourself by enclosing the file
name in double quotes (") instead of angle brackets (< and >). For example:
#include "myheader.h"
This directive includes the myheader.h header file, which may contain declarations
and macros that you have defined yourself.