You are on page 1of 24

Chapter-Two

Outline:
C programming Constants and Variables
C Programming Data Types
 C programming Comments
Variables:

 In programming, a variable is a container


(storage area) to hold data.
 To indicate the storage area, each variable
should be given a unique name (identifier).
Variable names are just the symbolic
representation of a memory location.
 For example:
 int playerScore = 95
Variables(con…)
 Here, playerScore is a variable of integer type.
The variable is assigned value: 95.
 The value of a variable can be changed, hence
the name 'variable'.
 In C programming, you have to declare a
variable before you can use it
Variables(con…)
The general rules for naming variables are:
Names can contain letters, digits and underscores
Names must begin with a letter or an underscore (_)
Names are case sensitive (myVar and myvar are
different variables)
Names cannot contain whitespaces or special
characters like !, #, %, etc.
Reserved words (such as int) cannot be used as
names
Constants/Literals

 A constant is a value or an identifier whose


value cannot be altered in a program. For
example: 1

const double PI = 3.14


Here, PI is a constant. Basically what it means is
that, PI and 3.14 is same for this program.
Basic Data types in C-Data types in C

In this lesson, you will learn about basic data types such as int , float, char etc. in C
programming.
Data Type Size Description

int 2 or 4 bytes Stores whole numbers, without decimals


float 4 bytes Stores fractional numbers, containing one
or more decimals. Sufficient for storing 7
decimal digit
double 8 bytes Stores fractional numbers, containing one
or more decimals. Sufficient for storing 15
decimal digits
char 1 byte Stores a single character/letter/number, or
ASCII values
Basic Format Specifiers

There are different format specifiers for each


data type. Here are some of them
Format Specifier Data Type
%d or %i int
%f float
%lf double
%c cha
%s Used for strings, which you will
learn more about in a later
chapter
Integer

 Integers is the set of numbers that include all the whole numbers
and their negative values.
 The set of integers can be represented as -∞ .... -5, -4, -3, -2, -1,
0, 1, 2, 3, 4, 5.....∞
 In programming, integer is a data type used to represent integer
values. We can declare a variable as an integer type or define the
result of a function or expression as of integer type. 
 A variable declared as an integer type occupies a specified
amount of memory space in computer. The amount of space
occupied depends on the computer system you are using –
typicall ranging from 4 bytes to over 64 bytes. 
 A variable defined as an integer type cannot store any other
types of data such as character like a,b,c or floating-point
numbers like 5.43, -0.43.
Integer(con..)

 in C, an integer variable is declared as


 int a = 5;
Here, a is a variable of int (integer) type. The size
of int is 4 bytes.
 In JavaScript, the type is inferred by the
interpreter so we just write
 var a = 5;
Float and Double

• float and double are used to hold real


numbers.
• float salary; double price;
Floating-point

 Floating-point are numbers with floating decimal


points.
 Floating-point, as the name implies, are numbers that
contain decimal points that can float left or right. 
 In programming, floating-point is used to represent
fractions. Numbers such as 1/2, 5/7, -100/3 can be
represented in floating-point as 0.5, 0.71428, -33.33. 
 Often the keyword 'float' is used to declare a variable
that stores floating-point numbers. 
Floating-point(con..)
For example, in C
float pi = 3.14;
 However, in other programming languages
such as Javascript, Python we do need to
specify the datatype. Instead, we can just
write,
var pi = 3.14; (in Javascript)
pi = 3.14 (in Python)
Floating-point(con…)

Example:1
#include<stdio.h>
#include<math.h>
int main()
{
float x=2.13;
printf("The output of X=%f",x);

return 0;
}
Floating-point(con…)
Example:2

#include<stdio.h>
#include<math.h>
int main()
{
float x;
printf("Enter a Float Number:");
scanf("%f",&x);
printf("The output of X=%f",x);

getch();
return 0;
}
Character
 Character is a symbol in programming language that has meaning.
 A character can be any letter, number, punctuation marks, symbols or
whitespace. For example, the word "character" consists of eight
characters, and the phrase "Hello World!" consists of 12 characters
including the whitespace and exclamation mark.
 In programming character is a datatype. We can declare a variable as of a
character type and store characters in the variable. For example, in C , we
write,
 char first = 'a'; 
 Characters when storing in a variable must be inserted between single
quotes.
 String is another datatype in programming, which is a modified version of
character datatype. Strings are used to store more than one characters.
Character(con…)
Example:1
#include<stdio.h>
int main()
{
char b='A';
printf("The Output of B is Equal to:%c",b);

return 0;
}
Character(con…)
Example:2
#include<stdio.h>
int main()
{
char a;
printf("Enter a Character:");
scanf("%c",&a);
printf("The Output of Variable A is Equal to:%c",a);

return 0;
}
Character(con…)
Example:3
#include<stdio.h>
int main()
{
char str[]="farah";

printf("The Result of String:%s",str);


return 0;
}
Character(con…)
Example:4
#include<stdio.h>
int main()
{
char str[20];
printf("Enter a Name:");
scanf("%s",str);
printf("The Result of String:%s",str);
return 0;
}
C programming- Comments

Comments can be used to explain code, and


to make it more readable. It can also be used
to prevent execution when testing alternative
code.
Comments can be singled-lined or multi-
lined.
Single-line Comments

Single-line comments start with two forward slashes (//).


Any text between // and the end of the line is ignored by the
compiler (will not be executed).
This example uses a single-line comment before a line of code:
Example
// This is a comment
printf("Hello World!");
This example uses a single-line comment at the end of a line of
code:
Example
• printf("Hello World!"); // This is a comment
Multi-line Comments

Multi-line comments start with /* and ends


with */.
Any text between /* and */ will be ignored by
the compiler:
Example
/* The code below will print the words Hello
World!
to the screen, and it is amazing */
printf("Hello World!");
END

You might also like