You are on page 1of 30

CS 110-Fundamentals of Computer Programming

Introduction
Asma Majeed

1
Revision
Agenda
Variables
To be followed in today’s lecture

Simple Expressions

Keywords

Data Types

Fundamentals of computer programming 2


C program skeleton (Rev)
• In short, the basic skeleton of a C program looks like this:

#include <stdio.h> Preprocessor directives

void main(void) Function main


{ Start of segment

statement(s);
} End of segment

Fundamentals of computer programming 3


Identifiers (Rev)
• Words used to represent certain program entities
(variables, function names, etc).
• Example:
• int my_name;
• my_name is an identifier used as a program variable
• void CalculateTotal(int value)
• CalculateTotal is an identifier used as a function name

Fundamentals of computer programming 4


Identifiers cont… (Rev)
• 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

Fundamentals of computer programming 5


Rules for naming identifiers (Rev)
Rules Example
Can contain a mix of characters and numbers. However it cannot start H2o
with a number

First character must be a letter or underscore Number1; _area

Can be of mixed cases including underscore character XsquAre


my_num
Cannot contain any arithmetic operators R*S+T
… or any other punctuation marks… #@x%!!

Cannot be a C keyword/reserved word struct; printf;


Cannot contain a space My height
… identifiers are case sensitive Tax != tax

Fundamentals of computer programming 6


Variables
• Variable  a name associated with a memory cell whose
value can change
• Variable Declaration: Telling the name and datatype of
the variable without allocating the memory space
• Variable Definition: Memory allocation to the variable
based on the data type mentioned
• Example: int num;
• Initialization: Assigning a value to the variable at the time
of/along with definition
• Example: int num = 5;
Fundamentals of computer programming 7
Variable Declarations
Example.
no t ype name; / * comment * /
1 e x t e r n i nt answer ; / * t he r esul t of our expr essi on * /
where type is one of the C variable types ( int, float, etc.) and name is any
valid variable name.
keyword extern. that this variable is already defined somewhere
keyword int. tells C that this variable contains an integer value.
variable name. is answer.
semicolon (;). marks the end of the statement, and
comment. is used to define this variable for the programmer.

Fundamentals of computer programming 8


Variables and Storage
•Variable names start with a letter or underscore ( _ )
followed by any number of letters, digits, or underscores.
• Uppercase is different from lowercase,
• sam, Sam, and SAM specify three different variables. to avoid
confusion, use different names for variables.
• Often name beginning with an underscore are
reserved for internal and system names.
• most C programmers use all-lowercase variable
names.
Fundamentals of computer programming 9
Initialization
• We can assign a value to a variable during declaration,
this is called initialization
• Variables initialized via assignment operator:
• int height = 10;
• Without initialization, variables typically have undefined
value (i.e. unknown)

Fundamentals of computer programming 10


Initializing variables examples

Note that

we can declare/initialize multiple variables at once


each variable has its own individual initialization
1 f l oat phi = 1. 6180339887;

1 i nt a, b, c = 0, d = 4;

Fundamentals of computer programming 11


Simple Expressions
expression are used to specify simple computations.
The five simple operators in C.

Operator Meaning
* Multiply
/ Divide Add
+ Subtract
- Modulus (return the remainder after division)
%

Note that,
multiply (*), divide (/), and modulus (%) have precedence over add (+)
and subtract (-).
parentheses, ( ), may be used to group terms.
Fundamentals of computer programming 12
Simple Expressions
Example.
1 ( 1 + 2) * 4

Fundamentals of computer programming 13


Simple Expressions
Example.
1 ( 1 + 2) * 4

yields 12.

Fundamentals of computer programming 14


Simple Expressions
Example.
1 ( 1 + 2) * 4

yields 12.
1 1 + 2 * 4

Fundamentals of computer programming 15


Simple Expressions
Example.
1 ( 1 + 2) * 4

yields 12.
1 1 + 2 * 4

yields 9.

Fundamentals of computer programming 16


Code example
Program computes the value of the expression (1 + 2) * 4.
1 i nt mai n( )
2 {
3 ( 1 + 2) * 4;
4 r et ur n 0;
5 }

Q. What seems strange here? Will this program work?

Fundamentals of computer programming 17


Code example
Program computes the value of the expression (1 + 2) * 4.
1 i nt mai n( )
2 {
3 ( 1 + 2) * 4;
4 r et ur n 0;
5 }

Q. What seems strange here? Will this program work?

We need to store the results of our calculations.

Fundamentals of computer programming 18


Keywords
• Reserved Words
• Keywords that identify language entities such as statements,
data types, language attributes, etc.
• Have special meaning to the compiler, cannot be used as
identifiers (variable, function name) in our program.
• Should be typed in lowercase.
• Example: const, double, int, main, void,printf, while, for, else
(etc..)

Fundamentals of computer programming 19


Keywords : can‘t be used as identifiers

auto enum unsigned break extern return void case

float short volatile char for signed while const

goto sizeof continue if static default inline struct

do int switch double long typedef else register

union

Fundamentals of computer programming 20


Basic Data Types
• There are 4 basic data types :
• int
• float
• double
• char

• int
• used to declare numeric program variables of integer type
• whole numbers, positive and negative
• keyword: int
int number;
number = 12;
Fundamentals of computer programming 21
Basic Data Types: IntegerTypes

Type Signed Storage


Unsigned Value range
size

int 2 or 4 bytes -32,768 to 32,767 or -2,147,483,648 to 2,147,483,647


unsigned int 2 or 4 bytes 0 to 65,535 or 0 to 4,294,967,295
short 2 bytes -32,768 to 32,767
unsigned short 2 bytes 0 to 65,535
long 4 bytes -2,147,483,648 to 2,147,483,647
unsigned long 4 bytes 0 to 4,294,967,295

22
Basic Data Types cont…
• float
• fractional parts, positive and negative
• keyword: float
float height;
height = 1.72;
• double
• used to declare floating point variable of higher precision or higher range of
numbers
• exponential numbers, positive and negative
• keyword: double
double valuebig;
valuebig = 12E-3;
Fundamentals of computer programming 23
Basic Data Types: Floating-pointTypes

Type signed Storage unsigned Value range Precision


size

float 4 byte 1.2E-38 to 3.4E+38 6 decimal places

double 8 byte 2.3E-308 to 1.7E+308 15 decimal places

long double 10 byte 3.4E-4932 to 1.1E+4932 19 decimal places

24
Basic Data Types cont…
• char
• equivalent to ‘letters’ in English language
• Example of characters:
• Numeric digits: 0 - 9
• Lowercase/uppercase letters: a - z and A - Z
• Space (blank)
• Special characters: , . ; ? “ / ( ) [ ] { } * & % ^ < > etc
• single character
• keyword: char
char my_letter; The declared character must be enclosed
my_letter = 'U'; within a single quote!

• In addition, there are void, short, long, etc.


Fundamentals of computer programming 25
Basic Data Types: DatatypesSize

•The individual sizes are machine/compiler


dependent. However, following is guaranteed:
• sizeof(char) < sizeof(short) <= sizeof(int) <= sizeof(long)

• sizeof(char) < sizeof(short) <= sizeof(float) <= sizeof(double)

Fundamentals of computer programming 26


Basic Data Types: Placeholder / Conversion Specifier

No Conversion Output Type Output Example


Specifier
1 %d Signed decimal integer 76
2 %i Signed decimal integer 76
3 %o Unsigned octal integer 134
4 %u Unsigned decimal integer 76
5 %x Unsigned hexadecimal (small letter) 9c
6 %X Unsigned hexadecimal (capital letter) 9C
7 %f Integer including decimal point 76.0000
8 %e Signed floating point (using e notation) 7.6000e+01
9 %E Signed floating point (using E notation) 7.6000E+01
10 %g The shorter between %f and %e 76
11 %G The shorter between %f and %E 76
12 %c Character ‘7’
13 %s String ‘76'

Fundamentals of computer programming 27


Constants
• Entities that appear in the program code as fixed values.
• Any attempt to modify a CONSTANT will result in error.
• 4 types of constants:
• Integer constants
• Positive or negative whole numbers with no fractional part
• Example:
• const int MAX_NUM = 10;
• const int MIN_NUM = -90;
• Floating-point constants (float or double)
• Positive or negative decimal numbers with an integer part, a decimal point and a fractional part
• Example:
• const double VAL = 0.5877e2; (stands for 0.5877 x 102)

Fundamentals of computer programming 28


Constants cont…
• Character constants
• A character enclosed in a single quotation mark
• Example:
• const char letter = ‘n’;
• const char number = ‘1’;
• printf(“%c”, ‘S’);
• Output would be: S

• Enumeration
enum Language {
• Values are given as a list Malay,
English,
• Example: Arabic
};

Fundamentals of computer programming 29


Constant example – volume of a cone

#include <stdio.h>

void main(void)
{
const double pi = 3.412;
double height, radius, base, volume;

printf(“Enter the height and radius of the cone:”);


scanf(“%lf %lf”,&height, &radius);

base = pi * radius * radius;


volume = (1.0/3.0) * base * height;

printf(“\nThe volume of a cone is %f ”, volume);


}
Fundamentals of computer programming 30

You might also like