|
C Programming Tutorials
Basics of C:
Facts about C
Why to Use C
C Program File
C Compilers
Program Structure:
Simple C Program
C
Program Compilation
Basic DataTypes:
DataTypes
Modifiers
Qualifiers
Arrays
Variable Types:
Local Variable
Global
Variable
Storage Classes:
auto storage class
register storage
class
static storage
class
extern storage
class
Using Constants:
Defining Constants
The enum Data Types
Operator Types:
Arithmetic Operators
Logical Operators
Bitwise Operators
Assignment Operators
Misc
Operators
Control Statements:
Branching
Looping
Input and Output:
printf() function
scanf() function
Pointing to Data:
Pointers and Arrays
Pointer
Arithmetic
Pointer Arithmetic
with arrays
Functions:
Using functions
Declaration and
Definition
Strings:
Reading and Writing
Strings
String Manipulation Function
Structured DataTypes:
Structure
Pointer to Structure
Working with Files:
Files
Basic I/O
Bits:
Bits Manipulation
Bits Field
Pre-Processors:
Pre-Processors Examples
Parameterized Macros
Macro
Caveats
Useful Concepts
Built-in Library Functions:
String Manipulation
Function
Memory Management
Function
Buffer
Manipulation
Character
Functions
Error Handling
Functions
Soft Skills
Communication Skills
Leadership Skills
.........More
|
|
C Programming Tutorials
Structured Datatypes
-
A structure in C is a collection of items of different
types. You can think of a structure as a "record" is in Pascal or a class in
Java without methods.
-
Structures, or structs, are very useful in creating data
structures larger and more complex than the ones we have discussed so far.
-
Simply you can group various built-in data types into a
structure.
-
Object concepts was derived from Structure concept. You
can achieve few object oriented goals using C structure but it is very
complex.
Following is the example how how to define a structure.
struct student
{
char firstName[20];
char lastName[20];
char SSN[9];
float gpa;
}; |
Now you have a new datatype called student and you can use
this datatype define your variables of student type:
struct student student_a, student_b;
or an array of students as
struct student students[50]; |
Another way to declare the same thing is:
struct {
char firstName[20];
char lastName[20];
char SSN[10];
float gpa;
} student_a, student_b; |
All the variables inside an structure will be accessed using
these values as student_a.firstName will give value of firstName variable.
Similarly we can access other variables.
Structure Example:
Try out following example to understand the concept:
#include <stdio.h>
struct student {
char firstName[20];
char lastName[20];
char SSN[10];
float gpa;
};
main()
{
struct student student_a;
strcpy(student_a.firstName, "Deo");
strcpy(student_a.lastName, "Dum");
strcpy(student_a.SSN, "2333234" );
student_a.gpa = 2009.20;
printf( "First Name: %s\n", student_a.firstName );
printf( "Last Name: %s\n", student_a.lastName );
printf( "SNN : %s\n", student_a.SSN );
printf( "GPA : %f\n", student_a.gpa );
} |
This will produce following results:
First Name: Deo
Last Name: Dum
SSN : 2333234
GPA : 2009. |
Pointers to Structs:
Sometimes it is useful to assign pointers to structures (this will be evident in
the next section with self-referential structures). Declaring pointers to
structures is basically the same as declaring a normal pointer:
| struct student *student_a; |
To dereference, you can use the infix operator: ->.
| printf("%s\n", student_a->SSN); |
typedef Keyword
There is an easier way to define structs or you could "alias" types you create.
For example:
typedef struct{
char firstName[20];
char lastName[20];
char SSN[10];
float gpa;
}student; |
Now you can use student directly to define variables of
student type without using struct keyword. Following is the example:
You can use typedef for non-structs:
typedef long int *pint32;
pint32 x, y, z; |
x, y and z are all pointers to long ints
Unions Datatype
Unions are declared in the same fashion as structs, but have a fundamental
difference. Only one item within the union can be used at any time, because the
memory allocated for each item inside the union is in a shared memory location.
Here is how we define a Union
union Shape
{
int circle;
int triangle;
int ovel;
}; |
We use union in such case where only one condition will be
applied and only one variable will be used.
Conclusion:
You can create arrays of structs.
Structs can be copied or assigned.
The & operator may be used with structs to show addresses.
Structs can be passed into functions. Structs can also be returned from
functions.
Structs cannot be compared!
Structures can store non-homogenous data types into a single collection, much
like an array does for common data (except it isn't accessed in the same
manner).
Pointers to structs have a special infix operator: -> for dereferencing the
pointer.
typedef can help you clear your code up and can help save some keystrokes.
NEXT >> Working With Files
Have a Question ?
post your questions here. It
will be answered as soon as possible.
Check
C Aptitude Questions
for more C Aptitude Interview Questions with Answers
Check
C Interview Questions
for more C Interview Questions with Answers
|