KCA University
Nairobi, Kenya
BSD 1207/BIT01203
INTRODUCTION TO PROGRAMMING
Prepared By:
Dr. Linus Aloo, PhD
E-mail: [email protected]
Phone: 0754188380
Course Text
1. Programming in ANSI C" by E. Balagurusamy (8th Edition), McGraw Hill Education, 2019.
References
1. The C Programming Language" by Brian Kernighan and Dennis Ritchie (2nd Edition)
2. Fundamentals of programming languages by Dipali P. Baviskar, 2010.
3. Expert C Programming: Deep C Secrets" by Peter van der Linden.
4. C++: The Complete Reference" by Herbert Schildt (4th Edition)
5. https://www.tutorialspoint.com/cprogramming/index.htm
6. https://www.javatpoint.com/c-programming-language-tutorial
7. https://www.w3schools.com/c/index.php
10/19/2024 Introduction to Programming Lecture Notes By Dr. Linus .A. Aloo 1
CONSTANTS AND LITERALS
Constants and Literals
Integer Literals
Constants refer to fixed values that the
An integer literal can be a decimal,
program may not alter during its
octal, or hexadecimal constant.
execution.
Examples:
These fixed values are also called literals.
Constants can be of any of the basic data
types like an integer constant, a floating
constant, a character constant, or a string
literal.
There are enumeration constants as well.
CONSTANTS AND LITERALS
Constants and Literals
Floating-point Literals Character Constants
A floating-point literal has an integer part, Character literals are enclosed in
a decimal point, a fractional part, and single quotes, e.g., 'x' can be stored in
an exponent part. You can represent a simple variable of char type.
floating point literals either in decimal A character literal can be a plain
form or exponential form. character (e.g., 'x'), an escape
3.14159 /* Legal */ sequence (e.g., '\t'), or a universal
314159E-5L /* Legal */ character (e.g., '\u02C0').
CONSTANTS AND LITERALS
Constants and Literals
Character Constants
List of such escape sequence codes:
CONSTANTS AND LITERALS
Constants and Literals
Defining Constants
String Literals There are two simple ways in C to define
String literals or constants are constants:
enclosed in double quotes "". i. Using #define preprocessor
A string contains characters ii. Using const keyword
that are similar to character The #define Preprocessor
literals: plain characters, The syntax is:
escape sequences, and #define identifier value
universal characters.
CONSTANTS AND LITERALS
Constants and Literals
The #define Preprocessor
Example code:
#include <stdio.h>
#define LENGTH 10
#define WIDTH 5
#define NEWLINE '\n'
int main()
{
int area;
area = LENGTH * WIDTH;
printf("value of area : %d", area);
printf("%c", NEWLINE);
return 0;
}
RESULT: value of area : 50
CONSTANTS AND LITERALS
Constants and Literals
The const Keyword
You can use const prefix to declare constants with
a specific type as follows:
const type variable = value;
#include <stdio.h>
int main()
{
const int LENGTH = 10;
const int WIDTH = 5;
const char NEWLINE = '\n';
int area;
area = LENGTH * WIDTH;
printf("value of area : %d", area);
printf("%c", NEWLINE);
return 0;
}
RESULT: value of area : 50
STORAGE CLASSES
Types of Storage Classes
A storage class defines the scope The auto Storage Class
(visibility) and life-time of variables The auto storage class is the default storage class for
and/or functions within a C Program. all local variables.
They precede the type that they {
modify. int mount;
We have four different storage classes auto int month;
in a C program: }
auto The example above defines two variables within
register the same storage class. ‘auto’ can only be used
static within functions, i.e., local variables.
extern
STORAGE CLASSES
Types of Storage Classes
The register Storage Class The static Storage Class
The register storage class is used The static storage class instructs the compiler
to define local variables that to keep a local variable in existence during the
should be stored in a register life-time of the program instead of creating and
instead of RAM. destroying it each time it comes into and goes
{ out of scope.
register int miles;
} In C programming, when static is used on a
The register should only be used class data member, it causes only one copy of
for variables that require quick that member to be shared by all the objects of
access such as counters. its class.
STORAGE CLASSES
Types of Storage Classes
The static Storage Class
#include <stdio.h>
/* function declaration */
void func(void);
static int count = 5; /* global variable */
main()
{
while(count--)
{
func();
}
return 0;
}
/* function definition */
void func( void )
{
static int i = 5; /* local static variable */
i++;
printf("i is %d and count is %d\n", i, count);
}
STORAGE CLASSES
Types of Storage Classes The extern modifier is most commonly
The extern Storage Class
used when there are two or more files
The extern storage class is used to give
sharing the same global variables or
a reference of a global variable that is
functions as explained below.
visible to ALL the program files. First File: main.c
When you have multiple files and you #include <stdio.h>
int count;
define a global variable or function,
extern void write_extern();
which will also be used in other files, main()
then extern will be used in another file {
to provide the reference of defined count = 5;
write_extern();
variable or function.
}
STORAGE CLASSES
Types of Storage Classes
The extern Storage Class
Second File: support.c Now, compile these two files as follows:
#include <stdio.h> $gcc main.c support.c
extern int count; It will produce the executable program
void write_extern(void)
a.out. When this program is executed, it
{
produces the following result:
printf("count is %d\n", count);
} 5
Here, extern is being used to declare count
in the second file, whereas it has its
definition in the first file, main.c.
OPERATORS
Types of Operators
An operator is a symbol that tells the
compiler to perform specific
mathematical or logical functions. C
language is rich in built-in operators and
provides the
following types of operators:
Arithmetic Operators
Relational Operators
Logical Operators
Bitwise Operators
Assignment Operators
Other Operators
OPERATORS
Types of Operators Arithmetic Operations in C
S. No. Symbol Operator Description Syntax
Adds two numeric
1 + Plus a+b
values.
1. Arithmetic Operations in C Subtracts right
2 – Minus operand from left a–b
operand.
The arithmetic operators are Multiply two
3 * Multiply a*b
numeric values.
used to perform 4 / Divide
Divide two numeric
a/b
values.
arithmetic/mathematical Returns the
remainder after
5 % Modulus diving the left a%b
operand with the
operations on operands. right operand.
There
Used to specify the
are 9 arithmetic 6 + Unary Plus
positive values.
+a
Flips the sign of the
operators in C language: 7 – Unary Minus
value.
-a
Increases the value
8 ++ Increment a++
of the operand by 1.
Decreases the value
9 — Decrement a–
of the operand by 1.
OPERATORS
1. Arithmetic Operations in C
Example:
// C program to illustrate the arithmetic
operators
#include <stdio.h>
int main()
{
int a = 25, b = 5;
// using operators and printing results
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);
printf("+a = %d\n", +a);
printf("-a = %d\n", -a);
printf("a++ = %d\n", a++);
printf("a-- = %d\n", a--);
return 0;
}
OPERATORS
2. Relational Operators in C Relational Operators in C
S. No. Symbol Operator Description Syntax
The relational operators in C
Returns true if the left operand
are used for the comparison of 1 < Less than is less than the right operand. a<b
Else false
the two operands. Returns true if the left operand
Greater
2 > is greater than the right a>b
All these operators are binary than
operand. Else false
operators that return true or Less than
Returns true if the left operand
3 <= is less than or equal to the a <= b
or equal to
false values as the result of right operand. Else false
Greater Returns true if the left operand
comparison. 4 >= than or is greater than or equal to right a >= b
equal to operand. Else false
These are a total of 6 relational
Returns true if both the
5 == Equal to a == b
operators in C: operands are equal.
Not equal Returns true if both the
6 != a != b
to operands are NOT equal.
OPERATORS
2. Relational Operators in C
// C program to illustrate the
relational operators
#include <stdio.h>
int main()
{
int a = 25, b = 5;
// using operators and printing
results
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);
printf("a != b : %d\n", a != b);
return 0;
} Here, 0 means false and 1 means true.
OPERATORS
3. Logical Operator in C Logical Operator in C
Logical Operators are used S. No. Symbol Operator Description Syntax
to combine two or more Returns true if
conditions/constraints or to Logical both the
1 && a && b
AND operands are
complement the evaluation true.
of the original condition in Returns true if
consideration. Logical both or any of
2 || a || b
OR the operand is
The result of the operation true.
of a logical operator is a Returns true if
Logical
Boolean value 3 ! the operand is !a
NOT
false.
either true or false.
OPERATORS
3. Logical Operators in C
// C program to illustrate the
logical operators
#include <stdio.h>
int main()
{
int a = 25, b = 5;
// using operators and printing
results
printf("a && b : %d\n", a && b);
printf("a || b : %d\n", a || b);
printf("!a: %d\n", !a);
return 0;
}
OPERATORS
4. Bitwise Operators in C Bitwise Operators in C
S. No. Symbol Operator Description Syntax
The Bitwise operators are used to
Performs bit-by-bit AND
perform bit-level operations on the 1 & Bitwise AND operation and returns the a && b
result.
operands. Performs bit-by-bit OR
2 | Bitwise OR operation and returns the a || b
The operators are first converted to result.
Performs bit-by-bit XOR
bit-level and then the calculation is 3 ^ Bitwise XOR operation and returns the a^b
result.
performed on the operands.
Bitwise First Flips all the set and unset bits
4 ~ ~a
Mathematical operations such as Complement on the number.
Shifts the number in binary
addition, subtraction, multiplication, Bitwise form by one place in the
5 << a << b
Leftshift operation and returns the
etc. can be performed at the bit level result.
for faster processing. Shifts the number in binary
Bitwise form by one place in the
6 >> a >> b
There are 6 bitwise operators in C: Rightshilft operation and returns the
result.
OPERATORS
4. Bitwise Operators in C
// C program to illustrate the
bitwise operators
#include <stdio.h>
int main()
{
int a = 25, b = 5;
// using operators and printing
results
printf("a & b: %d\n", a & b);
printf("a | b: %d\n", a | b);
printf("a ^ b: %d\n", a ^ b);
printf("~a: %d\n", ~a);
printf("a >> b: %d\n", a >> b);
printf("a << b: %d\n", a << b);
return 0;
}
OPERATORS
Assignment Operator in C
5. Assignment Operators in C S. No. Symbol Operator Description Syntax
Simple Assign the value of the right operand to the left
Assignment operators are used to assign value 1 =
Assignment operand.
a=b
Plus and Add the right operand and left operand and assign
to a variable. 2 +=
assign this value to the left operand.
a += b
The left side operand of the assignment 3 -=
Minus and
assign
Subtract the right operand and left operand and
assign this value to the left operand.
a -= b
operator is a variable and the right side Multiply Multiply the right operand and left operand and
4 *= a *= b
and assign assign this value to the left operand.
operand of the assignment operator is a value.
Divide and Divide the left operand with the right operand and
5 /= a /= b
The assignment operators can be combined assign assign this value to the left operand.
Modulus Assign the remainder in the division of left operand
with some other operators in C to provide 6 %=
and assign with the right operand to the left operand.
a %= b
AND and Performs bitwise AND and assigns this value to the
multiple operations using single operator. 7 &=
assign left operand.
a &= b
OR and Performs bitwise OR and assigns this value to the
8 |= a |= b
These operators are called compound assign left operand.
XOR and Performs bitwise XOR and assigns this value to the
9 ^= a ^= b
operators. assign left operand.
Rightshift Performs bitwise Rightshift and assign this value to
10 >>= a >>= b
In C, there are 11 assignment operators : and assign the left operand.
Leftshift Performs bitwise Leftshift and assign this value to
11 <<= a <<= b
and assign the left operand.
OPERATORS
5. Assignment Operators in C
// C program to illustrate the arithmatic
operators
#include <stdio.h>
int main()
{
int a = 25, b = 5;
// using operators and printing results
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);
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);
return 0;
}
OPERATORS
6. Other Operators
Apart from the above operators, 2. Comma Operator ( , )
there are some other operators •The comma operator (represented by the
available in C used to perform token) is a binary operator that evaluates its
some specific tasks. first operand and discards the result, it then
Some of them are discussed here: evaluates the second operand and returns this
1. sizeof Operator: used to value (and type).
compute the size of the variable or
Syntax
datatype. operand1 , operand2
Syntax
sizeof (operand)
OPERATORS
6. Other Operators
Example of Other C Operators
3. Conditional Operator ( ? : )
To know more about the topic refer to this article. // C Program to demonstrate the use of Misc operators
#include <stdio.h>
4. dot (.) and arrow (->) Operators
int main()
To know more about dot operators refer {
to this article and to know more about arrow(->) // integer variable
int num = 10;
operators refer to this article. int* add_of_num = #
5. Cast Operator printf("sizeof(num) = %d bytes\n", sizeof(num));
printf("&num = %p\n", &num);
To know more about the topic refer to this article.
printf("*add_of_num = %d\n", *add_of_num);
6. addressof (&) and Dereference (*) printf("(10 < 5) ? 10 : 20 = %d\n", (10 < 5) ? 10 :
20);
Operators printf("(float)num = %f\n", (float)num);
To know more about the topic refer to this article. return 0;
}
OPERATORS
Operators Precedence in C
#include <stdio.h>
main()
{
int a = 20;
int b = 10;
int c = 15;
int d = 5;
int e;
e = (a + b) * c / d; // ( 30 * 15 ) / 5
printf("Value of (a + b) * c / d is :
%d\n", e );
e = ((a + b) * c) / d; // (30 * 15 ) / 5
printf("Value of ((a + b) * c) / d is :
%d\n" , e );
e = (a + b) * (c / d); // (30) * (15/5)
printf("Value of (a + b) * (c / d) is :
%d\n", e );
e = a + (b * c) / d; // 20 + (150/5)
printf("Value of a + (b * c) / d is :
%d\n" , e );
return 0;
}