Preprocessor chapter recap
This commit is contained in:
parent
84071736e2
commit
f4401884b8
6 changed files with 46 additions and 11 deletions
|
|
@ -2,14 +2,17 @@
|
|||
#include <stdio.h>
|
||||
|
||||
#define FREEZING_PT 32.0f
|
||||
#define SCALE_FACTOR (5.0f / 9.0f)
|
||||
#define BEGIN {
|
||||
#define END }
|
||||
|
||||
int main(void) {
|
||||
int main(void)
|
||||
BEGIN
|
||||
float farenheit, celsius;
|
||||
|
||||
printf("Enter Fahrenheit temperature: ");
|
||||
scanf("%f", &farenheit);
|
||||
|
||||
#define SCALE_FACTOR (5.0f / 9.0f)
|
||||
celsius = (farenheit - FREEZING_PT) * SCALE_FACTOR;
|
||||
|
||||
printf("Celcius equivalent is: %.1f\n", celsius);
|
||||
|
|
|
|||
|
|
@ -1,7 +1,15 @@
|
|||
#include <stdio.h>
|
||||
#define MK_ID(n) i##n
|
||||
#define MK_ID(n) kakulya_##n
|
||||
|
||||
#define GENERIC_MAX(type) \
|
||||
type type##_max(type x, type y) { return x > y ? x : y; }
|
||||
|
||||
GENERIC_MAX(float);
|
||||
/* The macro above will transorf into something like following:
|
||||
* float float_max(float x, float y) { return x > y ? x : y; }
|
||||
*/
|
||||
|
||||
int main(int argc, char *argv[]) {
|
||||
int MK_ID(1); // This converts to `int i1`
|
||||
printf("%d", i1);
|
||||
int MK_ID(1); // This converts to `int kakulya_1`
|
||||
printf("%d", kakulya_1);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,9 +1,13 @@
|
|||
#include <stdio.h>
|
||||
#define MAX(x, y) ((x) > (y) ? (x) : (y))
|
||||
#define IS_EVEN(n) ((n) % 2 == 0)
|
||||
#define IS_EVEN_STR(n) ((n) % 2 == 0 ? "yes" : "no")
|
||||
|
||||
int main(int argc, char *argv[]) {
|
||||
int j, k;
|
||||
j = 10;
|
||||
k = 20;
|
||||
printf("%d", MAX(j, k));
|
||||
printf("%d\n", MAX(j, k));
|
||||
printf("%d is even: %d\n", j, IS_EVEN(j));
|
||||
printf("%d is even? %s\n", j, IS_EVEN_STR(j));
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,9 +1,10 @@
|
|||
#include <stdio.h>
|
||||
#define PRINT_INT(n) printf(#n " AND " #n " = %d\n", n)
|
||||
|
||||
int main(void) {
|
||||
int i, j;
|
||||
i = 10;
|
||||
j = 20;
|
||||
PRINT_INT(i / j);
|
||||
int main(void)
|
||||
{
|
||||
unsigned long int k = 10;
|
||||
printf("sizeof k: %lu", k);
|
||||
int x = sizeof(int);
|
||||
printf("sizeof k: %d", x);
|
||||
}
|
||||
|
|
|
|||
8
c_modern_approach/Preprocessor/predefined.c
Normal file
8
c_modern_approach/Preprocessor/predefined.c
Normal file
|
|
@ -0,0 +1,8 @@
|
|||
#include <stdc-predef.h>
|
||||
#include <stdio.h>
|
||||
int main() {
|
||||
// printf("%s", __STDC_IEC_559__);
|
||||
// printf("%s", __STDC_IEC_559_COMPLEX__);
|
||||
printf("%d\n", __STDC_VERSION__);
|
||||
printf("%d", __STDC_HOSTED__);
|
||||
};
|
||||
11
c_modern_approach/Preprocessor/single_hashtag_op.c
Normal file
11
c_modern_approach/Preprocessor/single_hashtag_op.c
Normal file
|
|
@ -0,0 +1,11 @@
|
|||
#include <stdio.h>
|
||||
#define PRINT_INT(n) printf(#n " = %d\n", n)
|
||||
|
||||
int main() {
|
||||
int i = 10;
|
||||
PRINT_INT(i);
|
||||
}
|
||||
|
||||
/* This program with the help of `#` operator will conver #n to string literal
|
||||
* of an argument, so the output of the program will be: i = 10
|
||||
*/
|
||||
Loading…
Reference in a new issue