1|Page UNIT-1(Basic C Programming)
Embedded C programming:
What is C programming?
C is a programming language developed at AT & T’s Bell Laboratories of USA in 1972. It was designed
and written by a man named Dennis Ritchie. It is reliable, simple, and easy to use language. It is one of
the most influential programming languages and forms the foundation of many modern languages like
C++, Java, and Python. Large parts of popular operating systems including UNIX, Linux, and Windows
are still written in C.
C language runs fast and uses limited memory, making it suitable for use in so many applications
including the following:
To develop operating system
Used in embedded system–based applications, such as mobiles, microwave ovens, washing
machines, Internet of Things (IoT), and other smart devices.
To develop gaming applications
C is the preferred language for application that needs interaction with hardware devices.
Features of C:
1. Portability or machine independent (i.e., C program can run on different machines with little
or no modification).
2. Sound and versatile language (i.e., supports a wide range of applications from system to
application software)
3. Fast program execution.
4. An extendible language (i.e., new functions and features can be easily added by the
programmer).
5. Structured language (i.e., Supports structured programming using functions and control
statements).
2|Page UNIT-1(Basic C Programming)
Structure of a C Program:
The standard structure of a C program consists of the following six sections, in order. While not
all sections are mandatory, they provide a clear outline for organizing code.
Figure 1.1: Structure of a C Program
Documentation Section: The documentation section is used for displaying any information
about the program like the purpose of the program, name of the author, date and time written etc,
and this section should be enclosed within comment lines (/* ... */ for multi-line or // for
single-line). The statements in the documentation section are ignored by the compiler. But, the
statements are crucial for human understanding and maintenance of the program.
E.g., for Single line comment:
// This program was created on 24.06.2025
E.g., for Multiple line comment: e.g.,
/* Comment line 1
Comment line 2
Comment line 3 */
Note: This section is optional
3|Page UNIT-1(Basic C Programming)
Link Section (Preprocessor Directives): In C programming, a header file is a file that ends with
the .h extension and contains features like functions, data types, macros, etc that the particular C
program uses.
When use the preprocessor directive #include (e.g., #include <stdio.h> or #include
"myheader.h"), the C preprocessor effectively copies the entire content of the specified header
file into the source code before the actual compilation begins.
Figure 1.2: An example program to understand documentation and link sections
In the above program, we have included the stdio.h header file that provides the standard input
and output library in C.
Some of the C Header files include the following:
stdio.h - Defines core input and output functions.
math.h - Defines common mathematical functions.
stdlib.h - Defines numeric conversion functions, pseudo-random number generator, and
memory allocation
stddef.h - Defines several useful types and macros.
Definition section: This section consists of macro definitions, defining constants ((e.g., #define
PI 3.14), etc,. It means in the program where ever we have used PI, it will be replaced with 3.14
Note: This section is optional
Global Declaration Section: Variables declared in the global declaration section is accessible
throughout the program, i.e. accessible to all the functions in the program.
4|Page UNIT-1(Basic C Programming)
main() function: This section is mandatory for any program. Execution of the program begins
with the first statement inside the main() function's curly braces {}. It typically contains:
Declaration part: Where local variables used within the function are declared.
Executable part: The main logic, statements, control flow, and function calls that
perform the program's task.
Subprogram Section (User-defined Functions): This is the optional section. It contains the
definitions of functions created by the programmer.
Example C program:
// Documentation section
// This program prints a message
#include <stdio.h> // Link section
#define PI 3.14 // Definition section
int main() // main() function
printf("Hello, World!");
return 0;
}
5|Page UNIT-1(Basic C Programming)
Definition of Variable in C:
A variable is a named memory location in the computer’s memory to store data during
execution of a program. The value of variable may change during program execution (see below
figure).
Figure: A variable X is initially declared with value “3”, then assigned to new value “5” .
To create a variable in C, we must specify:
1. The name of the variable
2. The data type of the variable (e.g., integer, char, float, double, etc)
The data type determines what kind of data (e.g., integer, char, float, double, etc) that the
variable can store and how much memory is allocated.
Examples
Integer variable:
An integer variable can store only integer values
int a = 10;
Float variable:
A float variable can store only real (fractional) values. The float point constant is also
called real constant.
6|Page UNIT-1(Basic C Programming)
float b = 3.14;
The real constant or float value can be written in fractional form and exponential form. For
example,
In fractional form: +345.78,
-267.89,
34.87
The exponential form of representation is used when float value (real constants) is either too
small or too large. For example,
3.4e-5
5.67e8
-0.2e+3
Character variable:
A character variable can store only a single character
char c = 'A';
A character denotes any alphabet, digit or special symbol used to represent information. Figure
1.3 shows the valid alphabets, numbers and special symbols allowed in C.
Figure 1.3: Shows the valid alphabets, numbers and special symbols allowed in C.
7|Page UNIT-1(Basic C Programming)
Rules for Constructing Variable Names:
A variable name is any combination of 1 to 31 alphabets, digits, or underscores. Some compilers
allow variable names whose length could be up to 247 characters. Still, it would be safer to stick
to the rule of 31 characters. It is better to avoid unnecessarily long variable names as they require
typing effort.
The first character in the variable name must be an alphabet or underscore.
No commas or blanks are allowed within a variable name.
No special symbol other than an underscore (as in student_mark) can be used in a variable name.
Examples: si_int
m_hra
pop_e_89
What are C keywords?:
Keywords are the words (also called ‘Reserved words') whose meaning has already been
explained to the compiler. The keywords should not be used as variable names because if we do
So we are trying to assign a new meaning to the keyword, which is not allowed by the computer.
Some C compilers allow the user to construct variable names that exactly resemble the
keywords. However, it is safer not to mix up the variable names and the keywords.
There are only 32 keywords available in C, which are listed below.
Figure: C keywords
8|Page UNIT-1(Basic C Programming)
Data Types in C?:
Data types in C specify what kind of data a variable can store and how much memory is
allocated for it. When declaring a variable, it must be declared with an associated data type.
The data type declaration is usually done before using the variable, often at the beginning of the
program or before the function where it is used.
There are 4 types of data types in C language.
9|Page UNIT-1(Basic C Programming)
Note: In fact, the number of data types that can be derived in C, is in principle, unlimited. A C
programmer can always invent whatever data type he needs.
Integer data type:
The integer (int) data type is used to store whole numbers (numbers without decimal points).
Stores positive and negative whole numbers, including zero.
The int keyword is used to declare the integer variable.
Format specifier: %d. [a format specifier is used in input and output functions like
printf() and scanf() to tell the compiler what type of data is being read or printed]
The range and memory size of int data type depend on the compiler and system. For
example:
On a 16-bit compiler (such as Turbo C / Turbo C++):
The int data type uses 16 bits (2 bytes)
One bit is used for the sign
So the range is: −32768 to 32767
On a 32-bit compiler (such as VC++):
The int data type uses 32 bits (4 bytes)
One bit is used for the sign
So the range is: –2147483648 to +2147483647.
Note: Here a 16-bit compiler means that when it compiles a C program it generates machine
language code that is targeted towards working on a 16-bit microprocessor like Intel
10 | P a g e UNIT-1(Basic C Programming)
8086/8088. As against this, a 32-bit compiler like VC++ generates machine language code that is
targeted towards a 32-bit microprocessor like Intel Pentium. Note that this does not mean that
a program compiled using Turbo C would not work on 32-bit processor. It would run
successfully but at that time the 32-bit processor would work as if it were a 16-bit processor.
C provides variations of the integer data type (e.g., signed integer, unsigned integer, short
integer, and long integer) to store a wide range of decimal values.
Signed integer?
Signed integer variables are declared using the keyword signed. The range of values
handled by the signed integer is: -32768 to +32767.
Format specifier is:%d
Unsigned integer?
Unsigned integer variables are declared using the keyword unsigned. The range of values
handled by the signed integer is: 0 to 65535.
Format specifier is:%u
11 | P a g e UNIT-1(Basic C Programming)
Table : Variations of the integer data type
Note: The size and range of int, short, and long are complier dependent. Sizes provided in this
table are for 16-bit complier.
Note: In this table, the signed int and short int have same range (i.e,-32768 to 32767) which one
to use? confusion comes for reader. So what is the difference between signed int and short
signed int.
Answer is: Generally, sizeof(short int) ≤ sizeof(int) in C. On a 16-bit compiler, both
short int and int have the same size and range. However, on 32-bit and 64-bit compilers,
they differ in memory allocation—short int occupies less memory than int.
Therefore, short int is used when the required value range is small and memory
optimization is important.
12 | P a g e UNIT-1(Basic C Programming)
When to Use short int:
Answer is: When memory is limited (Common in embedded systems, microcontrollers, or low-
memory devices). short int consumes less memory than int, making programs more
memory-efficient.
Float, double, and long double data types:
Character data type:
Note: A char variable stores the decimal equivalent of the ASCII value of the entered character.
13 | P a g e UNIT-1(Basic C Programming)
14 | P a g e UNIT-1(Basic C Programming)
The following program is an example program that puts to use all the data types that
we have discussed in this topic.
15 | P a g e UNIT-1(Basic C Programming)
Operators in C programming?:
Operators in the C programming language are symbols that instruct the compiler to perform
specific mathematical, relational, bitwise, or logical operations on variables and values
(operands). They are essential for calculations, comparisons, and controlling program flow.
Types of Operators in C:
Arithmetic operators
Relational operators
Logical operators
Assignment operators
Increment and Decrement operators
Bitwise operators
Conditional (Ternary) operator
Special operators
The following section discuss these operators one by one.
1. Arithmetic Operators:
Arithmetic operators are the operators used to perform basic mathematical operations
such as addition, subtraction, multiplication, division, and modulus on operands. The
following table provides list of arithmetic operators. The following table provides a list of
arithmetic operators.
Operator Meaning/Function Example
+ Addition a+b
- Subtraction a-b
* Multiplication a*b
/ Division a/b
% Modulus(remainder). The a%b
modulus operator is used to
find the remainder after
division.
16 | P a g e UNIT-1(Basic C Programming)
Example Program for modulus operator:
2. Relational Operators: These operators are used to compare values.
A relational operator checks the relationship between two operands. If the relation is true,
it returns 1; if the relation is false, it returns value 0. Relational operators are used in
decision making and loops. The following table provides a list of relational operators.
Operator Meaning/Function Example
== Used to compare two values a==b
and checks whether they are
equal.
!= used to compare two values a != b
and checks whether they are
not equal.
> Greater than a>b
< Less than a<b
>= Greater than or equal to a >= b
<= Less than or equal to a <= b
17 | P a g e UNIT-1(Basic C Programming)
18 | P a g e UNIT-1(Basic C Programming)
3. Logical Operators: An expression containing logical operator returns either 0 or 1 depending
upon whether expression results true or false. Logical operators are commonly used in decision
making in C programming. The following table provides a list of logical operators.
Operator Meaning/Function Example
&& Logical AND. It is a logical If c = 5 and d = 3 then,
operator that returns true (1) expression ((c==5) && (d>5))
if both conditions are true; equals to 0.
otherwise, it returns false (0).
|| Logical OR. True only if If c = 5 and d = 2 then,
either one operand is true. expression ((c==5) || (d>5))
equals to 1.
! Logical NOT. Logical NOT. If c = 5 then,
True only if the operand is 0. expression !(c==5) equals to 0.
19 | P a g e UNIT-1(Basic C Programming)
4. Assignment Operators
An assignment operator is used for assigning a value to a variable. The most common
assignment operator is =. The following table provides a list of assignment operators.
20 | P a g e UNIT-1(Basic C Programming)
21 | P a g e UNIT-1(Basic C Programming)
5. Increment and Decrement Operators:
Increment ++ increases the value by 1 whereas decrement -- decreases the value by 1.
These two operators are unary operators, meaning they only operate on a single
operand.
22 | P a g e UNIT-1(Basic C Programming)
6. Bitwise operators:
Bitwise operators are used in C programming to perform bit-level operations.
23 | P a g e UNIT-1(Basic C Programming)
7. Conditional (Ternary) Operator:
We use the ternary operator in C to run one code when the condition is true and another
code when the condition is false. For example,
Note: The ternary operator is used as a shorthand for if-else.
8 . Special Operators
Operator Use
sizeof Returns size of data type
, Comma operator
& Address-of operator
* Pointer operator
24 | P a g e UNIT-1(Basic C Programming)
C program control:
In C programming, control statements manage the flow of program execution using decisions,
repetitions, and transfers based on conditions. They fall into three main categories: decision-
making statements, loop control statements, and jump statements. Decision-making statements
execute code based on conditions, including if, if-else, else-if, switch, and the ternary
operator. Loop control statements like for, while, and do-while handle repetitions. Jump
statements such as break, continue, goto, and return alter the normal flow.
Types of Control Statements in C:
Decision (Selection) Control Statements - Used to make decisions- (e.g., if, if-else, else-
if, switch)
Looping (Iteration) Control Statements - Used to repeat a block of code – (e.g., for,
while, and do-while)
Jump (Branching) Control Statements - Used to jump from one part of the program to
another – (break, continue, goto, and return)
Decision (Selection) Control Statements:
if Statement?
25 | P a g e UNIT-1(Basic C Programming)
Example Program: While purchasing certain items, a discount of 10% is offered if the quantity
purchased is more than 1000. If quantity and price per item are input through the keyboard, write
a program to calculate the total expenses.
26 | P a g e UNIT-1(Basic C Programming)
The if-else Statement:
The if statement by itself will execute a single statement, or a group of statements, when the
expression following if evaluates to true. It does nothing when the expression evaluates to false.
Can we execute one group of statements if the expression evaluates to true and another group of
statements if the expression evaluates to false? Of course! This is what is the purpose of the else
statement that is demonstrated in the following example.
27 | P a g e UNIT-1(Basic C Programming)
28 | P a g e UNIT-1(Basic C Programming)
if...else Ladder:
The if...else statement executes two different codes depending upon whether the test expression
is true or false. Sometimes, a choice has to be made from more than 2 possibilities.
The if...else ladder allows you to check between multiple test expressions and execute different
statements.
29 | P a g e UNIT-1(Basic C Programming)
Looping (Iteration) Control Statements:
In programming, a loop is used to repeat a block of code until the specified condition is met.
C programming has three types of loops:
1. for loop
2. while loop
3. do...while loop
30 | P a g e UNIT-1(Basic C Programming)
What is While Loop?
In a program, sometimes we need to perform a task a repeated number of times. A while loop is
a looping control statement that is used to repeat a block of statements as long as a specified
condition remains true.
The while loop is an entry-controlled loop, which means the condition is tested before the loop
body is executed. If the condition is true, the loop body is executed; otherwise, the loop
terminates. The flowchart shown below helps illustrate the operation of the while loop.
Figure: While loop flow chart
31 | P a g e UNIT-1(Basic C Programming)
Example program:
32 | P a g e UNIT-1(Basic C Programming)
33 | P a g e UNIT-1(Basic C Programming)
34 | P a g e UNIT-1(Basic C Programming)
What is a do-while loop?
A do-while loop is a looping control statement in C used to execute a block of statements at
least once, and then repeat the execution as long as a given condition is true. It is an exit-
controlled loop, which means the condition is checked after executing the loop body.
35 | P a g e UNIT-1(Basic C Programming)
36 | P a g e UNIT-1(Basic C Programming)
What is a for loop?
A for loop is a control statement used to repeat a set of statements a fixed number of times.
Use a for loop when you know in advance how many times the loop should run.
37 | P a g e UNIT-1(Basic C Programming)
The following figure would help in further clarifying the concept of execution of the for loop
38 | P a g e UNIT-1(Basic C Programming)
Figure: for loop working
39 | P a g e UNIT-1(Basic C Programming)
40 | P a g e UNIT-1(Basic C Programming)
What is break statement?
What is continue statement?
The continue statement breaks one iteration (in the loop), if a specified condition occurs, and
continues with the next iteration in the loop.
The following example skips the value of 4:
41 | P a g e UNIT-1(Basic C Programming)
What is a switch statement in C?
A switch statement in C is a multi-way decision control statement used to execute one block
of code from multiple possible options based on the value of an expression. It is an alternative to
using multiple if-else statements when comparing the same variable with different constant
values.
42 | P a g e UNIT-1(Basic C Programming)
43 | P a g e UNIT-1(Basic C Programming)
C functions?
What is a function in C?
A function in C is a self-contained block of code that performs a specific task. A function is
executed only when it is called.
If a C program contains more than one function, then one (and only one) of these
functions must be main( ), because program execution always begins with main( ).
There is no limit on the number of functions that might be present in a C program.
Each function in a program is called in the sequence specified by the function calls in
main( ).
After each function has done its thing, control returns to main( ).When main( ) runs out of
function calls, the program ends.
One function can call another function it has already called but has in the meantime left
temporarily in order to call a third function which will sometime later call the function that has
called it, if you understand what I mean. No? Well, let’s illustrate with an example.
44 | P a g e UNIT-1(Basic C Programming)
Here, main( ) calls other functions, which in turn call still other functions. Trace carefully the
way control passes from one function to another. Since the compiler always begins the program
execution with main( ), every function in a program must be called directly or indirectly by
main( ). In other words, the main( ) function drives other functions.
45 | P a g e UNIT-1(Basic C Programming)
More information on C function:
46 | P a g e UNIT-1(Basic C Programming)
Uses of functions in C
Functions are used to:
Break a large program into smaller, manageable parts
Perform repeated tasks without rewriting code
Improve program structure and readability
Make programs easier to debug and test
Enable code reuse in the same or different programs
47 | P a g e UNIT-1(Basic C Programming)
Benefits (advantages) of using functions
1. Modularity
Programs are divided into independent modules, making them easier to design and
maintain.
2. Reusability
Once written, a function can be used multiple times, reducing code duplication.
3. Readability
Functions make the program easier to understand by organizing logic into meaningful
blocks.
4. Easy debugging
Errors can be located and fixed more easily within individual functions.
5. Reduced program size
Avoids repetition of code, resulting in shorter and cleaner programs.
6. Teamwork support
Different programmers can work on different functions simultaneously
What is a recursive function?
A function that calls itself is called a recursive function, and the process is known as
recursion. Recursion is commonly used to solve problems that can be broken down into smaller,
similar sub-problems.
#include <stdio.h>
int factorial(int n)
if (n == 0) // base condition
return 1;
else
48 | P a g e UNIT-1(Basic C Programming)
return n * factorial(n - 1); // recursive call
int main()
int num = 5;
printf("Factorial of %d is %d", num, factorial(num));
return 0;
Passing Values between Functions:
The functions that we have used so far haven’t been very flexible. We call them and they do
what they are designed to do. Like our mechanic who always services the motorbike in exactly
the same way, we haven’t been able to influence the functions in the way they carry out their
tasks. It would be nice to have a little more control over what functions do, in the same way it
would be nice to be able to tell the mechanic, “Also change the engine oil, I am going for an
outing”. In short, now we want to communicate between the ‘calling’ and the ‘called’ functions.
The mechanism used to convey information to the function is the ‘argument’. You have
unknowingly used the arguments in the printf( ) and scanf( ) functions; the format string and the
list of variables used inside the parentheses in these functions are arguments. The arguments are
sometimes also called ‘parameters’.
Consider the following program. In this program, in main( ) we receive the values of a, b and c
through the keyboard and then output the sum of a, b and c. However, the calculation of sum is
done in a different function called calsum( ). If sum is to be calculated in calsum( ) and values of
a, b and c are received in main( ), then we must pass on these values to calsum( ), and once
calsum( ) calculates the sum we must return it from calsum( ) back to main( ).
49 | P a g e UNIT-1(Basic C Programming)
Advanced Features of Functions:
With a sound basis of the preliminaries of C functions, let us now get into their intricacies.
Following advanced topics would be considered here.
(a) Function Declaration and Prototypes
(b) Calling functions by value or by reference
(c) Recursion (already discussed)
50 | P a g e UNIT-1(Basic C Programming)
Function Declaration and Prototypes?
51 | P a g e UNIT-1(Basic C Programming)
Now the expected answers i.e. 2.25 and 6.25 are obtained. Note that the function square( ) must
be declared in main( ) as
float square ( float ) ;
This statement is often called the prototype declaration of the square( ) function. What it means
is square( ) is a function that receives a float and returns a float. We have done the prototype
declaration in main( ) because we have called it from main( ). There is a possibility that we may
call square( ) from several other functions other than main( ). Does this mean that we would need
prototype declaration of square( ) in all these functions. No, in such a case we would make only
one declaration outside all the functions at the beginning of the program.
52 | P a g e UNIT-1(Basic C Programming)
What is Call by Value?
Call by value is a method of passing arguments to a function in which copies of the actual
values are passed to the function. Any changes made to the parameters inside the function do
not affect the original variables used in the function call.
53 | P a g e UNIT-1(Basic C Programming)
What is Call by Reference?
Call by reference is a method of passing arguments to a function in which the address of the
actual variables is passed to the function. Because the function works on the original memory
locations, any changes made inside the function directly affect the original variables. C does
not support call by reference directly, but it is implemented using pointers.
54 | P a g e UNIT-1(Basic C Programming)
Comparison between Call by Value and Call by Reference in C:
References:
[Link]
[Link]
[Link]
[Link]
[Link]
[Link]