0% found this document useful (0 votes)
78 views65 pages

C Programming Slides 03 (Fall 2021)

The document outlines the structure of a programming course in C including topics to be covered, assessment breakdown, grading policy and key parts of a C program such as comments, preprocessor commands, functions, and output statements. It also provides examples of basic C code and explanations of common programming elements like variables, literals, operators, and data types.

Uploaded by

Monika MORAN
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
78 views65 pages

C Programming Slides 03 (Fall 2021)

The document outlines the structure of a programming course in C including topics to be covered, assessment breakdown, grading policy and key parts of a C program such as comments, preprocessor commands, functions, and output statements. It also provides examples of basic C code and explanations of common programming elements like variables, literals, operators, and data types.

Uploaded by

Monika MORAN
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 65

Structured Programming Language

CSE 1111

Prof. Dr. A.K.M. Muzahidul Islam


Computer Science & Engineering (CSE)
United International University (UIU)

Fall 2021
Assessment
– Attendance : 5%
– Assignments : 5%
– Class Tests : 20%
– Mid Term : 30%
– Final : 40%

Grading Policy
A (Plain) : 90-100 C+ (Plus) : 70-73
A- (Minus) : 86-89 C (Plain) : 66-69
B+ (Plus) : 82-85 C- (Minus) : 62-65
B (Plain) : 78-81 D+ (Plus) : 58-61
B- (Minus) : 74-77 D (Plain) : 55-57
Course Outline: Summary
1. Topic 1: Review
• Algorithm, Flow-chart, Pseudocode, Variable, Logic
2. Topic 2: Decision (If/Else-if/Else)
3. Topic 3: Decision (Switch)
4. Topic 4: Loop (For/While/Do-while)
5. Topic 5: Array (1D and 2D)
6. Topic 6: String
7. Topic 7: Nested Loop
8. Topic 8: Function
9. Topic 9: Recursion
10. Topic 10: Structure
11. Topic 11: Pointer
12. Topic 12: File
The Parts of a C Program
//A simple C program comment
#include <stdio.h> preprocessor
command

int main() beginning of function named main


{ beginning of block for main
printf ("Hello, world\n”); output statement
return 0; string literal
send 0 to operating system and terminates the main
} end of block for main function
• Preprocessor-
– It setups the source code for the compiler. It reads your program before it is
compiled.
– It tells a C compiler to Include stdio.h file before going to actual compilation.
What is a Program Made of?

• Common elements in programming


languages:

– Key Words
– Programmer-Defined Identifiers
– Operators
– Punctuation
– Syntax
Program 1-1
//A simple C program
#include <stdio.h>

int main()
{
// Variable Definition
int a, b;
int c;
float f;

// Actual Initialization
a = 10;
b = 20;
c = a + b;
printf (”value of c: %d \n”, c); f =
70.0 / 3.0;
printf (”value of f: %f \n”, f);

return 0;
}
Key Words
• Also known as reserved words
• Have a special meaning in C
• Can not be used for any other purpose
• Key words in the Program 1-1: int, float
and return
Key Words
//A simple C program
#include <stdio.h>

int main()
{
// Variable Definition
int a, b;
int c;
float f;

// Actual Initialization
a = 10;
b = 20;

c = a + b;
printf (”value of c: %d \n”, c);

f = 70.0 / 3.0;
printf (”value of f: %f \n”, f);
return 0;
}
C Key Words
Programmer-Defined
Identifiers

• Names made up by the programmer


• Not part of the C language
• Used to represent various things:
variables (memory locations), functions,
etc.
• In Program 1-1: a, b, c, and f.
Identifiers
//A simple C program
#include <stdio.h>

int main()
{
// Variable Definition
int a, b;
int c;
float f;

// Actual Initialization
a = 10;
b = 20;

c = a + b;
printf (”value of c: %d \n”, c);

f = 70.0 / 3.0;
printf (”value of f: %f \n”, f);
return 0;
}
Operators
• Used to perform operations on data
• Many types of operators:
– Arithmetic - ex: +,-,*,/
– Assignment – ex: =
Operators
//A simple C program
#include <stdio.h>

int main()
{
// Variable Definition
int a, b;
int c;
float f;

// Actual Initialization
a = 10;
b = 20;
c = a + b;
printf (”value of c: %d \n”, c);

f = 70.0 / 3.0;
printf (”value of f: %f \n”, f);
return 0;
}
Punctuation
• Characters that mark the end of a
statement, or that separate items in a list
• In Program 1-1: , and ;
Punctuation
//A simple C program
#include <stdio.h>

int main()
{
// Variable Definition
int a, b;
int c;
float f;

// Actual Initialization
a = 10;
b = 20;

c = a + b;
printf (”value of c: %d \n”, c);

f = 70.0 / 3.0;
printf (”value of f: %f \n”, f);
return 0;
}
Syntax
• The rules of grammar that must be followed
when writing a program
• Controls the use of key words, operators,
programmer-defined symbols, and
punctuation
Variables
• A variable is a named storage location in
the computer’s memory for holding a piece
of data.
• In Program 1-1 we used three variables:
– The a, b, c and f variables were used to
hold the values
Variable Definitions
• To create a variable in a program you must
write a variable definition (also called a
variable declaration)

• Here is the statement from Program 1-1 that


defines the variables:

int a, b, c;
float f;
Variable Definitions
(Cont’d)
• There are many different types of data

• A variable holds a specific type of data.

• Variable definition specifies the type of


data a variable can hold, and the variable
name.
• char
• int
• float
• double
Variable Definitions
(Cont’d)
• Once again, line 7 from Program 1-1:

int a, b, c;
float f;

• The word float specifies that the


variables can hold floating point numbers.
Variable Definitions
(Cont’d)
• Following are the examples of some very
common data types used in C:
• char: The most basic data type in C. It stores a single
character and requires a single byte of memory in
almost all compilers.
• int: As the name suggests, an int variable is used to
store an integer.
• float: It is used to store decimal numbers (numbers
with floating point value) with single precision.
• double: It is used to store decimal numbers (numbers
with floating point value) with double precision.
• int a = 1;
• char b ='G';
• double c = 3.14;
Exercise 1
• There are Four sections in Fall 2021 who
have taken CSE 1111 course, where 35 in
Sec A, 40 in Sec B, 30 in Sec C and 45 in
Sec D. What is the total number of
students in Fall 2021 who have taken
taken CSI 1111 course?

Write a C code to display the total


number.
Exercise 2
• There are total 100 students in CSE.
However, only 35 of them took CSE 1111
course.

Write a C code to display how many


percentage is attending the course?
Special Characters
Character Name Meaning
// Double slash Beginning of a comment
# Pound sign Beginning of preprocessor
directive
< > Open/close brackets Enclose filename in #include
( ) Open/close Used when naming a
parentheses function
{ } Open/close brace Encloses a group of
statements
" " Open/close Encloses string of
quotation marks characters
; Semicolon End of a programming
statement
The \n Escape Sequence
• You can also use the \n escape sequence
to start a new line of output. This will
produce two lines of output:

printf( "Programming is\n”);

Notice that the \n is INSIDE


the string.
The #include Directive

• Inserts the contents of another file into the


program
• This is a preprocessor directive, not part of
C language
• #include lines not seen by compiler
• Do not place a semicolon at end of
#include line
Variables and Literals
• Variable: a storage location in memory

– Has a name and a type of data it can hold


– Must be defined before it can be used:

int area;
area = 100;
Variable Definition in Program 1-
2
//A simple C program to calculate the area
#include <stdio.h>

int main()
{
// Variable Definition & Initialization
int length = 10;
int width = 5;
int area;
Variable Definition

// Calculating area

area = length * width;


printf (”value of area: ”, area);

return 0;
}
Literals

• Literal: a value that is written into a


program’s code.

"value of area:" (string literal)


12 (integer literal)
Integer Literal in Program 1-3
//A simple C program to calculate the area
#include <stdio.h>

int main()
{
// Variable Definition & Initialization
int length;
int width;
int area;
20 is an integer literal

length = 20;
width = 10;

// Calculating area
area = length * width;
printf (”value of area: ”, area);
return 0;
}
String Literals
//A simple C program to calculate the area
#include <stdio.h>
#define PI 3.14159

int main()
{ This is string literals
// Variable Definition & Initialization
int length = 10;
int width = 5;
int area;

// Calculating area

area = length * width;


printf (”value of area: ”, area);

return 0;
}
Defining Constants
• There are two simple ways in C to define
constants
– Using #define preprocessor
• #define identifier value
– Using const keyword
• const type variable = value
Examples
#include <stdio.h> #include <stdio.h>

#define LENGTH 10
#define WIDTH 5 #int main() {
#define NEWLINE '\n' const int LENGTH = 10;
const int WIDTH = 5;
const char NEWLINE = '\n';
int main() {
int area;
int area;
area = LENGTH * WIDTH;
area = LENGTH * WIDTH;
printf("value of area : %d",
area); printf("value of area : %d", area);
printf("%c", NEWLINE); printf("%c", NEWLINE);
return 0; return 0;
} }
Identifiers

• An identifier is a programmer-defined
name for some part of a program:
variables, functions, etc.
Variable Names
• A variable name should represent the
purpose of the variable. For example:

int x;
• int itemsOrdered;

The purpose of this variable is to hold the


number of items ordered.
Identifier Rules
• The first character of an identifier must be
an alphabetic character or an underscore
( _ ),
• After the first character you may use
alphabetic characters, numbers, or
underscore characters.
• Upper- and lowercase characters are
distinct
Valid and Invalid Identifiers

IDENTIFIER VALID? REASON IF INVALID

totalSales Yes

total_Sales Yes

total.Sales No Cannot contain .

4thQtrSales No Cannot begin with digit

totalSale$ No Cannot contain $


Integer Data Types
• Integer variables can hold whole numbers such as 12,
7, and -99.
Defining Variables
• Variables of the same type can be defined
- On separate lines:
int length;
int width;
unsigned int area;
- On the same line:
int length, width;
unsigned int area;
• Variables of different types must be in
different definitions
– int length;
– unsigned int area;
Exercise 3
• Write a C program to calculate
the area of a circle (A = pi *
radius2).

• printf(“Radius = ?”);
• scanf ( “%f ”, &radius ) ;

44
The char Data Type
• Used to hold characters or very small
integer values
• Usually 1 byte of memory
• Numeric value of character from the
character set is stored in memory:

CODE: MEMORY:
char letter; letter
letter = 'C';
67
Character Literals
• Character literals must be enclosed in single
quote marks. Example:

'A'
• printf(control string, arg1, arg2, . . . , argn)
– where control string refers to a string that contains
formatting information, and arg1, arg2, . . . , argn are
arguments that represent the individual output data
items.
Character Literals in Program

//A simple C program to calculate the area


#include <stdio.h>

int main()
{
// Variable Definition & Initialization
char letter;

letter = ‘B’;
printf (”value of the letter is: %c”, letter);

letter = ‘C’;
printf (”value of the letter is: %c”, letter);

return 0;
}
double in Program
//A simple C program to calculate the area
#include <stdio.h>

int main()
{
// Variable Definition & Initialization
double val1, val2;

scanf (“Enter values: %lf ”, &val1, &val2 );


printf (”value is: %lf”, val1, val2);

return 0;
}
Exercise

• Write a program that asks the


users to perform the following
processes:
– Display : 1. Obtained grades in Math:
– Input : mathGrade;
– Display : 2. Obtained grade in Programing:
– Input : programmingGrade;
– Display : 1. Grade in Math is : B
– Display : 2. Grade in Progmramming is : A
Exercise

• Write a program that asks the


users to input two integer
values:
– Task 1. Perform addition
– Task 2. Perform subtraction
– Task 3. Perform multiplication
– Task 4. Perform division
Commonly Used Conversion
Characters for Data Output
Conversion Character Meaning
• c Data item is displayed as a single character
• d  Data item is displayed as a signed decimal integer
• e  Data item is displayed as a floating-point value with an exponent
• f  Data item is displayed as a floating-point value without an exponent
• 0 Data item is displayed as an octal integer, without a leading zero
• S Data item is displayed as a string
• U Data item is displayed as an unsigned decimal integer
• X Data item is displayed as a hexadecimal integer, without the leading Ox

51
Floating-Point Data Types
• Can hold real numbers i.e., fractional values

• The floating-point data types are:


float
double
long double

• They can hold real numbers such as:


12.45 -3.8

• Stored in a form similar to scientific notation

• All floating-point numbers are signed


Floating-Point Data Types
Floating-Point Literals

• Can be represented in
– Fixed point (decimal) notation:
31.4159 0.0000625
– E notation:
3.14159E1 6.25e-5
• Are double by default
• Can be forced to be float (3.14159f) or
long double (0.0000625L)
Variable Assignments and
Initialization
• An assignment statement uses the =
operator to store a value in a variable.

item = 12;

• This statement assigns the value 12 to the


item variable.
Assignment and Initialization

The variable receiving the value must appear on the left side of the
= operator.
• This will NOT work:
// ERROR!
12 = item;

To initialize a variable means to assign it a value when it is defined:

int length = 12;

• Can initialize some or all variables:


int length = 12, width = 5, area;
Arithmetic Operators
• Used for performing numeric calculations
• C++ has unary, binary, and ternary operators:
– unary (1 operand)
• ex. int value = 12;
– binary (2 operands)
• ex. value = val1 + val2
• Ex. if (a == b)
– ternary (3 operands) exp1 ? exp2 : exp3
Ex. a < b : true
Binary Arithmetic Operators

SYMBOL OPERATION EXAMPLE VALUE OF


ans
+ addition ans = 7 + 3; 10

- subtraction ans = 7 - 3; 4

* multiplication ans = 7 * 3; 21

/ division ans = 7 / 3; 2

% modulus ans = 7 % 3; 1
Comments

• Used to document parts of the program


• Intended for persons reading the source
code of the program:
– Indicate the purpose of the program
– Describe the use of variables
– Explain complex sections of code
• Are ignored by the compiler
Single-Line/Multiple Lines Comments
Begin with // through to the end of line:
int length = 12; // length in inches
int width = 15; // width in inches
int area; // calculated area

// calculate rectangle area


area = length * width;
Begin with /*, end with */
/* this is a multi-line
comment
*/
• Can begin and end on the same line:
int area; /* calculated area */
Named Constants
• Named constant (constant variable):
variable whose content cannot be changed
during program execution

• Used for representing constant values with


descriptive names:
const double TAX_RATE = 0.0675;
const int NUM_STATES = 50;

• Often named in uppercase letters


Programming Style

• The visual organization of the source code


• Includes the use of spaces, tabs, and blank
lines
• Does not affect the syntax of the program
• Affects the readability of the source code
Summary
• Common elements in programming languages:

– Key Words
– Programmer-Defined Identifiers
– Operators
– Punctuation
– Syntax

You might also like