Lecture-01-Introduction-and-Basic (1)
Lecture-01-Introduction-and-Basic (1)
Prof. Dr. Rafiqul Islam Programming in C: Introduction and Basics January 18, 2025 1 / 27
Lecture Outline
Prof. Dr. Rafiqul Islam Programming in C: Introduction and Basics January 18, 2025 2 / 27
What is C?
Prof. Dr. Rafiqul Islam Programming in C: Introduction and Basics January 18, 2025 3 / 27
Why Learn C?
Prof. Dr. Rafiqul Islam Programming in C: Introduction and Basics January 18, 2025 4 / 27
Setting Up Your C Environment
Tools Needed:
IDEs: Code::Blocks, Dev-C++, or VS Code.
Compilers: GCC, Clang.
Example Task: Install GCC and run a ”Hello, World!” program.
Prof. Dr. Rafiqul Islam Programming in C: Introduction and Basics January 18, 2025 5 / 27
Writing Your First Program: ”Hello, World!”
Prof. Dr. Rafiqul Islam Programming in C: Introduction and Basics January 18, 2025 6 / 27
Writing your First Program in Code Block
Prof. Dr. Rafiqul Islam Programming in C: Introduction and Basics January 18, 2025 7 / 27
Build and Run
Prof. Dr. Rafiqul Islam Programming in C: Introduction and Basics January 18, 2025 8 / 27
Code Explained
Prof. Dr. Rafiqul Islam Programming in C: Introduction and Basics January 18, 2025 9 / 27
Statements
Prof. Dr. Rafiqul Islam Programming in C: Introduction and Basics January 18, 2025 10 / 27
Explanation
Prof. Dr. Rafiqul Islam Programming in C: Introduction and Basics January 18, 2025 11 / 27
Session 2: C Syntax, Keywords, Data Types, and Operators
Prof. Dr. Rafiqul Islam Programming in C: Introduction and Basics January 18, 2025 12 / 27
Data Types, Their Meanings, and printf Identifiers
Prof. Dr. Rafiqul Islam Programming in C: Introduction and Basics January 18, 2025 13 / 27
Example: Input and Output with Data Types
Prof. Dr. Rafiqul Islam Programming in C: Introduction and Basics January 18, 2025 14 / 27
Explanation: Input and Output Program with Data Types
Variables Used:
int age: Stores an integer value.
float height: Stores a floating-point number.
char grade: Stores a single character.
Input:
scanf("%d", &age): Reads an integer.
scanf("%f", &height): Reads a floating-point number.
scanf(" %c", &grade): Reads a character (the space before %c
ensures any previous newline is ignored).
Output:
Displays the entered values using printf with the correct format
specifiers:
%d for integer.
%.2f for floating-point (2 decimal places).
%c for character.
Prof. Dr. Rafiqul Islam Programming in C: Introduction and Basics January 18, 2025 15 / 27
Variables
Variables are containers for storing data values, like numbers and
characters.
In C, there are different types of variables (defined with different
keywords), for example:
int - stores integers (whole numbers), without decimals, such as 123
or -123
float - stores floating point numbers, with decimals, such as 19.99 or
-19.99
char - stores single characters, such as ’a’ or ’B’. Characters are
surrounded by single quotes.
Prof. Dr. Rafiqul Islam Programming in C: Introduction and Basics January 18, 2025 16 / 27
C Syntax, Data Types, and Operators
Example
Prof. Dr. Rafiqul Islam Programming in C: Introduction and Basics January 18, 2025 17 / 27
C Syntax: Variables and Data Types
3 int main () {
4 int age = 25;
5 float height = 5.9;
6 char grade = ’A ’;
7
8 printf ( " Age : %d , Height : %.1 f , Grade : % c \ n " , age , h
9 return 0;
10 }
Prof. Dr. Rafiqul Islam Programming in C: Introduction and Basics January 18, 2025 18 / 27
Input and Output Functions in C
3 int main () {
4 int num ;
5 printf ( " Enter a number : " );
6 scanf ( " % d " , & num );
7 printf ( " You entered : % d \ n " , num );
8 return 0;
9 }
Prof. Dr. Rafiqul Islam Programming in C: Introduction and Basics January 18, 2025 19 / 27
Keywords in C Programming
Keywords are reserved words in C that have special meanings and cannot
be used as variable names or identifiers. Some common C keywords
include:
int while
float for
char switch
return case
if break
else continue
Note: Keywords are case-sensitive and must be used as defined in the C
language syntax.
Prof. Dr. Rafiqul Islam Programming in C: Introduction and Basics January 18, 2025 20 / 27
Keywords in a Simple Addition Program
Prof. Dr. Rafiqul Islam Programming in C: Introduction and Basics January 18, 2025 21 / 27
Basic Operators in C
Prof. Dr. Rafiqul Islam Programming in C: Introduction and Basics January 18, 2025 22 / 27
Arithmetic Operators
Prof. Dr. Rafiqul Islam Programming in C: Introduction and Basics January 18, 2025 23 / 27
Relational Operator: Equal to (==)
Prof. Dr. Rafiqul Islam Programming in C: Introduction and Basics January 18, 2025 24 / 27
Relational Operator: Greater than (>)
Prof. Dr. Rafiqul Islam Programming in C: Introduction and Basics January 18, 2025 25 / 27
Logical Operators
3 int main () {
4 int a = 5 , b = 3;
5 printf ( " a = %d , b = % d \ n " , a , b );
6 printf ( " a > b && a != 0: % d \ n " , a > b && a != 0); /
7 printf ( " a < b || b == 3: % d \ n " , a < b || b == 3); /
8 printf ( " !( a == b ): % d \ n " , !( a == b )); // Logical NO
9 return 0;
10 }
Prof. Dr. Rafiqul Islam Programming in C: Introduction and Basics January 18, 2025 26 / 27
C Programming Exercises
Prof. Dr. Rafiqul Islam Programming in C: Introduction and Basics January 18, 2025 27 / 27