0% found this document useful (0 votes)
45 views21 pages

BCA 103 Unit2 PPT

This document provides an introduction to constants, data types, and variables in the C programming language. It discusses the different types of constants in C including integer, floating-point, character, and string constants. It then describes the main data types - character, integer, float, long. It explains that variables must be declared before use and variables follow naming conventions. It also discusses symbolic constants and the C character set which includes letters, numbers, and special symbols.

Uploaded by

Kinjal Bhatt
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)
45 views21 pages

BCA 103 Unit2 PPT

This document provides an introduction to constants, data types, and variables in the C programming language. It discusses the different types of constants in C including integer, floating-point, character, and string constants. It then describes the main data types - character, integer, float, long. It explains that variables must be declared before use and variables follow naming conventions. It also discusses symbolic constants and the C character set which includes letters, numbers, and special symbols.

Uploaded by

Kinjal Bhatt
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/ 21

FUNDAMENTALS OF PROGRAMMING

USING 'C' LANGUAGE

Session 2 [Block 1 - Unit 2]


Block 1: Basics of C
Unit 2 : Understanding Constants, Data types and
Variables
Index
Introduction

Types of Programming Languages

Introduction to C-Programming
Constants
• Constants are those quantities whose value may not change during the
execution of a C program.
• C Supports Integer, Character, String and Floating–point constants.

• Integer Constants : An integer constant contains digits. There are three


types of integers : decimal, octal and hexadecimal. An integer constant can
consist of any combination of digits from 0 to 9, they can be positive or
negative with + or – For example, 156, –562, 3584, +72, 0.

• Floating-point Constants : These constants are base 10 numbers that


contain either a decimal point or an exponent (or both). Some of the valid
floating–point constants are : 8.5, 0.6, 1.4526E + 5, 425147e15
cont.

• Character Constants : They are single characters, enclosed in single


quotation marks. For example, 'B', 'a', 'O', 'u' etc.

• String Constants : They consist of consecutive characters enclosed in


double quotation marks. For example, "Hello", "Welcome", "Computers",
"Object Oriented". The compiler automatically places a null character ('\0') at
the end of every string constant, as the last character within the string.
Data Types
Character
• Character data type is used in the C–Language to store or to access a
character.
• To declare the character type of variable we will use keyword "char".
• A variable declared with "char" datatype can store one character, and
variable will occupy 1Byte of storage memory space.

char ch;
ch= 'A';
printf ("%c", ch);
Integer
• Integer variables are used in the C–Language to store numbers without
decimal points.
• To declare a variable of type integer, we will use keyword "int".
• Any variable declared with type "int" will occupy 2 Bytes (=16 Bits) of space
in the memory.

int number;
number=45;
printf ("%d", number);
Float
• To access and to store any real value, float data type is used. C's float data
type occupies 4 bytes in the memory.

To declare any float variable :


float per;
float total;

To assign or to store some real value :


per=78.20;
total=123.45
Long
• To access and to store any integer value long data type is used. C's long data
type occupies 4 bytes in the memory.

To declare any long variable :


long account1;
longmobileno;

To assign or to store some long value :


account1=123456;
mobileno=9898998765;
Exercise 1

1. Variable declared with float data type will occupy ________space in memory.
[A] 1 Bytes [B] 2 Bytes [C] 4 Bits [D] 4 Bytes

2. From the following ______ is not a type of constant.


[A] Integer [B] String [C] Image [D] Floating–point

3. %c is used to print _______ value.


[A] character [B] Integer [C] Float [D] Double
Answers

1. Variable declared with float data type will occupy ________space in memory.
[A] 1 Bytes [B] 2 Bytes [C] 4 Bits [D] 4 Bytes

2. From the following ______ is not a type of constant.


[A] Integer [B] String [C] Image [D] Floating–point

3. %c is used to print _______ value.


[A] character [B] Integer [C] Float [D] Double
Character Set
• All those set of characters which are used to write a C program are called the
C character set.
• C uses the uppercase letters A to Z, the lowercase letters a to z, digits 0 to 9
and some special characters to form basic program elements.

• The C Character set is given below


• Uppercase Letters (A–Z)
• Lowercase Letters (a–z)
• Digits (0–9) and Special Characters ( #, }, < , / etc.)

• C also uses some combinations of these characters such as \t, \n, \b to


represent special conditions such as horizontal tab, newline and back space
characters respectively
C Tokens
• Keywords : Words with a specific meaning are known as keywords. Keyword
must not be used as variable names. There are 32 keywords in the
programming language
Cont.

• Identifiers : Identifiers are names given to various elements of a program


such as variables, functions and arrays. They consist of digits and letters
which can be in order but the first character should be a letter, using both
uppercase and lowercase letters, but should have different meanings.

• For example, Mam and mam are not same. The underscore character can
also be included and is treated as a letter.

• Given below are some valid identifiers :


• A05, Computer_05, interest_rate, WELCOME
Variables
• A variable is a storage location in your computer's memory which stores data.

• Variable Naming Convention :


To use variables in your C programs it must follow rules :
• The variable name can be of letters, or combination of letters and digits
or special character underscore character '_'.
• The first letter of variable must be a character or underscore and rest can
be digits.
• Variable names are case sensitive e.g. count and Count refer to two
different variables.
• Reserved words cannot be used as variable names. e.g. int, float, char, if,
else, include, for, while, switch cannot be name of variables.
Declaration of Variables
• Declaration of a variable involves specification of data type with it. In the
• C language all variables must be declared before they appear in executable
statements.
• The syntax for the same can be written as
• <data type> <variable name>;
int x, y;
float w, l;
int a[10];
char k[5];

• Thus, x and y are integer variables, w and l are float variables, a is an integer
array whose size is 10 and k is a character array with a size of 5
Assigning Values to Variables
• If the values of the variables are known, then you can present the
declarations as given below :
int x=5;
char name= 'A';
float z= 5.36;

• A character array also known as string, may also be initialized within a


declaration as shown below :
char name[]= "Computer";
Symbolic Constant
• Symbolic constants are also termed as sequence of characters. The
characters may represent a numeric constant, a character constant or a
string constant.
• Symbolic constant allows a name to appear in place of a numeric or
character or string constant.

• They are defined in the beginning of a program. For example :


# define MAX 10
Exercise 2
1. From the given below _____ is not a valid variable name.
[A] abc123 [B] abc+123
[C] abc_123 [D] All are valid variable names

2. In the declaration, variable names are followed by _______ .


[A] switch [B] continue [C] datatype [D] break

3. From the given below ______ is not a keyword.


[A] int [B] string [C] float [D] break

4. C–language character set includes ________.


[A] Alphabets [B] Numbers
[C] Special symbols [D] All of the above
Answers
1. From the given below _____ is not a valid variable name.
[A] abc123 [B] abc+123
[C] abc_123 [D] All are valid variable names

2. In the declaration, variable names are followed by _______ .


[A] switch [B] continue [C] datatype [D] break

3. From the given below ______ is not a keyword.


[A] int [B] string [C] float [D] break

4. C–language character set includes ________.


[A] Alphabets [B] Numbers
[C] Special symbols [D] All of the above
Thank You

You might also like