Unit 1-1
Unit 1-1
B24CI0109: Fundamentals of C
Programming
D r. N e e l a m M a l y a d r i
Syllabus--
Introduction
SYLLABUS-- INTRODUCTION
SYLLABUS – INTRODUCTION -- OBJECTIVES
The objectives of this course are to:
1) Understand the basic structure and history of the C programming language, and set up
an environment to execute simple programs.
2) Write programs in modules using functions and recursive calls to achieve modularity
3) Understand and utilize concepts of arrays and pointers to write efficient programs.
CO1-Understand the foundational concepts of the C language, including its history and basic
building blocks.
CO2-Utilize conditional and unconditional control flow statements to write efficient programs.
CO3-Understand strings and library functions on strings.
1. INTRODUCTION TO C PROGRAMMING:
2. BUILDING BLOCKS OF C:
ARITHMETIC OPERATORS, RELATIONAL OPERATORS, LOGICAL
OPERATORS, BITWISE OPERATORS, ASSIGNMENT OPERATORS,
CONDITIONAL OPERATOR, PRECEDENCE AND ASSOCIATIVITY OF
OPERATORS AND TYPE CONVERSION
SYLLABUS – INTRODUCTION – IN BRIEF
3. CONTROL STATEMENTS:
DECISION MAKING WITH IF AND IF-ELSE, NESTED IF-ELSE
STATEMENTS, SWITCH CASE STATEMENTS,
LOOPING CONSTRUCTS: FOR, WHILE, AND DO-WHILE LOOPS,
NESTED LOOPS, BREAK AND CONTINUE STATEMENTS AND GOTO
STATEMENT AND LABELS
4.FUNCTIONS:
DEFINING AND CALLING FUNCTIONS, FUNCTION PROTOTYPES,
SCOPE AND LIFETIME OF VARIABLES, PASSING ARGUMENTS TO
FUNCTIONS, RETURN VALUES AND RECURSIVE FUNCTIONS
SYLLABUS – INTRODUCTION – IN BRIEF
Program
Algorithm
Flowcharts
PROGRAM - DEFINITION
A computer program is a
sequence or set of A computer program in
instructions in a its human-readable form
programming language for is called source code.
a computer to execute.
Video Games
SOFTWARE - DEFINITIONinstructions,
Software is a set of
data or
programs used to
operate computers and
execute specific tasks.
Software helps a
It is the opposite of
business organization
hardware, which
to cut costs by
describes the physical
automating routine
aspects of a
tasks.
computer.
SOFTWARE
Software not only makes
your computer hardware Software is a generic
perform important tasks, term used to refer to
but can also help your applications, scripts &
business work more programs that run on a
efficiently. device.
It can be thought of as
the variable part of a
computer, while
hardware is the
invariable part.
SOFTWARE – TYPES
Language
Unambiguity Fineness Effectiveness
Independence
GENERAL STRUCTURE OF AN ALGORITHM
• It makes use of
symbols which are
connected among
them to indicate the
flow of information
and processing.
Flowchart
NOTATIONS USED TO DRAW A FLOW CHART
ADVANTAGES OF
be analysed in more effective way therefore reducing cost
and wastage of time.
FLOWCHART
Proper documentation: Program flowcharts serve as a good
program documentation, which is needed for various
purposes, making things more efficient.
Unit 1
INTRODUCTION TO C PROGRAMMING
D r. N e e l a m M a l y a d r i
1. Introduction to C Programming:
Overview of C language, Structure of a C program,
Compilation and execution, Basic syntax and semantics,
Variables and data types, Constants and literals and Input
and output operations
Overview of C language
PROGRAMMING LANGUAGES : AN INTRODUCTION
To write a program (tells what to do) for a computer, we must use a computer language.
Over the years computer languages have evolved from machine languages to natural languages.
The following is the summary of computer languages
PROGRAMMING LANGUAGES : AN INTRODUCTION
Machine Language
Advantages Disadvantages
The desire to improve programmer efficiency and to change the focus the
computer to the problems being solved led to the development of high-level
languages.
High-level languages are portable to many different computer allowing the programm
to concentrate on the application problem at hand rather than the intricacies of the
computer.
PROGRAMMING LANGUAGES : AN INTRODUCTION
Language Translators
These are the special programs which are used for converting the programs in one language into
machine language instructions, so that they can be executed by the computer.
C language is
very easy to
Learn
C language can
be compiled on Easy to write a
variety of program code
computers
C language can C is a
handle low- structured
level activities language
C produces the
efficient
programs
C PROGRAMMING LANGUAGE : AN INTRODUCTION
C PROGRAMMING LANGUAGE : AN INTRODUCTION
CREATING AND RUNNING PROGRAMS
Documentation Consists of the description of the program, programmer's name, and creation date. These are
generally written in the form of comments.
Link All header files are included in this section which contains different functions from the
libraries. A copy of these header files is inserted into your code before compilation.
Definition Includes preprocessor directive, which contains symbolic constants. E.g.: #define allows us to
use constants in our code. It replaces all the constants with its value in the code.
Global Includes declaration of global variables, function declarations, static global variables, and
Declaration functions.
Main() Function For every C program, the execution starts from the main() function. It is mandatory to
include a main() function in every C program.
Includes all user-defined functions (functions the user provides). They can contain the inbuilt
Subprograms functions and the function definitions declared in the Global Declaration section. These are
called in the main() function.
C PROGRAMMING LANGUAGE : AN INTRODUCTION
BASIC STRUCTURE OF C PROGRAMMING
1 2
Identifiers in C are short and informative Each program element in a C program is
called an identifier. An identifier is a variable
names that uniquely identify variables or
that holds a value and stores it in the
function names. memory.
Identifiers
KEYWORDS
KEYWORDS
KEYWORDS
whose meaning has language are the functionality are
already been defined collection of pre- already known to the
by the computer – defined or reserved compiler. Each
they are pre-defined words. These are Keyword is meant to
words in the C case-sensitive and perform a specific
compiler. written in lower function in a C
cases. program.
These two lines can be modified as: (without knowing the data-type)
auto num = 10; //auto keyword is used to deduce the data type of a variable
auto univ= “REVA";
TOKENS IN C LANGUAGE – 3. CONSTANTS
CONSTANTS
execution of a program once they are defined.
• The constant variables in C can be initialized only
once and their default value is zero. If we try to re-
initialize or re-define any constant variable, then we
will get a compilation error.
TOKENS IN C LANGUAGE – 3. CONSTANTS
We can declare constants in C language in the following ways:
By using a const keyword
By using a #define pre-processor
1. Using const keyword
const keyword Here, we are using the const keyword to declare a variable and assigning a value to
it that can not be modified later.
• These are constants • These are constants • These are constants • These are constants
that represent integer that represent real that represent single that represent a
values. They can be numbers with a characters enclosed in sequence of
represented in fractional part. They single quotes. characters enclosed in
decimal, octal, or can be written in • For example, the double quotes.
hexadecimal notation. decimal or character constant 'A' • For example, the
• For example, the exponential notation. represents the string constant "Hello,
decimal integer • For example, the character A. world!" represents
constant 42 can be floating-point the sequence of
written as 42, the constant 3.14159 can characters Hello,
octal integer constant be written as 3.14159, world!.
42 can be written as and the floating-point
052, and the constant 1.23 x 10^4
hexadecimal integer can be written as
constant 42 can be 1.23e4.
written as 0x2A.
TOKENS IN C LANGUAGE – 3. CONSTANTS
#include <stdio.h>
int main() {
const int NUM = 10;
const float PI = 3.14159;
const char LETTER = 'A';
const char *MESSAGE = "Hello, world!"; OUTPUT
printf("Integer constant: %d\n", NUM);
printf("Floating-point constant: %f\n", PI);
printf("Character constant: %c\n", LETTER);
printf("String constant: %s\n", MESSAGE);
return 0;
}
TOKENS IN C LANGUAGE – 4. OPERATORS
Expressions
In programming, an expression is a combination of one or more
constants, variables, operators, and function calls that can be
evaluated to produce a single value. Expressions can be used in
various programming constructs, such as assignments, function calls,
conditional statements, and loops.
In C programming language, operators are symbols that are used to perform various
mathematical, logical and decision making operations on variables and constants. Operators in
C can be categorized into several different groups.
TOKENS IN C LANGUAGE – 4. OPERATORS
Types Description
Arithmetic Operators These operators are used to perform mathematical operations such as addition, subtraction,
multiplication, division, and modulus. The arithmetic operators in C are + (addition), - (subtraction),
* (multiplication), / (division), and % (modulus).
Relational Operators These operators are used to compare two values and return a boolean value (true or false)
depending on the result of the comparison. The relational operators in C are == (equal to), != (not
equal to), < (less than), > (greater than), <= (less than or equal to), and >= (greater than or equal
to).
Logical Operators These operators are used to perform logical operations such as AND, OR, and NOT. The logical
operators in C are && (AND), || (OR), and ! (NOT).
Assignment Operators These operators are used to assign values to variables. The assignment operators in C are = (simple
assignment), += (add and assign), -= (subtract and assign), *= (multiply and assign), /= (divide and
assign), and %= (modulus and assign).
Bitwise Operators These operators are used to perform bit-level operations such as bitwise AND, bitwise OR, and
bitwise NOT. The bitwise operators in C are & (bitwise AND), | (bitwise OR), ^ (bitwise XOR), ~
(bitwise NOT), << (left shift), and >> (right shift).
Conditional Operator This is a ternary operator that is used to evaluate a condition and return one of two values based
on the result of the evaluation. The conditional operator in C is ? : (ternary conditional operator).
TOKENS IN C LANGUAGE – 4. OPERATORS
#include <stdio.h>
int main() { // Relational operators
int a = 5, b = 2; printf("a == b : %d\n", a == b);
float c = 7.5, d = 2.0; printf("a != b : %d\n", a != b);
// Arithmetic operators printf("c > d : %d\n", c > d);
printf("c < d : %d\n", c < d);
printf("a + b = %d\n", a + b);
printf("a >= b : %d\n", a >= b);
printf("a - b = %d\n", a - b); printf("a <= b : %d\n", a <= b);
printf("a * b = %d\n", a * b); // Logical operators
printf("a / b = %d\n", a / b); printf("(a == b) && (c > d) : %d\n", (a == b) && (c > d));
printf("c + d = %f\n", c + d); printf("(a != b) || (c < d) : %d\n", (a != b) || (c < d));
printf("c - d = %f\n", c - d); printf("!(a == b) : %d\n", !(a == b));
printf("c * d = %f\n", c * d); // Bitwise operators
printf("c / d = %f\n", c / d); printf("a & b : %d\n", a & b);
// Increment and decrement operators printf("a | b : %d\n", a | b);
printf("a ^ b : %d\n", a ^ b);
printf("a++ = %d\n", a++);
printf("~a : %d\n", ~a);
printf("a = %d\n", a); printf("a << 1 : %d\n", a << 1);
printf("++b = %d\n", ++b); printf("b >> 1 : %d\n", b >> 1);
printf("b = %d\n", b); return 0;
}
TOKENS IN C LANGUAGE – 5. SPECIAL CHARACTERS
In C programming, there are certain special characters that have special meanings and are used to represent control characters,
escape sequences, and other special characters in strings and character literals. These special characters are represented by
escape sequences, which are combinations of the backslash character ‘\’ followed by a special code.
#include <stdio.h>
int main() {
char str[] = "Hello, world!"; // declare and initialize a string
printf("%s\n", str); // print the string
return 0;
}
We can also
initialize a string
by assigning it a
sequence of
characters one by
DATATYPES IN C
DATATYPES IN C LANGUAGE
Data types in C programming language refer to the type of data that can be stored in a variable.
In C, each variable must have a specified data type, which determines the size of the memory
required to store the data and the operations that can be performed on that data.
The need for data types in C arises from the fact that C is a strongly-typed language, which means
that the type of data stored in a variable must be declared before it can be used. This allows the
compiler to perform type checking and ensure that operations are performed only on data types
that are compatible with each other.
Using data types in C ensures that the program works as intended, with the
correct data being used in each operation. It also helps in reducing the
memory usage of a program, as the compiler knows the size of each variable
and can allocate memory accordingly.
Note : The use of data types enables the programmer to write more efficient
and readable code by using the appropriate data type for each variable or
constant.
DATATYPES IN C LANGUAGE
DATATYPES IN C LANGUAGE
In C programming language, there are several data types available, which can
be categorized as follows:
BASIC DATATYPES DERIVED DATATYPES ENUMERATED DATATYPES
char: character type, used to store a single arrays: a collection of similar data items
character or a small integer value. stored in contiguous memory locations.
int: integer type, used to store integer pointers: variables that hold the memory
values. addresses of other variables.
enum: user-defined data type used to
float: floating-point type, used to store structures: user-defined data types that assign names to integral constants, making
floating-point values with a single precision. combine a group of variables of different the code more readable.
data types under a single name.
double: floating-point type, used to store unions: similar to structures, but the
floating-point values with double precision. memory allocated to a union is shared
between all of its members.
A signed datatype can represent both positive and An unsigned datatype can only represent non-negative
negative values. It uses one bit to represent the sign of values. It uses all the bits to represent the magnitude of
the value (positive or negative), and the remaining bits to the value.
represent the magnitude of the value. For example, an unsigned 8-bit integer can represent
For example, a signed 8-bit integer (or "char") can values from 0 to 255.
represent values from -128 to +127.
Another difference between signed and unsigned datatypes is in how they handle arithmetic operations. For example,
in a signed integer, adding a negative value can result in overflow or underflow, while in an unsigned integer, adding a
negative value will simply wrap around to the maximum value. In addition, in a signed integer, dividing by a negative
value can result in a different result than dividing by a positive value, while in an unsigned integer, dividing by a
negative value is not possible.
DATATYPES IN C LANGUAGE
In C programming language, there are four basic data types:
char int float double
A character is a An integer is a A floating-point A double-precision
single byte that can whole number that number is a floating-point
represent a can be positive, number with a number is a
character from the negative, or zero. An fractional part. number with a
ASCII character set, int variable can A float variable fractional part and
or an integer value store an integer can store a more precision than
from -128 to 127. value in the range of floating-point a float. A double
A char variable is -2147483648 to value with variable can store a
typically used to 2147483647, single precision, floating-point value
represent a single depending on the typically 4 bytes with double
character or a small compiler and precision, typically
in size.
integer value. platform. 8 bytes in size.
DATATYPES IN C LANGUAGE
In C programming language, there are several datatypes available, which are classified into two
categories:
Primitive datatypes: These are the basic data types that are provided by the C language. The
primitive datatypes are further classified into three categories:
a. Integer types: These are used to store integer values. The size of the integer type
depends on the system architecture. The following diagram shows the different integer types in C
with their size and range:
Integer Type Size Range
char 1 byte -128 to 127
Boolean type: This is used to store true/false or 0/1 values. The size of the
Boolean type is implementation-dependent, but it is usually represented as an
integer type with a size of 1 byte.
Note : In C programming language, the boolean datatype is not a built-in
datatype. However, it can be emulated using the integer datatype, where 0
represents false and any non-zero value represents true.
Boolean Type Size Range
bool 1 byte 1 – TRUE
0 - FALSE
DATATYPES IN C LANGUAGE
Derived data types
Derived Datatypes
Derived Datatypes
whose variables types are type won’t
allow us to store basically derived typically create a
multiple values out of the new data type –
of same type. primitive data but would add
But they never types. They are various new
allows to store used to create a functionalities to
multiple values complex data the existing ones
of different structures. instead.
types.
DATATYPES IN C LANGUAGE
Derived data types
Example: Example:
int a[5] = {9,8,7,6,5}; int var = 20;
Int *p = &var; //declaration and initialization of pointer
DATATYPES IN C LANGUAGE
sizeof( ) operator
The sizeof operator in C is used to determine the size, in bytes, of a type or a variable.
It returns the size as an unsigned integer value.
Variables language.
• Variables are used to store the values
that are used by the program to
perform various tasks.
Variables
• In C, variables must be declared before they
are used. This means that we need to
declare a variable before we can use its
value in an expression or assign a new value
to it.
VARIABLES IN C
Variables are useful in C programming for several reasons. Here are a few examples:
datatype variable_name;
Assignment Operator
Any Valid C Identifier
Any Valid C Datatype
Example
SIMPLE I/O (INPUT/OUTPUT) FUNCTIONS IN C
SIMPLE I/O FUNCTIONS IN C
#include <stdio.h>
int main() {
// Formatted Input In this example, the scanf() function is
int num1, num2; used to read two integers from the user.
printf("Enter two numbers: "); The format specifier %d is used to
scanf("%d %d", &num1, &num2); indicate that integers are expected. The
printf("Sum: %d\n", num1 + num2); values are stored in the variables num1
// Formatted Output and num2, and their sum is printed
int value = 42; using printf().
printf("The value is: %d\n", value);
return 0;
}
Assignment Questions – Unit 1
ASSIGNMENT 1 QUESTIONS
Q1. Ajay is a first year B.Tech CSE student. His Programming with C faculty has given one task to find the count the number of
digits in a number which will enter by user. Help Ajay to write a C Program to Count number of digits in number without using
mod operator.
Q2. Lally is very weak in mathematics. So Raju challenges Lally to add each digit of a number, if she adds correctly she will get a
chocolate. Help Lally to get a chocolate by writing a C program to find the sum of the digits.
Q3. Arun is interested in Chess game. He joined in a chess club and first task is given by trainer to find the number of squares in
chessboard. Help Arun by writing a C program to find the number of squares in chessboard.
Q4. Gautam is a first year civil engineering student. His Faculty has given a simple task to convert the kilometers into miles.
Write a C program to convert the user entered Kilometers (KM) value into Miles.
Q5. In the Dubai, fuel efficiency for vehicles is normally expressed in miles-per-gallon (MPG). In India, fuel efficiency is normally
expressed in liters-per-hundred kilometers (L/100 km). Use your research skills to determine how to convert from
Miles-per-Gallon to Litres/100 km by writing a Simple C Program.
DISCUSSION
5 MINUTES
Applications !!!!!
Certification Courses!!!!!
Thank You
Regards
Associate Professor