0% found this document useful (0 votes)
19 views54 pages

C Programming: Data Types & Identifiers

The document provides an overview of constants, variables, and data types in the C programming language, detailing the rules for identifiers, types of data, and their respective sizes. It explains primary, derived, and user-defined data types, including integral, floating-point, and void types, as well as constants and variables. Additionally, it covers the significance of keywords, character sets, and the conditions for specifying variables.

Uploaded by

samalasaharsha23
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)
19 views54 pages

C Programming: Data Types & Identifiers

The document provides an overview of constants, variables, and data types in the C programming language, detailing the rules for identifiers, types of data, and their respective sizes. It explains primary, derived, and user-defined data types, including integral, floating-point, and void types, as well as constants and variables. Additionally, it covers the significance of keywords, character sets, and the conditions for specifying variables.

Uploaded by

samalasaharsha23
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

Constants, Variables

and Data types


Identifiers
•Identifiers allow us to name data and other objects in the program
•Each identified object in the computer is stored at a unique address
•We simple give data identifiers and let the compiler keep track of
using the address
•The only valid name symbols are the upper case letters A-Z, the
lower case letters a-z, the digits 0-9 and the underscore.
•The first character of the identifier cannot be a digit.
•The identifier cannot be a keyword or reserved word
•Identifiers can be 63 characters long.
Rules of identifiers

First character must be alphabetic character or underscore


Must consist only of alphabetic character, digits or underscore
First 63 characters of an identifiers are significant
Cannot duplicate a keyword
It may not have a space or hyphen
C is a case sensitive language
Valid names: a, student_name, _aSystemname,_Bool
Invalid names: $num, 2names, sum-salary, stdnt number, int
Types

A type defines a set of values and a set of operations that can


be applied on those values.

The c language has defined a set of types that can be divided


into four general categories.
Data Types
Void type

It is designated by the keyword void


It has no values and operations
It is a very useful data type.

Integral type

C language has 3 integral types: Boolean, Character and Integer


Integral numbers cannot contain a fraction part: they are whole
numbers

Boolean:
C99 incorporated a Boolean type
It can be represented with only two values true and false.
It is represented by the keyword bool, is stored in memory as
0(false) or 1(true)
Character
To a computer the character is any value that can be represented in
the computer’s alphabet, or as it is better known, its character set.

C standard provides two character types: char and wchar_t.

Normally computers use 1 byte to store the char data types.

There are 256 different values in the char set.


Ex: letter a is represented in memory as : 0110 0001 => 97

A single character can be defined as a character (char) type data.

Characters are usually store in one byte of internal storage.

Qualifier signed or unsigned may be used in char explicitly.


Unsigned characters have values between 0 and 255, signed
characters have values from -128 to 127
Integer

It is a number without a fraction part

It supports 4 different sizes of the integer data type: short int
(short), int, long int (long) and long long int (long long)

The type defines the size of the field in which data can be stored.

C provides an operator sizeof that will tell us the exact size in bytes

C requires the following relationship always be true:

sizeof(short) ≤ sizeof(int) ≤ sizeof(long) ≤ sizeof(long long)


Floating point types

•C standard recognizes three floating-point types: Real, imaginary,


and complex

•The real type holds values that consist of an integer and a fractional
part.

•C language supports three different sizes of real types: float,


double, and long double

•sizeof(float) ≤sizeof(double) ≤sizeof(long double)

•Floating point numbers are stored in 32bits, with 6 digit precision.

•Floating point numbers are defined by keyword “float”.


•When accuracy is provided by a float number is not sufficient,
double can be used to define number.

•A double data type number uses 64 bits giving a precision of 14


digits.

•These are known as double precision number.

•Double data type represent the same data type that float
represents but with greater precision.

•To extend the precision we may use long double which uses 80
bits.
DATATYPES
• Data types simply refers to the type and size of data associated
with variables and functions.

• C supports 3 classes of data types:


1)Primary data types
2) Derived data types
3)User-defined data types.
Primary data types
int Used to denote an integer type.

char Used to denote a character type.

float, double Used to denote a floating point type.

int *, float *, Used to denote a pointer type.


char *

void As the name suggests it holds no value and is generally


used for specifying the type of function or what it
returns. If the function has a void type, it means that the
function will not return any value.
Example
#include <stdio.h>
int main()
{
int a = 4000; // positive integer data type
float b = 5.2324; // float data type
char c = 'Z'; // char data type
long d = 41657; // long positive integer data type
long e = -21556; // long -ve integer data type
int f = -185; // -ve integer data type
short g = 130; // short +ve integer data type
short h = -130; // short -ve integer data type
double i = 4.1234567890; // double float data type
float j = -3.55; // float data type
}
#include<stdio.h>
int main()
{
printf("\nType\t Size(bytes)");
printf("\nCharacter--->%d",sizeof(char));
printf("\nInteger --->%d",sizeof(int));
printf("\nLong int--->%d",sizeof(long int));
printf("\nFloat --->%d",sizeof(float));
printf("\nDouble --->%d",sizeof(double));
printf("\nLong double--->%d\n",sizeof(long double));

}
MODIFIERS
• The amount of memory space to be allocated
for a variable is derived by modifiers.
• Modifiers are prefixed with basic data types to
modify (either increase or decrease) the
amount of storage space allocated to a variable.
• For example, storage space for int data type is 4
byte for 32 bit processor. We can increase the
range by using long int which is 8 byte. We can
decrease the range by using short int which is 2
byte.
• short
• long
• signed
• Unsigned
Derived Datatypes
Data Types Description

Arrays Arrays are sequences of data items having


homogeneous values. They have adjacent
memory locations to store values.
References Function pointers allow referencing functions
with a particular signature.

Pointers These are powerful C features which are used


to access the memory and deal with their
addresses.
User Defined Data types
Data Types Description
Structure It is a package of variables of different types under
a single name. This is done to handle data
efficiently. "struct" keyword is used to define a
structure.
Union These allow storing various data types in the same
memory location. Programmers can define a union
with different members, but only a single member
can contain a value at given time. It is used for

Enum Enumeration is a special data type that consists of


integral constants, and each of them is assigned
with a specific name. "enum" keyword is used to
define the enumerated data type.
INTEGER TYPES:

• Integers are whole numbers with a range of values supported


by particular machine.

• Integers occupy one word storage generally and since the


word sizes of machine vary the size of integer that can be
stored depends on computer.

• If we use 16-bit word length, the size of integer value is


limited to range -32768 to 32767.

• If we use 32-bit word length can store an integer ranging from


-2147483648 to 2147483647.
• In order to provide control over range of numbers and storage
space C has 3 classes of integer storage namely: short int, int,
long int in both signed and unsigned.

• Short int represents fairly small integer values and requires


half amount as regular int number uses.

FLOATING POINT TYPE:


• Floating point numbers are stored in 32bits, with 6 digit
precision.

• Floating point numbers are defined by keyword “float”.


• When accuracy is provided by a float number is not sufficient,
double can be used to define number.

• A double datatype number uses 64 bits giving a precision of


14 digits.

• These are known as double precision number.

• Double datatype represent the same datatype that float


represents but with greater precision.

• To extend the precision we may use long double which uses 80


bits.
VOID TYPES:
• Void type has no values. This is used to specify the type of
functions.

• The type of function is said to be void when it doesn't return


any value to the calling function.
CHARACTER TYPES:
• A single character can be defined as a character (char) type
data.

• Characters are usually store in one byte of internal storage.

• Qualifier signed or unsigned may be used in char explicitly.


Unsigned characters have values between 0 and 255, signed
characters have values from -128 to 127
Constants and Variables
• A programming language is designed to help process certain
kinds of data consisting of numbers, characters and strings to
provide useful output known as information.

• The task of processing of data is accomplished by executing a


sequence of precise instructions called program.

• These instructions are formed using certain symbols and


words according to some rigid rules known as syntax rules.
CHARACTER SET:

• The characters in C are grouped into following categories:


1) Letters
2) Digits
3) Special characters
4) White spaces.

• Compiler ignores white spaces unless they are part of a string


constant.

• White spaces may be used to separate words but prohibited


between characters of keywords and identifiers.
• LETTERS: Uppercase A….Z, lower case a..z.

• DIGITS: All decimal digits 0..9

• SPECIAL CHARACTERS: comma(,), period(.),


semicolon(;) , colon(:), question mark(?), quotation(“), dollar
sign($), slash(/),back slash(\), percent sign(%), underscore(_),
ampersand(&), asterisk(*), number sign(#).

• WHITE SPACES: Blank space, Horizontal tab, Carriage


return, Newline, Form feed.
Trigraph characters:

• C introduces the concept of trigraph sequences to provide a


way to enter certain characters that are not available on some
keywords.

• Each trigraph sequence consists of three characters, 2 question


marks followed by another character.
C tokens:
• In a passage of text individual words and punctuation marks
are called tokens.

• In a C program the smallest individual units known as C


tokens.

• C has 6 types of tokens namely:


1)Keywords
2) identifiers
3)constants
4)Strings
5)special symbols
6)operators.
Keywords and identifiers.
• Every C word is classified as either a keyword or an identifier.

• All keywords have fixed meanings and these meanings cannot


be changed.

• Keywords serve as basic building blocks for program


statements.

• All keywords must be written in lower case.

• The underscore character is also permitted in identifiers.

• It is usually used a link between two words in long identifiers


Identifiers
• Identifiers refer to the names of variables,
functions and arrays.
RULES FOR IDENTIFIERS:
1. First character must be an alphabet.
2. Must consist of only letters, digits or
underscore.
3. Only first 31 characters are significant.
4. Cannot use keyword.
5. Must not contain white space.
CONSTANTS
• Constants refer to fixed values that do not change during the execution of
program.
INTEGER CONSTANTS:
• An integer constant refer to a sequence of digits.

There are 3 types of integers namely: Decimal integer, octal integer and
hexadecimal integer.

• Decimal integer consist of a set of digits 0 through 9,precceded by an optional


– or + sign.
• Ex: 123,-321,0

• An octal integer constant consist of any combination of digits from the set 0
through 7. with a leading 0
Eg: 037,0, 0456.

• A sequence of digits preceded by 0x or 0X is considered as hexadecimal


integer.

• They may include alphabets A through F or f. Letter A through F represents


numbers 10 to 15.
• Ex:0X2,0x9F
REAL CONSTANTS:
• To represent quantities that vary continuously real constants
are used.

• A real number may be expressed in exponential notation.


SYNTAX: mantissa e exponent.

• Mantissa can be either real number expressed in decimal


notation or an integer.

• Exponent is an integer number with an optional + or – sign.

• The letter ‘e’ separating the mantissa and the exponent, it can
be written either lower case or upper case.
SYNTAX: 0.65e4,12e-2.
• White space is not allowed.

• Exponential notation is useful for representing numbers that


are either very large or very small in magnitude.

• Floating point constants are normally represented as double-


precision quantities.

SINGLE CHARACTER CONSTANTS:

• A single character constant contains a single character enclose


in a pair of single quote marks.
Eg: ‘5’,’x’.
• Character constant ‘5’ is not same as number 5.

• Character constants have integer values known as ASCII


values.

• Statement: printf (“%d”, ’a’); would print number 97, the


ASCII value of letter ‘a’.
STRING CONSTANTS:
• A string constant is a sequence of characters enclosed in
double quotes.

• Characters may be letters, numbers, special characters and


blank spaces.
Eg: “hello” , “1987”, “?...!”.

• Character constant is not equivalent to single character string


constant.
.
BACK SLASH CHARACTER CONSTANTS:

• C supports some special back slash character constants that


are used in output functions.

• These characters combinations are known as escape


sequences.

• Back slash character constants are:

‘\a’ audible alert; ‘\b’ backspace; ‘\f’ form feed; ‘\n’ newline; ‘\
r’ carriage return; ‘\t’ horizontal tab; ‘\v’ vertical tab; ‘\”single
quote, ‘\?’ question mark; ‘\\’ backslash; ‘\0’ null.
#include <stdio.h>
int main()
{
const int height = 100; /*int constant*/
const float number = 3.14; /*Real constant*/
const char letter = 'A'; /*char constant*/
const char letter_sequence[10] = "ABC"; /*string constant*/
const char backslash_char = '\?'; /*special char cnst*/

printf("value of height :%d \n", height );


printf("value of number : %f \n", number );
printf("value of letter : %c \n", letter );
printf("value of letter_sequence : %s \n", letter_sequence);
printf("value of backslash_char : %c \n", backslash_char);
}
#include<stdio.h>
#define val 10
#define floatVal 4.5
#define charVal 'G'

int main()
{
printf("Integer Constant: %d\n",val);
printf("Floating point Constant: %f\n",floatVal);
printf("Character Constant: %c\n",charVal);

return 0; Integer Constant: 10


} Floating point Constant: 4.500000
Character Constant: G
VARIABLES
• A variable is a data name that may be used to store a
data value.
• A variable may take different values at different times
during execution.
• A variable can be chosen by the programmer in a
meaningful way.
• A variable is a container (storage area) to hold data.
CONDITIONS FOR SPECIFYING VARIABLES:
1. They must begin with a letter. Some systems may permit
underscore as first character.

2. Uppercase and lowercase are significant. The variable TOTAL


is different from total and Total.

3. It should not be keyword.

4. Whitespace is not allowed.

• Examples of valid variables are:


john, x1, T_raise, first_tag.

• Examples of invalid variables are:


123, (area), 25th, price$, %.
ENUMERATION DATA TYPE
• Enumeration data type consists of named
integer constants as a list.
• It start with 0 (zero) by default and value is
incremented by 1 for the sequential identifiers
in the list.
Syntax
• enum identifier [optional{ enumerator-list }];
• enum month { Jan, Feb, Mar };
• enum State {Working = 1, Failed = 0};

#include<stdio.h>
enum week{Mon, Tue, Wed, Thur, Fri, Sat, Sun};
int main()
{
enum week day;
day = Wed;
printf("%d",day);
return 0;
}
Output: 2
#include<stdio.h>

int main()
{
enum days {MON=-1, TUE, WED=6, THU, FRI,
SAT};
printf("%d, %d, %d, %d, %d, %d\n", MON, TUE,
WED, THU, FRI, SAT);
return 0;
}
Output: -1, 0, 6, 7, 8, 9

You might also like