Introduction First Class
Introduction First Class
• But…
• You need to speak its language
• Tell computers exactly what operations to do,
step by step
• Programming…
Three steps in writing programs
Step 1: Write the program in a high-level language (HLL - in your case, C)
Step 2: Compile the program using a C compiler
Step 3: Run / execute the program (ask the computer to execute it)
HLL
program Executable
Compiler code
Library
Numbers are represented inside computers in the base-2 system (Binary Numbers)
• Only two symbols/digits 0 and 1
• Positional weights of digits: 20, 21, 22,…from right to left for integers
• Example: 1101 in binary = (1 x 20) + (0 x 21) + (1 x 22) + (1 x 23) = 13 in decimal
Bits and Bytes
• Bit – a single 1 or 0
• Number of integers that can be represented in 1 byte = 256 (the integers 0, 1, 2, 3,….255)
Problem solving: typical flow
Step 1:
• Clearly specify the problem to be solved.
Step 2:
• Draw flowchart or write algorithm (represent the sequence of operations needed).
Step 3:
• Convert flowchart (algorithm) into program code.
Step 4:
• Compile the program to get executable code.
Step 5:
• Execute the program on a computer.
Flowchart: represent the sequence of operations as a diagram
Computation
Input / Output
Decision Box
Start / Stop
Flow of control
Connector
Flowchart: Example 1: adding three numbers
START
READ A, B,
C
S=A+B+C
OUTPUT S
STOP
Flowchart: Example 2: find larger of two numbers
START
READ X, Y
YES IS
NO
X>Y?
OUTPUT X OUTPUT Y
STOP STOP
Flowchart: Example 3: sum first N integers
START
READ N
SUM = 0
COUNT = 1
COUNT = COUNT + 1
NO IS YES
COUNT > N? OUTPUT SUM
STOP
Flowchart: Example 4: compute factorial of a number
START
READ N
PROD = 1
COUNT = 1
COUNT = COUNT + 1
NO IS YES
COUNT > N? OUTPUT PROD
STOP
Fundamentals of C
First C program – print on screen
#include <stdio.h>
int main()
Output
{
Hello, World!
printf ("Hello, World! \n") ;
return 0;
}
A Simple C program
#include <stdio.h>
int main()
When you run the program
{
int x, y, sum, max;
scanf(“%d%d”, &x, &y);
sum = x + y;
if (x > y) max = x;
else max = y; Output after you type 15 and 20
• Statements are executed one by one in top-down order (though some special instructions
differ from this top-down order)
Things you will see in a C program
(we will look at all these one by one)
• Variables
• Constants
• Expressions (Arithmetic, Logical, Assignment)
• Statements (Declaration, Assignment, Control (Conditional/Branching, Looping))
• Arrays
• Functions
• Structures
• Pointers
The C Character Set
The C language alphabet
• Uppercase letters ‘A’ to ‘Z’
• Lowercase letters ‘a’ to ‘z’
• Digits ‘0’ to ‘9’
• Certain special characters:
! # % ^ & * ( )
– _ + = ~ [ ] \
| ; : ‘ “ { } ,
. < > / ?
whitespace characters (space, tab, …)
MEMORY
Example
#include <stdio.h>
int main( )
{
int x;
int y;
The = sign in a C program does NOT denote equality
return 0;
}
Variables in Memory
X = X +1
X = X*5
X = 10
after executing X = 1
0
X 10
X = 20
X = X +1
X = X*5
X = 10
e c uti ng X = 20 X 20
X = 20 after e x
X = X +1
X = X*5
X = 10
1 X 21
X = 20 = X+
gX
ec utin
te r ex
af
X=X+1
X=X*5
X = 10
X 105
X = 20
* 5
X =X
X=X+1 ti ng
x ecu
fte re
a
X=X*5
X = 20
(may contain whatever
X ? value at the beginning)
Y = 15
Y=X/6
Variables in Memory
X = 20
after executing X = 2
0
X 20
Y = 15
X=Y+3 Y ?
Y=X/6
Variables in Memory
X = 20
X 20
Y = 15
after execu
ting Y = 15
X=Y+3 Y 15
Y=X/6
Variables in Memory
X = 20
X 18
Y = 15
Y=X/6
X = 20
X 18
Y = 15
X=Y+3 6 Y 3
Y =X/
ti ng
e xecu
after
Y=X/6
Data Types
• Each variable has a type, indicates what type of values the variable can hold
• First rule of variable use: Must declare a variable (specify its type and name) before using
it anywhere in your program
• All variable declarations should ideally be at the beginning of the main( ) or other
functions
• There are exceptions, we will see later
• A value can also be assigned to a variable at the time the variable is declared.
int speed = 30;
Initialization of a variable
char flag = ‘y’;
Variable Names
• Sequence of letters and digits
• First character must be a letter or ‘_’
• No special characters other than ‘_’
• No blank in between
• Names are case-sensitive (max and Max are two different names)
• Specific terms used by the C language having specific meaning, cannot be used as variable
names
• Examples:
int, float, char, double, main, if else, for, while, do, struct, union, typedef, enum, void,
return, signed, unsigned, case, break, sizeof,….
Variable list (note the & before d for int type variable
each variable name)
f for float type variable
scanf( “%d%d%f”, &a, &b, &c); lf for double type variable
Format string
Examples
scanf ("%d", &size) ;
• Reads one integer from keyboard into an int type variable named size
• scanf( ) will wait for you to type the input from the keyboard
• You must type the same number of inputs as the number of %’s in the format string
• Example: if you have scanf (“%d%d”,…), then you must type two integers (in same line or
different lines), otherwise scanf( ) will just wait and the next statement will not be
executed
Reading a single character
• Program waits at the getchar() line until a character is typed, and then reads it and stores it
in c
Output: printf function
• Performs output to the standard output device (typically defined to be the screen)
• The format specification %d causes the value listed after the format string to be
embedded in the output as a decimal number in place of %d
• format string refers to a string containing formatting information and data types of the
arguments to be output
• the arguments arg1, arg2, … represent list of variables/expressions whose values are to be
printed
• The conversion characters are the same as in scanf
• Examples:
printf (“Average of %d and %d is %f”, a, b, avg);
printf (“Hello \nGood \nMorning \n”);
printf (“%3d %3d %5d”, a, b, a*b+2);
printf (“%7.2f %5.1f”, x, y);
• Many more options are available for both printf and scanf – read from the book
More Examples
(Explain the outputs to test if you understood format strings etc.)
More point
Output
#include <stdio.h>
Hello, World!
int main() Hello
{ World!
printf ("Hello, World! \n") ; Hell
printf ("Hello \n World! \n") ; o World!
printf ("Hell\no \t World! \n") ;
return 0;
}
Some more point
Output
Enter values for f1 and f2:
#include <stdio.h>
23.5 14.326
int main() Enter values for x1 and x2:
54 7
{
f1 = 23.500000, f2 = 14.33
float f1, f2; x1 = 54, x2 = 7
int x1, x2;
printf(“Enter values for f1 and f2: \n”); Can you explain why 14.326
scanf(“%f%f”, &f1, &f2); got printed as 14.33?
printf(“Enter values for x1 and x2: \n”);
scanf(“%d%d”, &x1, &x2);
printf(“f1 = %f, f2 = %5.2f\n”, f1, f2);
printf(“x1 = %d, x2 = %10d\n”, x1, x2);
return 0;
}