CSE115 - Lecture02 - Introduction To C Programming
CSE115 - Lecture02 - Introduction To C Programming
C Character Set
A character denotes any alphabet, digit or special
symbol used to represent information.
Following figure shows valid alphabets, numbers and
special symbols allowed in C.
ASCII Characters
ASCII character set
ASCII stands for American Standard Code for Information Interchange.
It is a character-encoding scheme based on the ordering of the English
alphabet.
Constants and Variables
Constants and Variables
A constant is an entity that doesn’t change whereas a variable
is an entity that may change.
In any program, we typically do lots of calculations. The results
of these calculations are stored in computer’s memory. The
computer memory consists of millions of cells (called bytes).
The calculated values are stored in these bytes. To make the
retrieval and usage of these values easy these memory bytes
are given names. Since the value stored in each location may
change, the names given to these locations are called variable
names.
In the following figure, x is a variable, and 3 and 5 are
constants.
Constants
Constants
Types of constants
Integer
Real/Float
Character
String
Integer Constants
Integer values (i.e. whole numbers)
Rules for Constructing Integer Constants
1. An integer constant must have at least one digit.
2. It must not have a decimal point.
3. It can be either positive or negative.
4. If no sign precedes an integer constant it is assumed to be positive.
5. No commas or blanks are allowed within an integer constant.
6. The allowable range for integer constants is -32768 to 32767 (for 16-bit
compiler).
Example
426
+782
-8000
-7605
Constants
Real Constants
Real constants are often called Floating Point constants.
The real constants could be written in two forms—
Fractional form and Exponential form.
Rules for Constructing Real Constants in fractional
form
A real constant must have at least one digit.
It must have a decimal point.
It can be either positive or negative.
Default sign is positive.
No commas or blanks are allowed within an real constant.
Example
+325.34
426.0
-32.76
-48.5792
Constants
Real Constants
Real Constants in exponential form
The exponential form of real constants is usually used if the value of the
constant is either too small or too large.
In exponential form, the real constant is represented in two parts. The
part appearing before ‘e’ is called mantissa, whereas the part following
‘e’ is called exponent.
Rules for Constructing Real Constants in exponential form
The mantissa part and the exponential part should be separated by a letter e.
The mantissa part may have a positive or negative sign.
Default sign of mantissa part is positive.
The exponent must have at least one digit, which must be a positive or
negative integer. Default sign is positive.
Range of real constants expressed in exponential form is -3.4e38 to 3.4e38.
Example
+3.2e-5
4.1e8
-0.2e+3
-3.2e-5
Constants
Character Constants
A character constant is a single alphabet, a single digit or a single
special symbol enclosed within single inverted commas.
Example
'A'
'I'
'5'
'#'
The maximum length of a character constant can be 1 character.
String Constants
A string constant is one or more alphabets, digits or special symbols
enclosed within double quotes (“ “).
Example:
"Hello World"
"2+2=4"
Code Example
constants.c
Variables
Variables
An entity that may vary during program execution is called a variable.
Variable names are names given to locations in memory.
These locations can contain integer, real or character constants.
An integer variable can hold only an integer constant, a real variable
can hold only a real constant and a character variable can hold only a
character constant.
Rules for Constructing Variable Names
A variable name is any combination of 1 to 31 alphabets, digits or
underscores.
The first character in the variable name must be an alphabet or
underscore.
No commas or blanks are allowed within a variable name.
Example:
sum
in1, output2
pop_e_89
_val
Code Example
variables.c
Data Types
The Five Basic Data Types
There are five atomic data types in C:
Character (char)
Integer (int)
Floating-point (float)
Double floating-point (double)
Valueless (void)
The size and range of these data types may vary between
processor types and compilers.
In all cases a char is 1 byte. Values of type char are generally
used to hold values defined by the ASCII character set.
For most 16-bit environments (i.e., DOS or Windows 3.1), an int is
16 bits (2 bytes).
For most 32-bit environments (i.e., Win 2000, WinXP) and 64-bit
environments (Win 10, Win 11), an int is 32 bits (4 bytes).
The range of float and double will depend upon the method used
to represent the floating-point numbers.
Data Types
The Five Basic Data Types
The type void either explicitly declares a function as returning no
value or creates generic pointers. Both of these use are discussed
in subsequent lessons.
}
Basic Input and Output (I/O)
Receiving Input
Input from user using keyboard can be taken using scanf() function.
#include <stdio.h>
main( )
{
int a, b, c;
printf ( "Enter values of a and b: " ) ;
scanf ( "%d %d", &a, &b) ;
c = a + b;
printf ("Sum is %d", c) ;
}
The ampersand (&) operator before the variables in the scanf( )
function must be used. In C, & symbol is called Address of operator.
When input is given, a blank, a tab or a new line must separate the
values supplied to scanf( ):
1000 5 15.5
1000 5 15.5
1000
5
15.5
C Statements
C Statement
Every C statement must complete with a semicolon (;).
Mathematical expressions can be written in a C statement.
#include <stdio.h>
main( )
{
int p, n ;
float r, s ;
printf ( "Enter values of p, n, r: " ) ;
scanf ( "%d %d %f", &p, &n, &r ) ;
s = p * n * r / 100 ;
printf ( "%f" , s ) ;
}
Backslash Characters
Backslash Character Constants ( escape
sequences)
Enclosing character constants in single quotes works for
most printing characters ('A’, '1', '$', etc.).
A few characters, such as the new line character, are
impossible to enter into a string from the keyboard. For
this reason, C/C++ includes the special backslash character
constants.
The following program outputs a new line and a tab and
then prints the string This is a test.
#include <stdio.h> Output will be
void main ()
{ This is a test.
printf("\n\tThis is a test.");
}
Backslash Characters
Backslash Character Constants ( Escape
Sequences)
Code Meaning
\b Backspace
\n New line
\t Horizontal tab
\" Double quote
\' Single quote
\0 Null
\\ Backslash
\v Vertical tab
\? Question mark