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

3.constants, Variables and Data Types

The document discusses various C programming concepts like constants, variables, data types and their usage. It explains the different data types in C like integer, character, floating point and their value ranges. It also talks about declaring variables as constants and defining variables in memory.

Uploaded by

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

3.constants, Variables and Data Types

The document discusses various C programming concepts like constants, variables, data types and their usage. It explains the different data types in C like integer, character, floating point and their value ranges. It also talks about declaring variables as constants and defining variables in memory.

Uploaded by

Ramastar Mastar
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 37

Constants, Variables &

Data Types
Introduction

► A program processes data consisting of numbers, characters, strings

► It produces output known as information

► The set of instructions of the program are formed using certain symbols and words
as per the grammar of the language

► Like any other language, C has its own vocabulary and grammar
The C Character Set

❑ White Spaces
❑ Blank
❑ Horizontal Tab
❑ Carriage Return
❑ New line
❑ Form feed
C Keywords

Keywords in C Programming
auto break case char
const continue default do
double else enum extern
float for goto if
int long register return
short signed sizeof static
struct switch typedef union
unsigned void volatile while
Sample Programs
//Program showing the use of keywords
#include<stdio.h>

int main()
{
int main = 45;
printf("\n main=%d",main);
return 0;
}
Sample Programs
//Program showing the use of keywords
#include<stdio.h>

int main()
{
int main = 45;
int enum = 67;
printf("\n main=%d",main);
printf("\n enum=%d",enum);
return 0;
}
Constants

❑ The largest integer value that can be stored is machine dependent, like 32767 on
16-bit machines, 2,147,483,647 on 32-bit machines

❑ It is also possible to store larger integer constants by appending qualifiers, like U,


L and UL to the constants, like 56789U/ 56789u (unsigned integer), 98761234UL
(unsigned long integer), 9876543L (long integer)
Constants contd..

❑ General form is: mantissa e exponent


❑ The mantissa is either a real number expressed in decimal notation or an
integer
❑ The exponent is an integer with an optional +/- sign
❑ Exponent, e, can be written in either uppercase or lowercase
Constants contd..

❑ The character constant ‘2’ is not the same as number 2


❑ Character constants have integer values known as ASCII values
Sample Program
#include<stdio.h>
int main()
{
printf("My mobile number is 7873923408\a");
printf("\nHello Friends\b\b\b\bE");
printf("\nHello fri\rends");
printf("\nHello\\GFG");
printf("\n\' Hello Friends\n");
printf("\n\" Hello Friends");
return (0);
}
Sample Programs

//program showing character constant

#include<stdio.h>

int main()
{
printf("\n%d",'a');
printf("\n%d",'5');
printf("\n%d",5);
printf("\n%c",'a');
return 0;
}
Constants contd..

❑ String Constants
❑ It is a sequence of characters enclosed in double quotes
❑ The characters may be letters, numbers, special characters
and blank spaces
❑ Examples: “Hello”, “1987”, “X”, “5+3”, “Hello World!”, …
Sample Program
//program showing string constant

#include<stdio.h>

int main()
{
/*printf("\n%d",'a');
printf("\n%d",'5');
printf("\n%d",5);
printf("\n%c",'a');*/
printf("\n%s","X");
printf("\n%s","Hello World!!");
printf("\n%c","A");
return 0;
}
Variables
► A variable is a data name that may ne used to store a data value

► Can have only one value assigned to it at any given time during the execution of
the program

► The value of a variable can be changed during the execution of the program

► Variables are stored in the memory

► Memory is a list of storage locations, each having a unique address

► A variable is mapped to a location of the memory, called its address

► The variable name is used to refer to the value of the variable


Variable Names
► Variable names may consist of letters, digits and the underscore (_)
character, subject to the following constraints:

► First character must be a letter or ‘_’


► No special characters other than ‘_’
► No white space in between
► Names are case-sensitive (max and Max are two different names)
► Variable name should not be a keyword
► Length of the variable name should not be normally more than 8
characters
Some Valid and Invalid identifiers
Exercise: Valid/ Invalid variable names

Variable Name Valid/ Invalid? Remarks


First_tag
char
Price$
group one
average_number
int_type
Exercise: Valid/ Invalid variable names

Variable Name Valid/ Invalid? Remarks


First_tag Valid
char Invalid Keyword
Price$ Invalid Dollar sign is illegal
group one Invalid Blank space is not
allowed
average_number valid
int_type valid
Variables in Memory
Variables in Memory contd..
Variables in Memory contd..
Variables in Memory contd..
Sample Program
//program showing use of variables
#include<stdio.h>

int main()
{
int x = 20, y=15;
printf("\n x=%d,\t y=%d",x,y);
x=y+3;
printf("\n x=%d,\t y=%d",x,y);
y=x/6;
printf("\n x=%d,\t y=%d",x,y);
return 0;
}
Data Types
► Each variable has a type, indicates what type of values the variable can hold

► Data types in C are broadly classified into 4 categories:


❑ Primary data types
Integer (int)
Character (char)
Floating point (float)
Double-precision floating point (double)

❑ User-defined data types


❑ Derived data types
❑ Empty data set
Data Types
Integer and Character data types with
their storage sizes and value range
Type Storage size Value range

char 1 byte -128 to 127 or 0 to 255


unsigned char 1 byte 0 to 255

signed char 1 byte -128 to 127

-32,768 to 32,767 or -2,147,483,648 to 2,147,483,647


int 2 or 4 bytes

unsigned int 2 or 4 bytes 0 to 65,535 or 0 to 4,294,967,295

short 2 bytes -32,768 to 32,767


unsigned short 2 bytes 0 to 65,535

long 8 bytes or (4bytes for 32 bit OS) -9223372036854775808 to 9223372036854775807

unsigned long 8 bytes 0 to 18446744073709551615

❑ To get the exact size of a type or a variable on a particular platform,


the expression sizeof(type) can be used
More on char Data Types
Example on char Data Types
ASCII codes
ASCII codes contd..
Sample Program
//Program showing storage size and value range of integer and char data types
#include <stdio.h>
#include <stdlib.h>
#include <limits.h>
#include <float.h>
int main() {
printf("CHAR_BIT : %d\n", CHAR_BIT);
printf("CHAR_MAX : %d\n", CHAR_MAX);
printf("CHAR_MIN : %d\n", CHAR_MIN);
printf("INT_MAX : %d\n", INT_MAX);
printf("INT_MIN : %d\n", INT_MIN);
printf("LONG_MAX : %ld\n", (long) LONG_MAX);
printf("LONG_MIN : %ld\n", (long) LONG_MIN);
printf("SCHAR_MAX : %d\n", SCHAR_MAX);
printf("SCHAR_MIN : %d\n", SCHAR_MIN);
printf("SHRT_MAX : %d\n", SHRT_MAX);
printf("SHRT_MIN : %d\n", SHRT_MIN);
printf("UCHAR_MAX : %d\n", UCHAR_MAX);
printf("UINT_MAX : %u\n", (unsigned int) UINT_MAX);
printf("ULONG_MAX : %lu\n", (unsigned long) ULONG_MAX);
printf("USHRT_MAX : %d\n", (unsigned short) USHRT_MAX);
return 0;
}
Floating point data types with their
storage sizes and value range

Type Storage size Value range Precision


float 4 byte 1.2E-38 to 3.4E+38 6 decimal places
double 8 byte 2.3E-308 to 1.7E+308 15 decimal places
long double 10 byte 3.4E-4932 to 1.1E+4932 19 decimal places
Sample Program
//Program showing storage size and value range of floating
point data types
#include <stdio.h>
#include <stdlib.h>
#include <limits.h>
#include <float.h>
int main()
{
printf("Storage size for float : %d \n", sizeof(float));
printf("FLT_MAX : %g\n", (float) FLT_MAX);
printf("FLT_MIN : %g\n", (float) FLT_MIN);
printf("-FLT_MAX : %g\n", (float) -FLT_MAX);
printf("-FLT_MIN : %g\n", (float) -FLT_MIN);
printf("DBL_MAX : %g\n", (double) DBL_MAX);
printf("DBL_MIN : %g\n", (double) DBL_MIN);
printf("-DBL_MAX : %g\n", (double) -DBL_MAX);
printf("Precision value: %d\n", FLT_DIG );
return 0;
}
The bool data type
Declaring a Variable as Constant
► Variables whose values can be initialized during declaration, but cannot be
changed after that

► Declared by putting the const keyword in front of the declaration

► Storage allocated just like any variable

► Used for variables whose values need not be changed


► Prevents accidental change of the value
Sample Program
//Program showing the use of constants

#include<stdio.h>

int main()
{
const int m = 10;
int p = m+20;
printf("\n m=%d",m);
printf("\n p=%d",p);
return 0;
}
Sample Program

You might also like