0% found this document useful (0 votes)
2 views

Lecture-01-Introduction-and-Basic (1)

The document is a lecture outline on programming in C, covering its introduction, setup, basic structure, and writing a simple program. It explains the significance of C as a foundational language in computer science, its syntax, data types, and operators. Additionally, it includes practical examples and exercises to reinforce learning.

Uploaded by

Asif Rahat
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

Lecture-01-Introduction-and-Basic (1)

The document is a lecture outline on programming in C, covering its introduction, setup, basic structure, and writing a simple program. It explains the significance of C as a foundational language in computer science, its syntax, data types, and operators. Additionally, it includes practical examples and exercises to reinforce learning.

Uploaded by

Asif Rahat
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 27

Programming in C: Introduction and Basics

Prof. Dr. Rafiqul Islam

January 18, 2025

Prof. Dr. Rafiqul Islam Programming in C: Introduction and Basics January 18, 2025 1 / 27
Lecture Outline

Introduction to Programming and C.


Setting up the C development environment (IDE, compiler).
Basic structure of a C program: #include, main(), and {}.
Writing and running your first program: ”Hello, World!”

Prof. Dr. Rafiqul Islam Programming in C: Introduction and Basics January 18, 2025 2 / 27
What is C?

C is a general-purpose programming language created by Dennis


Ritchie at the Bell Laboratories in 1972.
It is a very popular language, despite being old. The main reason for
its popularity is because it is a fundamental language in the field of
computer science.
C is strongly associated with UNIX, as it was developed to write the
UNIX operating system.

Prof. Dr. Rafiqul Islam Programming in C: Introduction and Basics January 18, 2025 3 / 27
Why Learn C?

It is one of the most popular programming languages in the world


If you know C, you will have no problem learning other popular
programming languages such as Java, Python, C++, C#, etc, as the
syntax is similar
C is very fast, compared to other programming languages, like Java
and Python
C is very versatile, it can be used in both applications and
technologies

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!”

The following C program displays ”Hello, World!” on the screen:


1 # include < stdio .h >
2
3 int main () {
4 printf ( " Hello , World ! " );
5 return 0;
6 }

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

Line 1: #include<stdio.h> is a header file library that lets us work


with input and output functions, such as printf() (used in line 4).
Header files add functionality to C programs.
Line 2: A blank line. C ignores white space. But we use it to make
the code more readable.
Line 3: Another thing that always appear in a C program is main().
This is called a function. Any code inside its curly brackets will be
executed.
Line 4: printf() is a function used to output/print text to the screen.
In our example, it will output ”Hello World!”.
Note that: Every C statement ends with a semicolon;

Prof. Dr. Rafiqul Islam Programming in C: Introduction and Basics January 18, 2025 9 / 27
Statements

C programs contain many statements.


The statements are executed, one by one, in the same order as they
are written:

Prof. Dr. Rafiqul Islam Programming in C: Introduction and Basics January 18, 2025 10 / 27
Explanation

The first statement is executed first (print ”Hello World!” to the


screen).
Then the second statement is executed (print ”Have a good day!” to
the screen).
And at last, the third statement is executed (end the C program
successfully).

Prof. Dr. Rafiqul Islam Programming in C: Introduction and Basics January 18, 2025 11 / 27
Session 2: C Syntax, Keywords, Data Types, and Operators

C syntax: keywords, identifiers, and variables.


Data types and constants.
Input and output: printf(), scanf().
Basic operators: arithmetic, relational, logical.

Prof. Dr. Rafiqul Islam Programming in C: Introduction and Basics January 18, 2025 12 / 27
Data Types, Their Meanings, and printf Identifiers

Common Data Types in C:


int: Integer values (e.g., -10, 0, 100)
float: Floating-point numbers (e.g., 3.14, -0.5)
double: Double-precision floating-point numbers
char: Single character (e.g., ’A’, ’z’)

printf Format Specifiers:


%d or %i: For integers (int)
%f: For floating-point numbers (float)
%lf: For double-precision floating-point numbers (double)
%c: For characters (char)
%s: For strings (array of char)

Prof. Dr. Rafiqul Islam Programming in C: Introduction and Basics January 18, 2025 13 / 27
Example: Input and Output with Data Types

1 # include < stdio .h >


2 int main () {
3 int age ;
4 float height ;
5 char grade ;
6 // Input values from the user
7 printf ( " Enter your age : " );
8 scanf ( " % d " , & age );
9 printf ( " Enter your height ( in meters ) and grade (A ,
10 scanf ( " % f % c " , & height , & grade );
11
12 // Display the input values
13 printf ( " Age : % d \ n " , age );
14 printf ( " Height : %.2 f meters \ n " , height );
15 printf ( " Grade : % c \ n " , grade );
16 return 0;
17 }

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

Example of declaring variables and performing operations in C:


1 # include < stdio .h >
2

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

Example of using scanf() for user input:


1 # include < stdio .h >
2

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

Example: Adding Two Numbers Using Keywords


1 # include < stdio .h >
2
3 int main () {
4 int num1 = 10; // Keyword : int
5 int num2 = 20; // Keyword : int
6 int sum ; // Keyword : int
7
8 sum = num1 + num2 ; // Calculate sum
9
10 printf ( " The sum is : % d \ n " , sum ); // Keyword : printf
11

12 return 0; // Keyword : return


13 }

Output: The sum is: 30

Prof. Dr. Rafiqul Islam Programming in C: Introduction and Basics January 18, 2025 21 / 27
Basic Operators in C

C provides several types of operators:


Arithmetic operators: +, -, *, /, %.
Relational operators: ==, !=, <, >, <=, >=.
Logical operators: &, &&, ||.

Prof. Dr. Rafiqul Islam Programming in C: Introduction and Basics January 18, 2025 22 / 27
Arithmetic Operators

Example: Using Arithmetic Operators


1 # include < stdio .h >
2
3 int main () {
4 int a = 10 , b = 3;
5 printf ( " a = %d , b = % d \ n " , a , b );
6 printf ( " a + b = % d \ n " , a + b ); // Addition
7 printf ( " a - b = % d \ n " , a - b ); // Subtraction
8 printf ( " a * b = % d \ n " , a * b ); // Multiplication
9 printf ( " a / b = % d \ n " , a / b ); // Division
10 printf ( " a %% b = % d \ n " , a % b ); // Modulus
11 return 0;
12 }

Prof. Dr. Rafiqul Islam Programming in C: Introduction and Basics January 18, 2025 23 / 27
Relational Operator: Equal to (==)

Example: Using the Equal to Operator


1 # include < stdio .h >
2
3 int main () {
4 int a = 5 , b = 5;
5 printf ( " a = %d , b = % d \ n " , a , b );
6 printf ( " a == b : % d \ n " , a == b ); // Outputs 1 ( true )
7 return 0;
8 }

Prof. Dr. Rafiqul Islam Programming in C: Introduction and Basics January 18, 2025 24 / 27
Relational Operator: Greater than (>)

Example: Using the Greater than Operator


1 # include < stdio .h >
2
3 int main () {
4 int a = 8 , b = 3;
5 printf ( " a = %d , b = % d \ n " , a , b );
6 printf ( " a > b : % d \ n " , a > b ); // Outputs 1 ( true )
7 return 0;
8 }

Prof. Dr. Rafiqul Islam Programming in C: Introduction and Basics January 18, 2025 25 / 27
Logical Operators

Example: Using Logical Operators


1 # include < stdio .h >
2

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

Exercise 1: Data Types and printf


Write a C program that declares variables of different data types (int,
float, char) and prints their values.

Exercise 2: Arithmetic Operators


Write a C program that takes two integers as input from the user and
outputs their sum, difference, product, and quotient.

Exercise 3: Relational Operators


Write a C program that checks if two numbers entered by the user are
equal or not.

Prof. Dr. Rafiqul Islam Programming in C: Introduction and Basics January 18, 2025 27 / 27

You might also like