Download as pdf or txt
Download as pdf or txt
You are on page 1of 16

Programming for Problem Solving

BCAC101

Mr. Subhas Chandra Nath


Assistant Professor
Department of Computer
Application

1 Lecture :: 4
Contents

 Variables in C
 Naming Variable
 Data Types
 Size & Range of Data Types
 Declaration of Variable
 Assigning Value To Variable
 Input/Output Function
 Example Of C Program
 Hands on Example
2
Variables in C

 A Variable is an identifier or data name that is used to store


any data value.
 Variables are used to store values that can be changed during
the program execution.
 Variables in C have the same meaning as variables in algebra.
That is, they represent some unknown, or variable, value.
x=a+b
p = 3*(q - 5)
 C is a case sensitive language. It matters whether an
identifier, such as a variable name, is uppercase or lowercase.
3
Continue…..

Rules of variable naming:


 A variable name is any combination of alphabets, digits or
underscores.
 The 1st character in the variable name must be an alphabet.
 No commas or blank spaces are allowed within a variable name.
 No special symbol other than an underscore can be used in a
variable name.
 May not be a C reserved word.
 May be as long as you like, but only the first 31 characters are
significant.
4
Example: tot_val, x, a, s1 etc.
Data Types

The kind/type of entities that a variable can store is known as data


type. There are three classes of data types here.
 Basic or Primitive data types.
int, float, double, char.

 Aggregate OR derived data types


Arrays, Function, Pointer.

 User defined data types


Structures, Union, Enum.
5
Basic Data Types

Integer Integer Character Floating


(Signed) (Unsigned) Point
int unsigned int char float

short int unsigned signed char double


short int
long int unsigned unsigned long double
long int char

6
Size & Range of Data Types

Data Type Size (Byte) Range


int or signed int 2 -32768 to 32767
unsigned int 2 0 to 65535
short int/ signed short int 1 -128 to127
unsigned short int 1 0 to 255
long int/ signed long int 4 -2,147,483,648 to 2,147,483,647
unsigned long int 4 0 to 4,294,967,295
char or signed char 1 -128 to 127
unsigned char 1 0 to 255
float 4 3.4E-38 to 3.4E+38
double 8 1.7E-308 to 1.7E+308

7 long double 10 3.4E-4932 to 1.1E+4932


Declaration of Variables

After designing suitable variable names, we must declare


them to the compiler. Declaration does 2 things:
 It tells the compiler what the variable name is.
 It specifies what type of data the variable will hold.
Declaration of variables must be done before they are
used in the program.
Syntax for declaring a variable is as follows:
data-type v1,v2,v3,………..vn;
where v1,v2,…vn are the names of variables. Variables
8
are separated by commas.
Declaration of Variables

Example: int num,sum;


float sal,height;
char ch;
Thus, it is good practice to initialize variables when
they are declared.
 Once a value has been placed in a variable it stays
there until the program alters it.
 Variable declaration must end with semicolon.

9
Assigning Values To Variables

Values can be assigned to variables using the assignment operator =


as follows:
variable-name=constant;
Example: sum=50;
sal=25000.00;
ch=‘x’;
It is also possible to assign a value to a variable at the time of
variable declaration. This takes the following form:
data-type variable-name=constant;
Example: int a=3;
10 float pi=3.14;
Input/Output Function

printf() Function: printf() is a function which is used to print on


the output screen the value contained in a variable.
Syntax: printf(“<format string>”,<list of variables>);
<format string> could be,
%d -> for integer values.
%f -> for real values.
%c -> for character values.
%ld -> for long int values.
%s -> for character string values.
Example: printf(“%d”,roll);
11 printf(“%f”,height);
Continue…..

scanf() Function: scanf() allows us to enter data from keyboard


that will be formatted in a certain way.
Syntax: scanf(“<format string>”, <list of addresses of variables>);

Example: int a;
float p;
char ch;
scanf(“%d”,&a);
scanf(“%f”,&p);
scanf(“%c”,&ch);

12
Or scanf(“%d%f%c”,&a,&p,&ch);
Example Of C Program

13
Output

14
Example

Hands on Example through Code


Blocks

15
Thank You.

Any clarification contact me through e-mail


scn.assignment@gmail.com

16

You might also like