0% found this document useful (0 votes)
11 views27 pages

Session 3

The document provides an introduction to C programming, focusing on escape codes, data types, variables, and constants. It explains the different classes of data types, rules for naming variables, and how to declare and assign values to them. Additionally, it covers formatted input and output using functions like printf and scanf, along with examples and tasks for practice.

Uploaded by

samwel.ochieng21
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views27 pages

Session 3

The document provides an introduction to C programming, focusing on escape codes, data types, variables, and constants. It explains the different classes of data types, rules for naming variables, and how to declare and assign values to them. Additionally, it covers formatted input and output using functions like printf and scanf, along with examples and tasks for practice.

Uploaded by

samwel.ochieng21
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 27

Introduction

to C Session
3
Example 1
Escape Codes

 Escape codes are special characters that


cannot be expressed otherwise in the
source code such as new line, tab and
single quotes.
 All of these characters or symbols are
preceded by an
inverted (back) slash (\ ).
Table 2.2 – Escape
codes
Data Types
Data Types

 a set of data with values having


predefined characteristics such as
integers and characters.
 C specifies the range of values for a given
data type, how the values are processed
by the computer and how they are stored.
classes of data
types
 Primitive (or basic) data types –
 these are the fundamental data types supported
by the language. These can be classified as
integer types, floating point types and
character types.
 User defined data types –
 based on the fundamental data types users can
define their own data types. These include type
defined data types (using t y pe d e f keyword) and
enumerated types (using e n u m keyword).
 Derived data types –
 programmers can derive data types such as arrays,
structures, unions and pointers by combining
several data types together
Primitive Data
Types
Variables
Variabl
es

 It is a memory location that can hold a


value of a certain data type.
 Programmers refer to a variable by its
name (identifier) so that it can be
accessed during the course of the
program.
Rules

 The name can contain letters, digits, and the


underscore character (_).
 The first character of the name must be a
letter. The underscore is also a legal first
character, but its use is not recommended.
 Case matters (that is, upper- and lowercase
letters). Thus, the names count and Count refer
to two different variables.
 C keywords can't be used as variable names. A
keyword is
a word that is part of the C language.
Declaring
Variables

 You must first declare it by specifying the


data type.
 Examples
 int a;
 float total;
 variables “a ” as an integer, “t
ot a l” as a floating point
number
Continuation

 Multiple variables belonging to the same data


type can be defined as separate set of
expressions or by listing variable names one
after the other (should be separated by a coma
sign (,))
 int a; int b;
 float total;
 Float sub_total

 int a,b;
 float total, sub_total;
Variable
Assignment

 After declaring a variable it should be


initialized with a suitable value.
 int a;
 a = 10;
or
 int a=10;
Displaying
Numbers

 When displaying numbers special care must


be given to the data type.
 Each data type has to be used with p r in t
f function
in a specific format
 In order to display the correct values using
the p r in t f function conversion specifiers
should be used.
 They are used to instruct the compiler about
the type of numbers appearing in the
program, which in turn determines the
suitable memory storage locations
Displaying
Numbers
Example
Results

 The first number is of the type integer


while the second number is of the type
float.
 Conversion specifier % d stands for
decimal while % f
stands for float.
 Incorrect use of format characters would
result in wrong outputs.
Attempt
Constan
ts

 The value of a constant cannot be changed


after an initial value is assigned to it.
 The C language supports two types of
constants; namely
declared constants and defined constants
 They are defined using the keyword const
i.e const float pi = 3.141; - Declared Constants
 a constant can be defined simply by using the
#define pre-processor directive.
 These are called defined constants
i.e #define pi 3.141
Formatted
Input/Output
Formatted
Input

 We have already used p rintf function


to display formatted output.
 The scanf is a similar function that is used
to read data into a program. The sca n f
function accepts formatted input from the
keyboard.
Example

Int main()
{
Int x;
Printf(“enter a
number\n”)
scanf("%d", &x);
Printf(“%d”,x);
}
 reads a decimal integer from the
keyboard and assigns it to the integer
variable x
Example 1
Explanation

 Note that the sca n f function uses the


variables “a ” and “b”as “& a ” and “& b”.
 The symbol “& ” is called the address of
operator. The string “& a ” represents the
memory address containing variable “a”
Group Task

 Write a program that inputs two floating-


point values from the keyboard and then
displays their product.
 Write a program that inputs 3 integer values
from the
keyboard and then displays their sum.
End of
Lecture

You might also like