C Programming Slides 03 (Fall 2021)
C Programming Slides 03 (Fall 2021)
CSE 1111
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
– 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
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)
int a, b, c;
float f;
Variable Definitions
(Cont’d)
• There are many different types of data
int a, b, c;
float f;
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
return 0;
}
Literals
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
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;
totalSales Yes
total_Sales Yes
• 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
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;
return 0;
}
Exercise
51
Floating-Point Data Types
• Can hold real numbers i.e., fractional values
• 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;
The variable receiving the value must appear on the left side of the
= operator.
• This will NOT work:
// ERROR!
12 = item;
- subtraction ans = 7 - 3; 4
* multiplication ans = 7 * 3; 21
/ division ans = 7 / 3; 2
% modulus ans = 7 % 3; 1
Comments
– Key Words
– Programmer-Defined Identifiers
– Operators
– Punctuation
– Syntax