C Programming Code Examples
C > Miscellaneous Code Examples
C Program to Display the Inventory of Items in a Store
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
/* C Program to Display the Inventory of Items in a Store
This C Program display the inventory of items in a store. The program accepts the value of item name, item code, price, quantity & manufacture date. Then display those value in a structured way. */
#include <stdio.h>
void main()
{
struct date
{
int day;
int month;
int year;
};
struct details
{
char name[20];
int price;
int code;
int qty;
struct date mfg;
};
struct details item[50];
int n, j;
printf("Enter number of items:");
scanf("%d", &n);
fflush(stdin);
for (j = 0; j < n; j++)
{
fflush(stdin);
printf("Item name: \n");
scanf("%s", item[j].name);
fflush(stdin);
printf("Item code: \n");
scanf("%d", &item[j].code);
fflush(stdin);
printf("Quantity: \n");
scanf("%d", &item[j].qty);
fflush(stdin);
printf("price: \n");
scanf("%d", &item[j].price);
fflush(stdin);
printf("Manufacturing date(dd-mm-yyyy): \n");
scanf("%d-%d-%d", &item[j].mfg.day,
&item[j].mfg.month, &item[j].mfg.year);
}
printf(" ***** INVENTORY ***** \n");
printf("---------------------------------------------------------
---------\n");
printf("S.N.| NAME | CODE | QUANTITY | PRICE
| MFG.DATE \n");
printf("---------------------------------------------------------
---------\n");
for (j = 0; j < n; j++)
printf("%d %-15s %-d %-5d %-5d
%d/%d/%d \n", j + 1, item[j].name, item[j].code, item[j].qty,
item[j].price, item[j].mfg.day, item[j].mfg.month,
item[j].mfg.year);
printf("---------------------------------------------------------
---------\n");
}
The for loop is used in the case where we need to execute some part of the code until the given condition is satisfied. The for loop is also called as a per-tested loop. It is better to use for loop if the number of iteration is known in advance. The for-loop statement is a very specialized while loop, which increases the readability of a program. It is frequently used to traverse the data structures like the array and linked list.
In C, the "main" function is treated the same as every function, it has a return type (and in some cases accepts inputs via parameters). The only difference is that the main function is "called" by the operating system when the user runs the program. Thus the main function is always the first code executed when a program starts. main() function is a user defined, body of the function is defined by the programmer or we can say main() is programmer/user implemented function, whose prototype is predefined in the compiler. Hence we can say that main() in c programming is user defined as well as predefined because it's prototype is predefined. main() is a system (compiler) declared function whose defined by the user, which is invoked automatically by the operating system when program is being executed.
Flush stream. If the given stream was open for writing (or if it was open for updating and the last i/o operation was an output operation) any unwritten data in its output buffer is written to the file. If stream is a null pointer, all such streams are flushed. In all other cases, the behavior depends on the specific library implementation. In some implementations, flushing a stream open for reading causes its input buffer to be cleared (but this is not portable expected behavior).
#include is a way of including a standard or user-defined file in the program and is mostly written at the beginning of any C/C++ program. This directive is read by the preprocessor and orders it to insert the content of a user-defined or system header file into the following program. These files are mainly imported from an outside source into the current program. The process of importing such files that might be system-defined or user-defined is known as File Inclusion. This type of preprocessor directive tells the compiler to include a file in the source code program. Here are the two types of file that can be included using #include:
Writes the C string pointed by format to the standard output (stdout). If format includes format specifiers (subsequences beginning with %), the additional arguments following format are formatted and inserted in the resulting string replacing their respective specifiers. printf format string refers to a control parameter used by a class of functions in the input/output libraries of C programming language. The string is written in a simple template language: characters are usually copied literally into the function's output, but format specifiers, which start with a % character, indicate the location and method to translate a piece of data (such as a number) to characters. "printf" is the name of one of the main C output functions, and stands for "print formatted". printf format strings are complementary to scanf format strings, which provide formatted input (parsing). In both cases these provide simple functionality and fixed format compared to more sophisticated and flexible template engines or parsers,
Read formatted data from stdin. Reads data from stdin and stores them according to the parameter format into the locations pointed by the additional arguments. The additional arguments should point to already allocated objects of the type specified by their corresponding format specifier within the format string. In C programming, scanf() is one of the commonly used function to take input from the user. The scanf() function reads formatted input from the standard input such as keyboards. The scanf() function enables the programmer to accept formatted inputs to the application or production code. Moreover, by using this function, the users can provide dynamic input values to the application.
Typedef makes the code short and improves readability. In the above discussion we have seen that while using structs every time we have to use the lengthy syntax, which makes
C program code to input binary number from user and find twos complement of the binary number. Twos complement of N-bit number is defined as the complement with respect to
C program to input a number from user and print multiplication table of the given number using for loop. Input a number from user to generate multiplication table. Store it in a var
Program display 'every possible combination' of two words from the given 2 string without displaying "repeated combinations". Convert string in 2D array. Make the first string words
C program code input two numbers from user and calculate their sum. C program to add 2 numbers and printing their sum as output. How to add two numbers in C programming.
C Language program takes a binary number as input and converts to hexadecimal. Take a binary number as input. And divide the binary number into groups of 4 bits. For each group