These examples are a companion Practical B in A Crash Course in C.
char type |
/* Program to illustrate the char type : -- tested on RedHat 7.2, AMD Duron, GCC 2.96 20000731 : -- shows that : -- a char variable has values from 0 to 255 (and "wraps" thereafter"); -- interpreted as a character, each value 0 -- 255 represents a different character in the character set; -- char can be used as a "very-small int" and so-interpreted, the ranger is -128 -- 127 if 8-bit; -- C is a systems-level language, not a high-level language; */ #include <stdio.h> void print_int_then_char_repr(char c) { printf("\n c as a decimal integer is: %d", c); printf("\n c as a character is : %c", c); printf("\n"); } main() { /* A small value : */ print_int_then_char_repr(3); /* A mid value : */ print_int_then_char_repr(77); /* Values just before and just after the middle of the range : */ print_int_then_char_repr(127); print_int_then_char_repr(128); /* Some of the largest possible char values : */ print_int_then_char_repr(254); print_int_then_char_repr(250); /* Values outside the range --- note the compiler warnings --- values "wrap" : */ print_int_then_char_repr(-2); print_int_then_char_repr(259); } |
Example 1 |
#include <stdio.h> main() { int i = 9; printf("Value is %d \n", I); } |
Example 2 |
#include <stdio.h> main() { int i = 9; printf("Value is d \n",i) } |
Example 3 |
/* Programme to print out the numbers 1 to 10 : */ #include <stdio.h> main() { for (i = 1; i = 10; i++); printf("%d ",i); printf("\n"); } |
Example 4 |
#include <stdio.h> main() { int x = 14, y = 5; printf("%d \n", x/y); printf("%d \n", x%y); printf("%d \n", (float)x/y); printf("%f \n", (float)x/y); } |