0% found this document useful (0 votes)
19 views

CSE115 - Lecture02 - Introduction To C Programming

The document discusses various C programming concepts including character sets, constants, variables, data types and keywords. Constants can be integer, real, character or string values. Variables are names given to memory locations that can store constant values. Common data types include char, int, float and double. Keywords are reserved words in C with special meanings.

Uploaded by

sajjadtasin111
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
19 views

CSE115 - Lecture02 - Introduction To C Programming

The document discusses various C programming concepts including character sets, constants, variables, data types and keywords. Constants can be integer, real, character or string values. Variables are names given to memory locations that can store constant values. Common data types include char, int, float and double. Keywords are reserved words in C with special meanings.

Uploaded by

sajjadtasin111
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 24

Introduction

 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.

Data Size in bits Range of values


Type
char 8 0 to 255 (ASCII values)
int 16 or 32 −32,767 to 32,767 (for 16-bit)
-2,147,483,647 to 2,147,483,647
(for 32-bit/64-bit)
float 32 +/- 3.4e +/- 38

double 64 +/- 1.7e +/- 308


 Compiler (GCC in MinGW) within Code::Blocks uses 4 bytes for int data
type
Data Types
 The Five Basic Data Types
 Data type sizes within the Compiler (GCC in MinGW) in
Code::Blocks:

Data Size in void main()


type bytes {
char 1 printf("%d", sizeof(char));
printf("%d", sizeof(int));
int 4
printf("%d", sizeof(float));
float 4 printf("%d", sizeof(double));
double 8 }

 sizeof() is an operator that returns the size of the


argument (here data type) in number of bytes.
Keywords
 C Keywords
 Special reserved words that are used for special purposes.
 The keywords cannot be used as variable names.
 Following table shows the 32 keywords available in C.

 A detailed discussion of each of these keywords will be


provided when the use of that keyword will be shown.
Identifiers
 Identifier Names
 The names of variables, functions, labels, and various other
user-defined objects are called identifiers.
 The first character must be a letter or an underscore, and
subsequent characters must be either letters, digits, or
underscores.
Correct Incorrect
Count 1count
test23 hi!there
high_balance high...balance
 In an identifier, upper- and lowercase are treated as distinct.
Hence, count, Count, and COUNT are three separate
identifiers.
 An identifier cannot be the same as a C or C++ keyword,
and should not have the same name as functions that are in
the C or C++ library.
Variables
 Variable Declaration
 A variable is a named location in memory that is used to
hold a value that may be modified by the program.
 All variables must be declared before they can be used. The
general form of a declaration is:
type variable_list;
Here, type must be a valid data type (e.g., char) and
variable_list may consist of one or more identifier names
separated by commas.
 Example:
int i, j, l;
char ch1;
float bank_interest;
double balance_2010, profit, loss;
Variables
 Variable Initializations
 You can give variables a value as you declare them by

placing an equal sign and a value after the variable name.


 General form of initialization:
 type variable_name = value;
 Examples:
 char ch = 'a';
 int first = 0;
 float balance = 123.23;
 Local variables (variables declared inside any function) that
are not initialized have unknown values before the first
assignment is made to them.
First C Program
 The First C Program
 Execution of a program starts from main() function.
 Every statement in C ends with a semicolon (;)
main( )
{
int i, j, k ;
i = 100;
j = 200;
k = i + j;
printf ( "%d" , k) ;
}
 Body of the main() function should be written within curly
braces { and }.
 Assignment operator (=) is used to assign a value to a variable.
 Addition operator (+) is used to add two values (variable or
constant).
 Statements in C are executed one after another in sequence,
starting from the topmost.
First C Program
 The First C Program
 Any variable used in the program must be declared
before using it. For example,
 int i, j, k ;
 float val;
 Comments
main( )
{
/* This program adds
two integer values */
int i, j, k ;
i = 100;
j = 200;
k= i + j;
printf ("%d" , k) ; // This line shows the sum of i and j
}
 /* */ is called multi-line comment.
 // is called single line comment.
The main() function
 main() function
 Every C program must have a single main function. Execution starts
from the beginning of main function.
 Structure of main function
 main()
{
// C statements/commands
}
Basic Input and Output (I/O)

 Showing output on the screen (printf function)


 printf function is used to show values on the screen.
 The general form of printf function is-
 printf ("<format string>" , <list of variables>);
 Format string can contain normal characters.
 "Hello World", "Bangladesh", "100.5"
 Format string can also contain some format specifiers.
 %c for printing character values
 %d for printing integer values
 %f for printing floating point (i.e., real) values
 %lf for printing double values
Basic Input and Output (I/O)
 printf ( "%f", si ); // si is a float type variable
 printf ( "%d %f", p, r); // p is an integer and r is a float type variable

 printf ( "value of data is %lf", data); // data is a double type variable

 printf ( "value of ch is %c", ch); // ch is a character type variable

 printf function can also print result of an expression.

main(){ Output of the program


char c1;
int i1;
float f1; D 10 20.500000 12.340000
double d1;
20 25.500000 10.340000
c1 = 'D';
i1 = 10;
f1 = 20.5;
d1 = 12.34;
printf ("%c %d %f %lf \n", c1, i1, f1, d1);
printf ("%d %f %lf", i1+10, f1+5, d1-2);

}
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

You might also like