Professional Documents
Culture Documents
“ Programming
Fundamentals
PROBLEM SOLVING AND PROGRAM DESIGN IN C BY HANLY AND KOFFMAN ”
Objectives:
Be able to recognize the structure of C program
Be able to apprehend the use of variables and constants
Be able to realize the use of data types
Be able to learn different operators and their precedence rules
Instructor: Aatif Shahbaz 1
CLO_1
Programming
Fundamentals Contents:
Structure of C programs
Chapter 2
Constants and Variables
Problem Solving and Program
Design in C by Hanly and Data Types
Koffman Standard Output – printf( )
Standard Input – scanf( )
Operators and Precedence
Lecture 03
STRUCTURE OF C PROGRAM. HELLO WORLD EXAMPLE
Structure of C Program
Library:
A collection of useful predefined functions and symbols that may be accessed by a program.
C language is case sensitive, hence Rate, rate, and RATE are viewed by compiler as different identifiers.
Lecture 04
CONSTANTS AND VARIABLES. DATA TYPES. STANDARD OUTPUT.
Variable Declarations
The memory cells used for storing a
program’s input data and its
computational results are called
variables.
They also tell the compiler what
kind of information will be stored in
each variable and how that
information will be represented in
memory.
A variable declaration begins with
an identifier (for example, int, float,
char) that tells the C compiler
which type of data stored in a
particular variable.
C requires you to declare every
variable, before accessing it in a
program.
Variable :
A name associated with a memory cell whose value can be changed.
Constant :
A name associated with a memory cell whose value cannot be changed.
Read the topic Main Memory from section 1.2 (Computer hardware).
ASCII Code :
American Standard Code for
Information Interchange
You should know that a character
is represented in memory as an
integer(binary).
The value stored is determined by
the code used by your C
compiler.
Examples:
kms = KMS_PER_MILE * miles;
The char variable xyz is assigned the character value ‘A’ by the assignment statement:
A single character variable or value may appear on the right-hand side of a character assignment
statement.
Example :
xyz = ‘A’ ;
This statement calls function printf(pronounced “print-eff”) to display a line of program output.
A function call consists of two parts: the function name and the function arguments, enclosed in
parentheses.
The arguments for printf consist of a format string (in quotes) and a print list (the variable kms ).
Output : That equals 16.090000 kilometers.
The function call above displays the above line which is the result of displaying the format string "That equals
%f kilometers.\n" after substituting the value of kms for its placeholder ( %f ) in the format string.
Multiple Placeholders:
Format strings can have multiple placeholders.
If the print list of a printf call has several variables, the format string should contain the same number of
placeholders.
C matches variables with placeholders in left-to-right order.
Lecture 05
STANDARD INPUT. PROGRAMMING WITH VARIABLES.
Standard Input – scanf( )
The statement [ scanf("%f", &miles) ] calls function scanf(pronounced “scan-eff”) to copy data into the
variable miles .
It copies the data from the standard input device.
In most cases the standard input device is the keyboard.
Consequently, the computer will attempt to store in miles.. whatever, data the program user types at the
keyboard.
Notice that in a call to scanf, the name of each variable that is to be given a value is preceded by the
ampersand character (&), address-of operator.
In the context of this input operation, the & operator tells the scanf function where to find each variable to
store a new value.
If the ampersand were omitted, scanf would know only a variable’s current value, not its location in memory,
so scanf would be unable to store a new value in the variable.
Lecture 06
OPERATORS AND PRECEDENCE.
Arithmetic Operators
The five arithmetical operations supported by the C language are:
Addition ( + )
Subtraction ( - )
Multiplication ( * )
Division ( / )
Modulo ( % )
Examples:
Examples:
Thus above all are equivalent in its functionality: the three of them increase by one the value of c.
Examples:
Examples:
Of course, instead of using only numeric constants, we can use any valid expression, including variables.
Suppose that a=2, b=3 and c=6
Example:
Bitwise Operators:
Bitwise operators modify variables considering the bit patterns that represent the values they store.
When writing complex expressions with several operands, we may have some doubts about which operand
is evaluated first and which later.
Example:
a = 5 + 7 % 2 , We may doubt if it really means a = 5 + (7 % 2) = 6 OR a = (5 + 7) % 2 = 0
What if want are expecting opposite of what is evaluated ?
Thumb Rule:
Precedence levels for operators can be manipulated or become more legible by removing possible
ambiguities using parentheses signs ( ).
Task 2:
Write a program to perform basic arithmetic operations which are addition,
subtraction, multiplication and division of two numbers.
Prompt user about two numbers and display results to user once calculated.
Assignment 2
Instructor: Aatif Shahbaz 35
C02
Practice Problems
Instructor: Aatif Shahbaz 36