C Programming Code Examples
C > Small Programs Code Examples
Positional notation of numbers
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
/* Positional notation of numbers */
#include <stdio.h>
#include <math.h>
#define PACKAGE "digits"
int number_of_digits(int x);
void print_help(void);
int main(int argc, char *argv[]) {
int n; /* input; the number to convert */
short base; /* input; base to which we will convert n */
short rhdigit; /* right-hand digit of n-prime */
int power; /* loop */
if(argc != 3) {
print_help();
return 1;
} else {
sscanf(argv[1], "%d", &n);
if(n < 1) {
fprintf(stderr, "%s: Error: Number must be greater than 0.\n", PACKAGE);
return 1;
}
sscanf(argv[2], "%hi", &base);
if(base < 2 || base > 10) {
fprintf(stderr, "%s: Error: Base is not in required range.", PACKAGE);
return 1 ;
}
} /* else */
printf("The number %d, is %d wide, ", n, number_of_digits(n));
printf("and contains the numbers: ");
/*
// Generate digits of converted number, right to left.
*/
for(power = 0; n != 0; power++) {
rhdigit = n % base; /* Isolate right-hand digit of n. */
n /= base; /* then eliminate right-hand digit. */
printf("%hi ", rhdigit);
}
printf("\n");
return 0;
}
int number_of_digits(int x) {
return x ? (int)(log10((double)(abs(x))))+1 : 1;
}
void print_help(void) {
fprintf(stdout, "Usage : %s [NUMBER] [BASE]\n", PACKAGE);
fprintf(stdout, "Example: %s 4591 10\n", PACKAGE);
}
Compute common logarithm. Returns the common (base-10) logarithm of x. Calculates the base-10 logarithm of a number. The log10() functions calculate the common logarithm of their argument. The common logarithm is the logarithm to base 10. The common logarithm of a number x is defined only for positive values of x. If x is negative, a domain error occurs; if x is zero, a range error may occur. Function returns common logarithm of x. If x is negative, it causes a domain error. If x is zero, it may cause a pole error (depending on the library implementation).
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.
Assignment operators are used to assign the value, variable and function to another variable. Assignment operators in C are some of the C Programming Operator, which are useful to assign the values to the declared variables. Let's discuss the various types of the assignment operators such as =, +=, -=, /=, *= and %=. The following table lists the assignment operators supported by the C language: Simple assignment operator. Assigns values from right side operands to left side operand. Add AND assignment operator. It adds the right operand to the left operand and assign the result to the left operand.
Write formatted data to stream. Writes the C string pointed by format to the stream. 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. After the format parameter, the function expects at least as many additional arguments as specified by format.
Read formatted data from string. Reads data from s and stores them according to parameter format into the locations given by the additional arguments, as if scanf was used, but reading from s instead of the standard input (stdin). The additional arguments should point to already allocated objects of the type specified by their corresponding format specifier within the format string.
Absolute value. The abs () function is a predefined function in the stdlib.h header file to return the absolute value of the given integers. So, if we want to return the absolute value of a given number, we need to implement the stdlib.h header file in the C program. The abs() function only returns the positive numbers.
An expression containing logical operator returns either 0 or 1 depending upon whether expression results true or false. Logical operators are commonly used in decision making in C programming. These operators are used to perform logical operations and used with conditional statements like C if-else statements.
#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:
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 the C Programming Language, the #define directive allows the definition of macros within your source code. These macro definitions allow constant values to be declared for use throughout your code. Macro definitions are not variables and cannot be changed by your program code like variables. You generally use this syntax when creating constants that represent numbers, strings or expressions.
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.
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,
The if-else statement is used to perform two operations for a single condition. The if-else statement is an extension to the if statement using which, we can perform two different operations, i.e., one is for the correctness of that condition, and the other is for the incorrectness of the condition. Here, we must notice that if and else block cannot be executed simiulteneously. Using if-else statement is always preferable since it always invokes an otherwise case with every if condition.
Put on the motor. Address of digital control port. Check whether the FDC is ready. Read the status of MSR. Check whether FDC is ready. Beginning of command phase. Input
Program code take the MxN matrix as input. Define two functions separately for row sum and column sum. Use for loops to calculate the sum of the elements of each row and...
Program input a number from user and check whether given number is Even or Odd using functions. Declare function to find even odd. First give a meaningful name to our function,
Print phonetic alphabet. Print lower case greek alphabet. Print lower case hebrew alphabet. Print the lower case phoenician alphabet. Print lower case arab alphabet.