Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 29

DEC20012 Programming

Fundamentals

CHAPTER 2:
FUNDAMENTALS OF
C
LANGUAGE

E D I T E D B Y : P U A N H J H WA N R O S L I N A B I N T I WA N M U S A

JKE, POLISAS

1
DEC20012

COURSE CONTENT

Fundamentals of C Language

2.1 Variables, Constants and Data Types


2.2 Fundamentals of C Programming
2.3 Apply Fundamentals of C Programming

2
DEC20012

Variables, Constants and Data Types

1. Identify variables in the command below.

Command :-

const float pi = 3.145;

float area, radius;

3
DEC20012

Variables, Constants and Data Types


Variables

Variable

const float pi = 3.145;

float area, radius;

Variables

4
DEC20012

Variables, Constants and Data Types


Variables
Data Types
Semicolon

const float pi = 3.145;


DECLARATION
float area, radius;

Variable

5
DEC20012

Variables, Constants and Data Types


Variables

 variable is a named location in a memory where a


program can manipulate the data
 This location is used to hold the value of the
variable.
 This value may changed in the program
*In short, a variable is a place to store a piece of information.
Just as you might store a friend’s phone number in your own memory.

6
DEC20012

Variables, Constants and Data Types


Declaring C Variables
 Variables should be declared in the C program before used.
 Memory space is not allocated for a variable while
declaration. It happens only on variable definition.
1. Syntax:
data_type variable_name;
2. Example command to declare variable : 
int marks;

7
DEC20012

Variables, Constants and Data Types


Initializing C Variables

 Variable initialization means assigning a value to the


variable.
1. Syntax:
data_type variable_name = value;
2. Example: 
int x = 5;
char alphabet = ‘Z’;

8
DEC20012

Variables, Constants and Data Types


Constants

 Constant – Value that do not change


during program execution.
 Types of constant:
1) Integer Constant
2) Character & String Constant
 The name of the constant or variable
are also called Identifiers

9
DEC20012

Variables, Constants and Data Types


Declaring & Initializing C Constants
To declare a constant, use keyword const.
1. Syntax:
const data_type constant_name = value;
2. Example: 
const int days_of_week=7;

const char alphabet= ‘G’;

const string Answer = “Yes”;


10
DEC20012

Variables, Constants and Data Types


C Character Set
character:- It denotes any alphabet, digit or special
symbol used to represent information.

Use:- These characters can be combined to form


variables. C uses constants, variables, operators,
keywords and expressions as building blocks to form a
basic C program.

Character set:- The character set is the fundamental


raw material of any language and they are used to
represent information. Like natural languages, computer
language will also have well defined character set, which
is useful to build the programs.

11
DEC20012

Variables, Constants and Data Types


C Character Set

The characters in C are grouped into the following two


categories:
1.      Source character set
         a.      Alphabets
         b.      Digits
         c.      Special Characters
         d.      White Spaces
2.      Execution character set
         a.     Escape Sequence

12
DEC20012

Variables, Constants and Data Types


C Character Set

Source character set


ALPHABETS
        Uppercase letters     A-Z
        Lowercase letters     a-z

DIGITS          
   0, 1, 2, 3, 4, 5, 6, 7, 8, 9

13
DEC20012

Variables, Constants and Data Types


C Character Set
SPECIAL CHARACTERS
~  tilde % percent sign |   Vertical bar
@ at symbol + plus sign <  less than
_ underscore - minus sign >  greater than
^ caret #  number sign = equal to
& ampersand $ dollar sign /  slash
( left parenthesis * asterisk  \ back slash
) right parenthesis ′  apostrophe :  Colon
[  left bracket  " quotation mark ;  semicolon
] right bracket  !  exclamation mark , comma
{ left flower brace ? Question mark .  dot operator
} right flower brace        
14
DEC20012

Variables, Constants and Data Types


C Character Set
Escape Sequence
Escape Character ASCII Result
sequence value
\b blank space 008 Moves previous position
\r carriage return 013 Moves beginning of the line
\\ Back slash 092 Present back slash
\? Question mark 063 Present Question Mark
\t horizontal tab 009 Moves next horizontal tab
\f form feed 012 Moves initial position of next page
\’ Single quote 039 Present Apostrophe
\0 Null 000 Null
\v vertical tab 011 Moves next vertical tab
\n new line 010 Moves next Line
\" Double Quote       034 Present Double quotes
\a Alarm (bell) 007 Beep Sound
  Octal Number \000  
  Hexadecimal number \x  
15
DEC20012

Variables, Constants and Data Types


Data Types
• Data types specify:
 what type of data we enter AND
 how we enter data into our programs
• C language has some predefined set of data types to
handle various kinds of data that we can use in our
program.
• Different data types have different storage capacities.
• Data type determines the type of data a variable will
hold. If a variable x is declared as int, it means x can
hold only integer values.
• Every variable which is used in the program must be
declared as what data-type it is.

16
DEC20012

Variables, Constants and Data Types


Data Types
C language supports 2 different types of data:
1.Primary data types:
fundamental data types in C namely integer(int),
floating point(float), character(char) and void.

2.Derived data types:


 primary data types grouped together like array,
stucture, union and pointer.

17
DEC20012

Variables, Constants and Data Types


Data Types

18
DEC20012

Variables, Constants and Data Types


Data Types
Integer type - used to store whole numbers.

Size and range of Integer type on 16-bit machine:

Type Size(bytes) Range


int or signed int 2 -32,768 to 32767
unsigned int 2 0 to 65535
short int or signed short int 1 -128 to 127
unsigned short int 1 0 to 255
long int or signed long int 4 -2,147,483,648 to
2,147,483,647
unsigned long int 4 0 to 4,294,967,295
19
DEC20012

Variables, Constants and Data Types


Data Types

Floating point type - used to store real numbers.

Size and range of Integer type on 16-bit machine:

Type Size(bytes) Range


Float 4 3.4E-38 to 3.4E+38
double 8 1.7E-308 to 1.7E+308
long double 10 3.4E-4932 to 1.1E+4932

20
DEC20012

Variables, Constants and Data Types


Data Types
Character type - used to store characters value.
Size and range of Integer type on 16-bit machine:

Type Size(bytes) Range


char or signed char 1 -128 to 127
unsigned char 1 0 to 255

void type - means no value.


- usually used to specify the type of
functions which returns nothing.

21
DEC20012

Variables, Constants and Data Types


Data Types in C
Keywords in C Programming Language :
1. Keywords are those words whose meaning is already
defined by Compiler
2. Cannot be used as Variable Name
3. There are 32 Keywords in C
4. C Keywords are also called as Reserved words .
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        
22
DEC20012

Variables, Constants and Data Types


Identifiers
 are the names you can give to entities such as variables,
functions, structures etc.
 must be unique.
 They are created to give unique name to a C entity to
identify it during the execution of a program.
 example:
1. int money;
2. double accountBalance;
3. Current float;

23
DEC20012

Variables, Constants and Data Types


Rules for naming constants & variables
1. The first letter of an identifier should be either a letter or an
underscore.
 Valid Examples  Invalid Examples
 Stud_Name1  1Stud_Name

 Stud_Name2  1000_Discount
 Stud_mark1  0Temperature
 _Stud_mark  #stud_mark

 Discount_rate  %Discount_rate
 Temperature
 X

24
DEC20012

Variables, Constants and Data Types


Rules for naming constants & variables
2. A valid identifier can have letters (both uppercase and
lowercase letters), digits and underscore only - no special
symbols - no blank spaces

 Valid Examples  Invalid Examples


 Stud_Name1  Stud_Name#

 Stud_Name2  Stud mark1


 Stud_mark1  Discount-rate

 Discount_rate  Temperature@

 Temperature  Note*1
 Name1  Cost$
 Student2  Name&Age

25
DEC20012

Variables, Constants and Data Types


Rules for naming constants & variables
3. There is no rule on the length of an identifier.
Good Programming Practice:
choose meaningful name for an identifier, it will be easy to
understand and work on.
 Valid Examples  Invalid Examples
 Stud_Name1  Stud Name
 Discount_Rate  Discount Rate
 DiscountRate  Tempe rature
 Temperature  Student Mark
 Stud_Mark  Box Volume
 Box_Volume  Initial Value
 InitialValue

26
DEC20012

Variables, Constants and Data Types


Defining vs declaring variables
What it Means to Declare Something in C
When you declare a variable, a function, or even a class
all you are doing is saying: “there is something with this
name, and it has this type”.
The compiler can then handle most (but not all) uses of
that name without needing the full definition of that name.

int func();  function declaration


int x, y;  integer variable declaration
float price, discount;  float variable declaration
char name[20];  character variable declaration

27
DEC20012

Variables, Constants and Data Types


Defining vs declaring variables

A declaration provides basic attributes of a symbol:


its type and its name.
A definition provides all of the details of that symbol
if it's a function, what it does;
if it's a class, what fields and methods it has;
if it's a variable, where that variable is stored.

28
Thank You!!

29

You might also like