INTRODUCTION TO C PROGRAMMING
Course Code: 1BPLC205E/105E
CIE Marks- 50
SEE Marks- 50
Total Marks- 100
Credits- 4
Course outcomes (Course Skill Set)
At the end of the course, the student will be able to:
CO1: Explain the fundamental structure of a C program and primitive constructs.
CO2: Apply decision-making and iterative control structures to solve simple computational problems.
CO3: Develop programs using arrays and string operations to solve real-world problems.
CO4: Construct user-defined functions to modularize the solution to the given problems.
CO5: Build programs using structures and pointers for complex data representation and access.
Textbooks:
1. Programming in ANSI C, 9e, E Balaguruswamy, Tata McGraw Hill Education.
Reference books / Manuals:
1. PROGRAMMING IN C, Reema Thareja, Oxford University, Third Edition, 2023.
2. The ‘C’ Programming Language, Brian W. Kernighan and Dennis M. Ritchie, Second Edition, Prentice
Hall of India, 2015
Importance of C
• Portability and efficiency
• Produces compact, fast code and runs on virtually all platforms.
• Foundation for systems programming
• Close-to-hardware features suit OS, drivers, compilers, and embedded work.
• Structured programming discipline
• Functions and control constructs promote modular, readable design.
• Language interoperability and influence
• Easy to interface with other languages; its syntax shaped C++, Java, C#, etc.
• Rich standard library and toolchain
• Portable I/O, strings, memory utilities, and a compile–link–run workflow.
Flowcharts, Algorithms, and C Basics
• Algorithm: A finite, step-by-step procedure to solve a problem; properties
include finiteness, definiteness, and effectiveness.
• Algorithm to add 2 numbers:
Step 1: Start
Step 2: Read the first number, A
Step 3: Read the second number, B
Step 4: Set sum=A+B
Step 5 : Print sum
Step 6: End
• Flowchart: Graphical algorithm representation using standard
symbols (terminal, process, input/output, decision, flow lines,
connectors).
Flowchart to add 2 numbers:
Basic structure of C programming
Programming Styles
• C is a free‑form language; formatting doesn’t change meaning but affects readability.
• Prefer consistent indentation, brace placement, and one statement per line.
• Use lowercase for keywords; terminate statements with semicolons.
• Braces group statements; align and indent blocks clearly.
• Insert comments judiciously to explain intent.
Example (expanded vs one line)
• Multi‑line statements:
x = y + 3;
z = x + 1;
a = b * x + y / z; z = a % x;
main()
{
printf("hello C");
}
• Single line (avoid in practice):
main() { printf("hello C"); }
Compiling and Executing a ‘C’ Program.
Key Steps:
• Create: write/edit source program.
• Compile: translate to object code; fix compile errors.
• Link: add required C library functions; produce executable.
• Execute: run with input; correct logical/runtime errors; obtain correct output.
C TOKENS
Tokens are the basic building blocks in C language. C has 6 Different
types of tokens. C programs are written using these tokens.
1. Keywords
2. Identifiers
3. Constants
4. Strings
5. Special characters
6. Operators
1. KEYWORDS
▪ C has a set of reserved words known as keywords.
▪ All keywords are basically a sequence of characters that have a fixed meaning.
▪ All keywords should be written in lower case letters.
▪ Following are the 32 keywords in C.
2. Identifiers:
These are the names given to various elements of the C program like
variables, functions, arrays, etc. Ex- roll_number, marks, name etc.
Rules to define an Identifiers -
1. Identifiers cannot include any special characters (like @ $ %? )
except underscores ‘_’
2. An Identifier should not contain two consecutive underscores.
3. Keywords cannot be used as identifiers.
4. The first character of the identifier must always be a letter or an
underscore followed by any number of letters or digits or
underscore.
5. Identifiers are case sensitive
DATA TYPES IN C
DATA TYPES USED IN C
▪ String (or str or text). Used for a combination of any
characters that appear on a keyboard, such as letters,
numbers and symbols.
▪ Character (or char). Used for single letters.
▪ Integer (or int). Used for whole numbers.
▪ Float (or Real). Used for numbers that contain decimal
points, or for fractions.
▪ Boolean (or bool). Used where data is restricted
to True/False or yes/no options.
DATA TYPE USED IN VARIABLES
Integer
Character
Float
Double
Bool
String
bool a=true, b= false;
char a[10]=”AJIET”;
DATA TYPES USED IN C
▪ Integer (or int). Used for whole numbers.
Output:
#include <stdio.h> The integer value
void main() is: 5
{
int i = 5;
printf(“The integer value is: %d \n”, i);
}
DATA TYPES USED IN C
▪ Float (or Real). Used for numbers that contain
decimal points, or for fractions.
#include <stdio.h>
void main()
{
float f = 7.2357;
printf(“The float value is: %f \n”, f);
}
Output:
The float value is:
7.2357
DATA TYPES USED IN C
▪ Character (or char). Used for single letters.
#include <stdio.h>
void main()
{
char c = ‘b’;
printf(“The character value is: %c \n”, c);
}
Output:
The character value is:
b
CONSTANTS IN C
▪ Constants refer to fixed values that the program may not alter during its
execution. These fixed values are also called literals.
▪ Ex- Constants are used to fixed values like mathematical constant pi.
▪ Various constants in C program are:
1.) Integer constants
2.) Floating point constants
3.) Character constants
4.) String constants
1) Integer constants- An integer constant refers to a sequence of digits. There
are three types of integers, namely decimal, octal and hexadecimal.
▪ Decimal integers consist of a set of digits 0 through 9, preceded by an optional –
or + sign.
▪ Ex: 123, -431, 0, +678
▪ An octal integer constant consists of any combination of digits from the set 0
through 7 with a leading 0.
▪ Ex- 010 (decimal 8), 077 (decimal 63), and 035 (decimal 29) are valid octal
integer constant
▪ A sequence of digits preceded by 0x is considered as hexadecimal integer. They
may also include alphabets A through F
▪ 0x2, 0x9, 0x1A3, 0x9F0
▪ To declare a constant, precede the normal variable
declaration with const keyword & assign it a value.
Ex- const float pi=3.14;
const keyword specifies that value of pi cannot change.
▪ Alternate way is to declare a constant is to use pre-processor
command define.
Ex- #define pi 3.14
2.) Floating-point constants:
▪ Integer numbers are inadequate to represent
quantities that vary continuously, such as distances,
heights, temperatures, prices and so on.
▪ These quantities are represented by numbers
containing fractional parts like 23.78.
▪ Such numbers are called floating-point constants or
real constants.
▪ Exs- .95 , -.75, +.5
3. Character constants- A character constant contains a
single character enclosed within a pair of single quote
marks. Examples of character constants are: ‘5’ ‘X’ ‘;’ ‘ ‘
4. Backslash Character Constants:
Constant Meaning
\b Back space
\n New line character
\t Horizontal tab
\v Vertical tab
\0 Null character
5. String constants:
▪ A string constant is a sequence of characters
enclosed in double quotes. The letters may be
numbers, special characters and blank space.
▪ Exs- “THANK YOU” “2345” “?.....” “7+8-9” “X”
VARIABLES
▪ A variable is nothing but a meaningful name given to a data storage location in computer
memory.
▪ When using a variable, we actually refer to the address of the memory where data is stored.
▪ C language supports 2 basic kinds of variables: numeric & character.
▪ Numeric variables can be used to store either integer values or floating point values.
▪ Character variables are single characters enclosed within single quotes.
▪ Note- In memory characters are stored in their ASCII Codes, ‘A’ has ASCII code 65.
VARIABLE DECLARATION
▪ Variables should be declared in the C program before it is
used.
▪ To declare a variable, specify the data type of the variable
followed by its name.
data_type variable_name;
int age ;
Data type Variable name
VARIABLE INITIALIZATION
▪ We can initialize the variables with some value.
Ex- int age=15;
float salary=2156.35;
char grade=‘A’;
TYPES OF VARIABLES
[Link] Variable
A variable that is declared and used inside the function or block is called local
variable.
It cannot be used outside the block. Local variables need to be initialized before use.
2. Global Variable
A variable that is declared outside the function or block is called a global variable.
It is declared at the starting of program. It is available to all the functions.
INPUT/OUTPUT STATEMENTS IN C
▪ A stream is the source of data. C uses 2 forms of streams- text and binary.
▪ In a text stream, sequence of characters is divided into lines
with each line being terminated with \n.
▪ Binary stream contains data values using their memory representation.
▪ All input/output operations are carried out through
formatting functions such as printf and scanf.
40
MANAGING INPUT AND OUTPUT
A) Formatted Output:
1. printf():(print formatting) is used to display information required by user and also prints the values
of variables. It is a predefined function from the header<stdio.h>
Syntax :
printf(“control string”,variable list);
▪ The number of format specifier must match the number of variables in the variable list.
▪ printf takes 2 parameters, control string may contain text to be printed like instructions to users,
captions etc.
41
Format specifiers
%c -for character
%d -for integer
%s - for string data
%f -for real /floating point numbers
%lf -for double
%ld -for long integer
MANAGING INPUT AND OUTPUT
B) Formatted Input:
▪ scanf() ( scan formatting) function reads all type of data value from input device or from a file.
▪ The address operator “&” is used to indicate the memory location of the variable.
▪ Syntax:
scanf(“format specifier”,addresslist);
▪ format specifier indicates the type of data to be stored in the variable.
▪ address list indicates the location of the variable where the value of the data is to be stored .
▪ Ex -
scanf(“%d %d”,&value1,&value 2);
43
C PROGRAM TO DEMONSTRATE FORMATTED INPUT AND OUTPUT STATEMENTS
#include<stdio.h>
void main()
{
int a, b, c, sum;
printf(“Enter three numbers\n”);
scanf(“%d%d%d”,&a,&b&c);
sum=a+b+c;
printf(“ Addition of three Numbers=%d\n”,sum);
}
44
C PROGRAM TO FIND THE AREA OF CIRCLE BY READING THE INPUT FROM KEYBOARD
#include<stdio.h>
#define pi 3.14
int main()
{
int r;
float area;
printf("enter the radius of a circle\n");
scanf("%d",&r);
area=pi*r*r;
printf("area of a circle is:%f",area);
return 0;
}
45