Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 4

EXPERIMENT NO.

02

DATE OF PERFORMANCE: GRADE:

DATE OF ASSESSMENT: SIGNATURE OF LECTURER/ TTA:

AIM: To Understand the Concept of Constant, Variables and Data Types


Using C Programming

THEORY:

SCANF():
scanf() is a predefined function in "stdio.h" header file. It can be used to read the input
value from the keyword.

SYNTAX:

scanf("format specifiers",&value1,&value2,.....);

EXAMPLE:

#include<stdio.h>
void main()
{
int a, b, sum;
printf("\nEnter two no: ");
scanf("%d %d", &a, &b);
sum = a + b;
printf("Sum : %d", sum);
getch();
}
CONSTANTS:
C Constants are also like normal variables. But, only difference is, their values can not be
modified by the program once they are defined.

Constants refer to fixed values. They are also called as literals


Page 1 of 4
Constants may be belonging to any of the data type.

There are two simple ways in C to define constants −

1. USING #DEFINE PREPROCESSOR.

#define LENGTH 10

#define WIDTH 5

#define NEWLINE '\n'

#include <stdio.h>

void main() {

int area;

area = LENGTH * WIDTH;

printf("value of area : %d", area);

printf("%c", NEWLINE);

getch();

2. USING CONST KEYWORD.


#include <stdio.h>
void main() {
const int LENGTH = 10;
const int WIDTH = 5;
const char NEWLINE = '\n';
int area;

area = LENGTH * WIDTH;


printf("value of area : %d", area);
printf("%c", NEWLINE);

getch();
}

VARIABLE:
A variable definition tells the compiler where and how much storage to create for the
variable. A variable definition specifies a data type and contains a list of one or more
variables of that type as follows,

Page 2 of 4
type variable_list;

Here, type must be a valid C data type including char, w_char, int, float, double, bool, or
any user-defined object; and variable_list may consist of one or more identifier names
separated by commas. Some valid declarations are shown here,

int i, j, k;
char c, ch;
float f, salary;
double d;

The line int i, j, k declares and defines the variables i, j, and k which instruct the compiler
to create variables named i, j and k of type int.
Variables can be initialized (assigned an initial value) in their declaration. The initializer
consists of an equal sign followed by a constant expression as follows,
type variable_name = value;

DATA TYPES:
A programming language is proposed to help programmer to process certain kinds of data
and to provide useful output. The task of data processing is accomplished by executing
series of commands called program. A program usually contains different types of data
types (integer, float, character etc.) and need to store the values being used in the program.
C language is rich of data types. A C programmer has to employ proper data type as per
his requirements.

Page 3 of 4
EXERCISE:
Write a C program
1. To find an area of Circle.
2. To find area of Triangle and Rectangle.
3. To convert the temperature entered by user in Centigrade to Ferenhit.
4. To convert the entered days by user in months.
5. For swapping of two numbers using third variable method and without using third
variable method.

Page 4 of 4

You might also like