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

prog..c 1 sem

The document provides an overview of C programming fundamentals, including the structure of a C program, data types, operators, typecasting, function prototypes, recursion, and storage classes. It details the six sections of a C program, various data types (primitive, derived, user-defined), and types of operators. Additionally, it explains concepts like recursion, function prototypes, and the four storage classes in C: auto, extern, static, and register.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

prog..c 1 sem

The document provides an overview of C programming fundamentals, including the structure of a C program, data types, operators, typecasting, function prototypes, recursion, and storage classes. It details the six sections of a C program, various data types (primitive, derived, user-defined), and types of operators. Additionally, it explains concepts like recursion, function prototypes, and the four storage classes in C: auto, extern, static, and register.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 9

[Pick the date] [ Programming in C with Data Structure]

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.

Sections of the C Program


There are 6 basic sections responsible for the proper execution of a program. Sections are mentioned
below:
1. Documentation
2. Preprocessor Section
3. Definition
4. Global Declaration
5. Main() Function
6. Sub Programs

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.

The data types in C can be classified as follows:

Types Description Data Types

Primitive data types are the most basic data types


Primitive int, char, float,
that are used for representing simple values such as
Data Types double, void
integers, float, characters, etc.

The data types that are derived from the primitive or


array, pointers,
Derived Types built-in datatypes are referred to as Derived Data
function
Types.

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.

 Constant expressions: Constant Expressions consists of only constant values. A constant


value is one that doesn’t change.
Examples:
5, 10 + 5 / 6.0, 'x’
 Integral expressions: Integral Expressions are those which produce integer results after
implementing all the automatic and explicit type conversions.
Examples:
x, x * y, x + int( 5.0)
where x and y are integer variables.
 Floating expressions: Float Expressions are which produce floating point results after
implementing all the automatic and explicit type conversions.
Examples:
x + y, 10.75
where x and y are floating point variables.
 Relational expressions: Relational Expressions yield results of type bool which takes a
value true or false. When arithmetic expressions are used on either side of a relational
operator, they will be evaluated first and then the results compared. Relational expressions
are also known as Boolean expressions.
Examples:
x <= y, x + y > 2
 Logical expressions: Logical Expressions combine two or more relational expressions and
produces bool type results.
Examples:
x > y && x == 10, x == 10 || y == 5
 Pointer expressions: Pointer Expressions produce address values.
Examples:
&x, ptr, ptr++
where x is a variable and ptr is a pointer.
 Bitwise expressions: Bitwise Expressions are used to manipulate data at bit level. They are
basically used for testing or shifting bits.
Examples:
x << 3
shifts three bit position to left
y >> 1
[Pick the date] [ Programming in C with Data Structure]
shifts one bit position to right.
Shift operators are often used for multiplication and division by powers of two.
Casting
Typecasting in C is the process of converting one data type to another data type by the
programmer using the casting operator during program design.
In typecasting, the destination data type may be smaller than the source data type when
converting the data type to another data type, that’s why it is also called narrowing conversion.
Types of Type Casting in C
In C there are two major types to perform type casting.
 Implicit type casting
 Explicit type casting

. Implicit Type Casting

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:

. Explicit Type Casting

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]

// C program to illustrate the function prototye

#include <stdio.h>

// Function prototype

float calculateRectangleArea(float length, float width);

int main()

float length = 5.0;

float width = 3.0;

// Function call

float area = calculateRectangleArea(length, width);

printf("The area of the rectangle is: %.2f\n", area);

return 0;

}
[Pick the date] [ Programming in C with Data Structure]

// Function definition

float calculateRectangleArea(float length, float width)

return length * width;

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]

You might also like