prog..c 1 sem
prog..c 1 sem
UNIT -01
Fundamentals of C Programming
Structure of a C Program- The basic structure of a C program is divided into 6 parts which
makes it easy to read, modify, document, and understand in a particular format. C program must follow
the below-mentioned outline in order to successfully compile and execute. Debugging is easier in a
well-structured C program.
Data Types –
Each variable in C has an associated data type. It specifies the type of data that the variable can
store like integer, character, floating, double, etc. Each data type requires different amounts of
memory and has some specific operations which can be performed over it.
User Defined The user-defined data types are defined by the user structure,
Data Types himself. union, enum
[Pick the date] [ Programming in C with Data Structure]
Operators in C
In C language, operators are symbols that represent operations to be performed on one or more
operands. They are the basic components of the C programming. In this article, we will learn
about all the built-in operators in C with examples.
What is a C Operator?
An operator in C can be defined as the symbol that helps us to perform some specific mathematical,
relational, bitwise, conditional, or logical computations on values and variables. The values and variables
used with operators are called operands. So we can say that the operators are the symbols that perform
operations on operands.
Here, ‘+’ is the operator known as the addition operator, and ‘a’ and ‘b’ are operands. The addition operator
tells the compiler to add both of the operands ‘a’ and ‘b’. To dive deeper into how operators are used with data
structures, the C Programming Course Online with Data Structures covers this topic thoroughly.
Types of Operators in C
C language provides a wide range of operators that can be classified into 6 types based on their functionality:
1. Arithmetic Operators
2. Relational Operators
3. Logical Operators
[Pick the date] [ Programming in C with Data Structure]
4. Bitwise Operators
5. Assignment Operators
6. Other Operators
Expression: An expression is a combination of operators, constants and variables. An
expression may consist of one or more operands, and zero or more operators to produce a
value.
Implicit type casting in C is used to convert the data type of any variable without using the actual value
that the variable holds. It performs the conversions without altering any of the values which are stored
in the data variable. Conversion of lower data type to higher data type will occur automatically.
Integer promotion will be performed first by the compiler. After that, it will determine whether two of
the operands have different data types. Using the hierarchy below, the conversion would appear as
follows if they both have varied data types:
There are some cases where if the data type remains unchanged, it can give incorrect output. In such
cases, typecasting can help to get the correct output and reduce the time of compilation. In explicit type
casting, we have to force the conversion between data types. This type of casting is explicitly defined
within the program.
Function Prototype in C
The C function prototype is a statement that tells the compiler about the function’s name, its
return type, numbers and data types of its parameters. By using this information, the compiler
cross-checks function parameters and their data type with function definition and function call.
Function prototype works like a function declaration where it is necessary where the function
reference or call is present before the function definition but optional if the function definition
is present before the function call in the program.
Example:
Program to illustrate the Function Prototype
C
[Pick the date] [ Programming in C with Data Structure]
#include <stdio.h>
// Function prototype
int main()
// Function call
return 0;
}
[Pick the date] [ Programming in C with Data Structure]
// Function definition
Output
The area of the rectangle is: 15.00
C Recursion
n C programming language, you may have heard of the concept of recursion. You may also
have heard that recursion is difficult and complex to understand and implement. Do not worry!
In this article, we are going to cover the basics of recursion in C, recursive functions, recursive
calls, and how it is different from iteration.
What is Recursion in C?
First, let’s start with the recursion definition,
Recursion is the process of a function calling itself repeatedly till the given condition is satisfied.
A function that calls itself directly or indirectly is called a recursive function and such kind of
function calls are called recursive calls.
In C, recursion is used to solve complex problems by breaking them down into simpler sub-
problems. We can solve large numbers of problems using recursion in C. For example, factorial
of a number, generating Fibonacci series, generating subsets, etc.
Let’s discuss some basic terminologies and fundamentals of recursion before going into working
and implementation.
Recursive Functions in C
In C, a function that calls itself is called Recursive Function. The recursive functions contain a
call to themselves somewhere in the function body. Moreover, such functions can contain
multiple recursive calls.
Basic Structure of Recursive Functions
The basic syntax structure of the recursive functions is:
type function_name (args) {
// function statements
[Pick the date] [ Programming in C with Data Structure]
// base condition
// recursion case (recursive call)
}
Storage Classes in C
C Storage Classes are used to describe the features of a variable/function. These features
basically include the scope, visibility, and lifetime which help us to trace the existence of a
particular variable during the runtime of a program.
C language uses 4 storage classes, namely:
1. auto
This is the default storage class for all the variables declared inside a function or a block.
Hence, the keyword auto is rarely used while writing programs in C language. Auto variables
can be only accessed within the block/function they have been declared and not outside them
(which defines their scope). Of course, these can be accessed within nested blocks within the
parent block/function in which the auto variable was declared.
However, accessing auto variables outside their scope using pointers to their exact memory
location is unsafe and results in undefined behavior. Auto variables are also assigned a garbage
value by default when they are declared.
Likewise, auto keyword is not used in front of functions as functions are not limited to block
scope.
2. extern
Extern storage class simply tells us that the variable is defined elsewhere and not within the
same block where it is used. Basically, the value is assigned to it in a different block and this
can be overwritten/changed in a different block as well. So an extern variable is nothing but a
global variable initialized with a legal value where it is declared in order to be used elsewhere.
It can be accessed within any function/block.
Also, a normal global variable can be made extern as well by placing the ‘extern’ keyword
before its declaration/definition in any function/block. This basically signifies that we are not
initializing a new variable but instead, we are using/accessing the global variable only. The
main purpose of using extern variables is that they can be accessed between two different files
which are part of a large program.
3. static
This storage class is used to declare static variables which are popularly used while writing
programs in C language. Static variables have the property of preserving their value even after
they are out of their scope! Hence, static variables preserve the value of their last use in their
scope. So we can say that they are initialized only once and exist till the termination of the
program. Thus, no new memory is allocated because they are not re-declared.
Their scope is local to the function to which they were defined. Global static variables can be
accessed anywhere in the program. By default, they are assigned the value 0 by the compiler.
4. register
This storage class declares register variables that have the same functionality as that of the auto
variables. The only difference is that the compiler tries to store these variables in the register of
the microprocessor if a free register is available. This makes the use of register variables to be
much faster than that of the variables stored in the memory during the runtime of the program.
[Pick the date] [ Programming in C with Data Structure]
If a free registration is not available, these are then stored in the memory only. Usually, a few
variables which are to be accessed very frequently in a program are declared with the register
keyword which improves the running time of the program. An important and interesting point
to be noted here is that we cannot obtain the address of a register variable using pointers.
[Pick the date] [ Programming in C with Data Structure]