CSE 112: Computer Fundamentals
and Programming Techniques
Sessional
Introduction to C Programming
Mahmudul Hasan Bhuiyan
Lecturer
Computer Science & Engineering 1
Port City International University
[email protected] What is Computer?
Computer
Device capable of performing computations and making
logical decisions
Computers process data under the control of sets of
instructions called computer programs
Hardware
Various devices comprising a computer
Keyboard, screen, mouse, disks, memory, CD-ROM, and
processing units
Software
Programs that run on a computer
2
Computer Organization
• Six logical units of computer
1. Input unit
• Obtains information from input devices
• Keyboard, mouse, microphone, scanner,
networks, …
2. Output unit
• Takes information processed by computer
• Places information on output devices
• Screen, printer, networks, …
3
• Information used to control other devices
Computer Organization
• Six logical units of computer
3. Memory unit
• Rapid access, relatively low capacity
• Retains information from input unit
• Immediately available for processing
• Retains processed information
• Until placed on output devices
• Primary memory (RAM)
4. Arithmetic and logic unit (ALU)
• Performs arithmetic calculations and logic 4
decisions
Computer Organization
• Six logical units of computer
5. Central processing unit (CPU)
• Supervises and coordinates other sections of
computer
6. Secondary storage unit
• Storage
• Inactive programs or data
• Secondary storage devices
• Hard Disk
• Longer to access than primary memory 5
• Less expensive per unit than primary memory
The von Neumann architecture
CPU
Input Device ALU CU Output Device
Main memory Secondary
(RAM) storage
6
Machine Languages, Assembly Languages,
and High-level Languages
• Three types of computer languages
1. Machine language
• Only language computer directly understands
• Defined by hardware design
• Machine-dependent
• Generally consist of strings of numbers
• Ultimately 0s and 1s
• Instruct computers to perform elementary operations
• One at a time
• Cumbersome for humans
• Example:
1000110101 7
1001000101
1001110110
Machine Languages, Assembly Languages,
and High-level Languages
• Three types of computer languages
2. Assembly language
• English-like abbreviations representing elementary
computer operations
• Clearer to humans
• Incomprehensible to computers
• Translator programs (assemblers)
• Convert to machine language
• Example:
LOAD BASEPAY
ADD OVERPAY 8
STORE GROSSPAY
Machine Languages, Assembly Languages,
and High-level Languages
• Three types of computer languages
3. High-level languages
• Similar to everyday English, use common mathematical
notations
• Single statements accomplish substantial tasks
• Assembly language requires many instructions to
accomplish simple tasks
• Translator programs (compilers)
• Convert to machine language
• Interpreter programs
• Directly execute high-level language programs 9
• Example:
grossPay = basePay + overTimePay
C History
• Developed between 1969 and 1973 along with Unix
• Due mostly to Dennis Ritchie
• Designed for systems programming
• Operating systems
• Utility programs
• Compilers
• Filters
• Evolved from B, which evolved from BCPL
10
C Programming Environment
Preprocess/
Compile/
Link
Load/
Edit Run
Editor test.c test.exe CPU
11
First C Program
/* My first simple C program */
#include <stdio.h> Comments
int main()
All C programs have a main function;
they also start at main
{
printf (“Welcome to C!\n”);
Function to print to screen
return 0;
What to print End of
} Braces indicate start statement 12
and end of main End of line
Try this…
• Print the following to the monitor using C program:
A * *****
BB ** * *
CCC *** *****
**
* *
* *
* * 13
* *
Identifiers
Names for variables, functions, macros, and other entities are called
identifiers.
An identifier may contain
letters,
digits, and
underscores,
but must begin with a letter or underscore:
times10 get_next_char _done
Examples of illegal identifiers:
10times get-next-char
C is case-sensitive: it distinguishes between upper-case and lower-
case letters in identifiers.
14
• For example, the following identifiers are all different:
job joB jOb jOB Job JoB JOb JOB
Keywords
Words reserved by the Programming Language for use by itself.
You can’t use them as identifiers.
Keywords
auto double int struct
break else long switch
case enum register typedef
char extern return union
const float short unsigned
continue for signed void
default goto sizeof volatile
do if static while
Fig. 2.15 C’s reserved keywords.
15
Variables
• C variable is a named location in a memory where a
program can store and manipulate the data. This memory
location is used to hold the value of the variable.
• The value of the C variable may get change in the
program.
• C variable might store any of the things like character,
string, number etc.
16
Data Type and Format
Specifiers
• Data types tells the compiler about the type of data so that it
can reserve memory for data. Format specifiers are used in
printf and scanf to specify the data type.
Data Purpose Size Format
Type (Byte Specifi
) er
char One character 1 %c
int The most natural size of 2 %d
integer for the machine
float A single-precision floating 4 %f
point value 17
doubl A double-precision floating 8 %lf
e point value
Displaying Output
• printf() function is used to display values
onto the output screen
• We use printf() function with format
specifiers to display the value of an
variable
• int unit = 11;
float price = 100.5;
printf(“Unit %d and Price %f”, unit, price);
• Output: Unit 11 and Price 100.5
18
Reading Input
• scanf() is the C library’s counterpart to printf()
• scanf() requires a format string to specify the appearance of
the input data.
• Example of using scanf() to read an int value:
scanf("%d", &i);
/* reads an integer; stores into i */
• The & symbol is usually required when using scanf()
• Reading a float value requires a slightly different call of scanf:
scanf("%f", &x);
• "%f" tells scanf to look for an input value in float format 19
Computing volume of a Box
/* Computes the dimensional weight of a box from input provided by the user
*/
#include <stdio.h>
int main()
{
int height, length, width, volume;
printf("Enter height of box: ");
scanf("%d", &height);
printf("Enter length of box: ");
scanf("%d", &length);
printf("Enter width of box: ");
scanf("%d", &width);
volume = height * length * width;
printf("Volume (cubic inches): %d\n", volume);
20
return 0;
}
Computing volume of a Box
• Sample output of program:
Enter height of box: 8
Enter length of box: 12
Enter width of box: 10
Volume (cubic inches): 960
21
Good Luck!
22