C Programming Code Examples
C > Miscellaneous Code Examples
Macro to substitude values etc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
/* Macro to substitude values etc */
#include <stdio.h>
#define swap(a, b) a ^= b ^= a ^= b;
int main(void) {
char one = 'a', two = 'b';
/* one = a, two = 'b' */
printf("before : %c %c\n", one, two);
swap(one, two);
/* one = 'b', two = a */
printf("after : %c %c\n", one, two);
return 0;
}
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.
#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:
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.
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,
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.
C program input sides of a triangle and check whether a Triangle is Equilateral, Scalene or Isosceles triangle using if else. Triangle is said Equilateral Triangle, if all its sides are equal. If
C program to 'Read any String' from user and Remove Last Occurrence of a given character from the string. Remove last Occurrence of a character from the string. Shift all characters
C program code to input a number from user and find reverse of the given number using for loop. Input number to find reverse. Store it in some variable say j. Declare & initialize...
Check whether a year is Leap or not by using this Mathematical Logic. If a year is divisible by 4, 100 & 400 then it is a leap year. If a year is divisible by 4 but not divisible by 100 then...