0% found this document useful (0 votes)
35 views10 pages

DSA - Unit1 - U-3

Structures syntax
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
35 views10 pages

DSA - Unit1 - U-3

Structures syntax
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 10

Primitive Data Types – floating point

• It stores fractional numbers (real numbers)


• Usually requires 4 bytes of memory
• Format specifier - %f
• Keyword – float
• Example – float factor=22.442e2;
Primitive Data Types – void
• is an incomplete type. i.e. "nothing" or "no type".
• It is used in three kinds of situations
Type Description Example

Function returns as A function with no return void exit (int status);


void value has the return type as
void.
Function arguments functions which do not accept int rand(void);
as void any parameter.
Pointers to void A pointer of type void * void *malloc( size_t size );
represents the address of an
object, but not its type returns a pointer to void which
can be casted to any data type
Derived Data Types
• A derived type is formed by using one or more
basic types in combination.
• Using derived types, an infinite variety of new types can
be formed.
• They are
• arrays
• pointers
Derived Data Types – arrays
• An array is a collection of similar data types under a single variable
name in continuous memory location
• i.e. one or more than one integers can be stored under a single
name
• similarly for other data types too
• Example:
int new[5] = {1,2,3,4,5};
char name = “Program”;
Derived Data Types – pointers
• A pointer is a special variable that stores address of another variable
• Declare a pointer before using it to store any variable address.
• Syntax:
type *ptr-variable-name; //declaration
variable-name = & ptr-variable-name; //assignment
• Example:
int *p;
int num;
p = #
User-defined Data Types (UDT)
• User-defined datatypes use the built-in datatypes and
other user-defined datatypes
• They are also called as data structures
• They are:
✓ Structure
✓ Union
✓ Enumeration
User-defined Data Types - Structure
• It is a package of variables of different types under a single
name.
• Structures are used to represent a record
• The struct statement defines a new data type, with more than
one member.
• Keyword – struct Example:
• Syntax: struct book {
struct variable { int book-id = 234;
member(s); char name[20] = “C – Program”;
}; variable(s); } b1,b2;
User-defined Data Types - Union
• A union is a special data type that allows to store different
data types in the same memory location.
• You can define a union with many members, but only one
member can contain a value at any given time.
• Unions provide an efficient way of using the same memory
location for multiple-purpose.
• The union tag is optional.
User-defined Data Types - Union
 Example:
• Keyword – union union number
 Now, the union variable can store
an integer, a floating-point
• Syntax: { number, or a string of characters.
int roll;  It means a single variable, i.e.,
union variable char option; same memory location, can be
used to store multiple types of
{ float mark; data.
} n1,n2;
member(s);
}; variable(s);
• The memory occupied by a union will be large
enough to hold the largest member of the union
• In the above example, the memory allocated is for
float which is the largest data type
User defined Data Types - Enumeration
• Enumeration is a user defined datatype in C language.
• It is used to assign names to the integral constants which
makes a program easy to read and maintain.
• Keyword – enum
• Syntax:
enum variable {constant1, constant2,…}
• Example –
enum week{Sunday,Monday,Tuesday,Wednesday,…….};
enum week day;

You might also like