0% found this document useful (0 votes)
42 views

Programming in C C

Mohit Kumar is taking the course "Programming in C" with course code DCA1102 and roll number 2214507293. The document describes Mohit answering 3 questions about C programming. The first question is about features of C including efficiency, portability, low-level access, structured programming, pointers, preprocessor, data types, standard library, and object-oriented programming. The second question explains branching statements in C like if, if-else, switch, and goto with examples. The third question defines a function and lists categories including functions with/without return values, fixed/variable parameters, recursive functions, inline functions, static functions, and extern functions.

Uploaded by

Mohit Sharma
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
42 views

Programming in C C

Mohit Kumar is taking the course "Programming in C" with course code DCA1102 and roll number 2214507293. The document describes Mohit answering 3 questions about C programming. The first question is about features of C including efficiency, portability, low-level access, structured programming, pointers, preprocessor, data types, standard library, and object-oriented programming. The second question explains branching statements in C like if, if-else, switch, and goto with examples. The third question defines a function and lists categories including functions with/without return values, fixed/variable parameters, recursive functions, inline functions, static functions, and extern functions.

Uploaded by

Mohit Sharma
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 10

NAME- MOHIT KUMAR

COURSE NAME: PROGRAMMING IN C


COURSE CODE : DCA1102
ROLL NUMBER: 2214507293
SET – I

Q.1. Describe various features of the C programming language.

Ans. C is a general-purpose, imperative programming language that was originally developed


in the early 1970s by Dennis Ritchie at Bell Labs. It is a high-level language, which means
that it is closer to human language than machine language, and it is designed to be easy to
read and write. C has a number of features that make it a popular choice for many types of
software development:

1. Efficiency: C is a compiled language, which means that it is converted into machine


code before it is executed. This makes it faster and more efficient than interpreted
languages, which are executed directly by the interpreter.
2. Portability: C is designed to be portable, which means that it can be easily adapted to
run on different types of computers. This is achieved through the use of a
standardized library of functions, called the C Standard Library, which is available on
most platforms.
3. Low-level access: C gives programmers low-level access to the underlying hardware
of the computer, which allows them to write code that is highly optimized and
efficient. This makes it a popular choice for systems programming, where
performance is critical.
4. Structured programming: C uses a structured programming paradigm, which means
that it encourages the use of functions and blocks to organize and modularize code.
This makes it easier to write, debug, and maintain large programs.
5. Pointers: C includes a feature called pointers, which allow programmers to
manipulate memory directly. This can be useful for tasks such as dynamic memory
allocation and data structures, but it can also be dangerous if used improperly, as it
can lead to bugs and security vulnerabilities.
6. Preprocessor: C includes a preprocessor, which is a program that processes the
source code before it is compiled. The preprocessor can be used to include header
files, define macros, and perform other tasks that can simplify the development
process.
7. Data types: C supports a variety of data types, including integers, floating-point
numbers, characters, and arrays. It also allows programmers to define their own data
types using structures and unions.
8. Standard library: C comes with a standard library of functions that can be used for
tasks such as input/output, string manipulation, and math operations. The standard
library is defined by the C Standard, which is a document that specifies the rules and
conventions for the language.
9. Object-oriented programming: While C is not a fully object-oriented language, it
does support some object-oriented programming features, such as encapsulation and
polymorphism, through the use of structures and function pointers.
10. Compatibility: C is widely used and has been around for a long time, which means
that it is supported by many compilers, libraries, and tools. It is also compatible with a
number of other programming languages, including C++, Objective-C, and C#.
----------------------------------------------------------------------------------------------------------------
-----

Q.2. Explain various branching statements in C with examples.

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:

1. if statement: The if statement is used to execute a block of code if a certain condition


is true. For example:

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.

It is important to note that the goto statement is generally considered to be a less-


preferable way to control the flow of execution in a program, as it can make the code
more difficult to read and maintain. However, it can be useful in certain situations,
such as when breaking out of deeply nested loops or when handling exceptions.
----------------------------------------------------------------------------------------------------------------
-----

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:

int add(int x, int y) {

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.

There are several categories of user-defined functions in C:

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.

The syntax for declaring an array in C is as follows:

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];

To initialize a one-dimensional array, you can use the following syntax:

data_type array_name[array_size] = {element1, element2, ..., elementN};

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:

int my_array[10] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};

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:

int my_array[10] = {1, 2, 3};

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];

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

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.

Ans. In C programming, a structure is a user-defined data type that represents a collection of


variables of different data types. A structure is a way to group together related variables
under a single name, so that they can be easily accessed and manipulated.

The syntax for declaring a structure in C is as follows:

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:

struct structure_name variable_name;

For example, to declare a variable called s1 of type student, you can use the following syntax:

struct student s1;

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:

struct student s2 = {"John", 20, 85.5};

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:

typedef struct student {

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;

(b) List out the differences between unions and structures.

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:

#define MAX(a, b) ((a) > (b) ? (a) : (b))

Here, MAX is defined as a macro that returns the maximum of a and b. You can use
MAX in your program as follows:

int c = MAX(a, b);

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.

In summary, preprocessor directives are commands that are executed by the


preprocessor before the compilation of a program. The #define directive is used to
define a constant value or a macro, while the #include directive is used to include a
header file in your program

You might also like