ankitweblogic logo
W e b t u t o r i a l s

C Tutorial

Programing Approach

Algorithm

Flow Chart

Define C

Define Programming

C - Syntax

Compiler vs Interpreter

C - Variables

C - Keywords

C - Data Types

C - Comments

Turbo C - Shortcut keys

C - Operators

Hierarchy of Operators

Ex: Arithmetic Operator

C - Formatting Output

Constant Variable

C - Escape Sequence

C - if statement

Ex: If statement

C - switch statement

Ex: Inc / Dec Operator

C - loops

Ex: Loops

C - Nesting Of loops

Ex: Nested Loops

Jumping Statements

C - Arrays 1D

C - Arrays 2D

C - Sorting

ASCII Value

Character I/O Function

C - String Functions

Ex: Strings

Array of Strings

C - Math Functions

User-defined Function

Exercise Function

C - Types of Functions

Storage Class

C - Header Files

C - Preprocessor

C - Pointers

C - Structures

C - Typedef

C - File Handling

Ex: File Handling

Command Line Argument

MCQ

C - Structure

Structure is a user-defined data type, which allows you to combine data items of different kinds.

Default value of a structure:
Struct emp p= {11, 20.50, "abc"};

Array of Structures

Pointer to Structures

Nested Structures:

Structures as Function Arguments

You can pass a structure as a function argument in very similar way as you pass any other variables.

Example : Structure as function argument

#include <stdio.h>
#include <string.h>

struct Books
{
    char title[50];
    char author[50];
    char subject[100];
    int book_id;
};

/* function declaration */
void printBook( struct Books book );
int main( )
{
   struct Books Book1; /* Declare Book1 of type Book */
   struct Books Book2; /* Declare Book2 of type Book */

   /* book 1 specification */
   strcpy( Book1.title, "C Programming");
   strcpy( Book1.author, "Nuha Ali");
   strcpy( Book1.subject, "C Programming Tutorial"); 
   
   Book1.book_id = 6495407;
   
   /* book 2 specification */
   strcpy( Book2.title, "Telecom Billing");
   strcpy( Book2.author, "Zara Ali");
   strcpy( Book2.subject, "Telecom Billing Tutorial");
   
   Book2.book_id = 6495700;
   
   /* print Book1 info */
   printBook( Book1 );
   
   /* Print Book2 info */
   printBook( Book2 );
	
   return 0;
}
void printBook( struct Books book )
{
   printf( "Book title : %s\n", book.title);
   printf( "Book author : %s\n", book.author);
   printf( "Book subject : %s\n", book.subject);
   printf( "Book book_id : %d\n", book.book_id);
}

Example : Pointer structure as function argument

#include <stdio.h>
#include <string.h>

struct Books
{
   char title[50];
   char author[50];
   char subject[100];
   int book_id; 
};
 
/* function declaration */
void printBook( struct Books *book );
void main( )
{
   struct Books Book1; /* Declare Book1 of type Book */
   struct Books Book2; /* Declare Book2 of type Book */
   
   /* book 1 specification */
   strcpy( Book1.title, "C Programming");
   strcpy( Book1.author, "Nuha Ali");
   strcpy( Book1.subject, "C Programming Tutorial");
   
   Book1.book_id = 6495407;
   
   /* book 2 specification */
   strcpy(Book2.title, "Telecom Billing");
   strcpy(Book2.author, "Zara Ali");
   strcpy(Book2.subject, "Telecom Billing Tutorial");
   
   Book2.book_id = 6495700;
   /* print Book1 info by passing address of Book1 */
   printBook(&Book1 );
   
   /* print Book2 info by passing address of Book2 */
   printBook(&Book2 );
   getch();
}

void printBook(struct Books *book )
{
   printf("Book title : %s\n", book->title);
   printf("Book author : %s\n", book->author);
   printf("Book subject : %s\n", book->subject);
   printf("Book book_id : %d\n", book->book_id);
}

1. Create a structure to maintain the students record. Structure should contain name, 5 subject marks, total and average.
a. Find the record of a student who score highest marks
b. Who score lowest marks
c. Display grade corresponding to the percentage.

2. Create a structure to specify data of customers in a bank. The data to be stored is: Account number, Name, Balance in account. Assume maximum of 200 customers in the bank.
(a) Write a function to print the Account number and name of each customer with balance below Rs. 100.
(b) If a customer request for withdrawal or deposit, it is given in the form: Acct. no, amount, code (1 for deposit, 0 for withdrawal)
Write a program to give a message, "The balance is insufficient for the specified withdrawal".

3. There is a structure called employee that holds information like employee code, name, date of joining. Write a program to create an array of the structure and enter some data into it. Then ask the user to enter current date. Display the names of those employees whose tenure is 3 or more than 3 years according to the given current date.

4. Write a menu driven program that depicts the working of a library. The menu options should be:
1. Add book information
2. Display book information
3. List all books of given author
4. List the title of specified book
5. List the count of books in the library
7. Exit Create a structure called library to hold accession number, title of the book, author name, price of the book, and flag indicating whether book is issued or not.

Ad

Burkolóként nem csak tesszük egymás mellé a csempéket, padlólapokat vagy egyéb hidegburkoló anyagokat, de bizony szükség van ezek megmunkálására is. Ehhez nyújt kiváló segítséget a csempevágó , hiszen a RUBI termékekre jellemző magas minőség és precizitás jellemzi.

Unions

Unions are the same as a structures but the difference is that structure will take separate memory location for the variables declare in it whereas union will share memory space for all the variables. Union may affect the original value but structure will not affect the values.

Difference between Structure and Union
Sno Structure Union
1. Each member assign unique storage area. All the member share same storage area.
2. Each member can assign different values. Each member need not be assigned a different value.
Updated: 13-May-19