Processing variable arguments

Wednesday, October 15, 2014
/* 1503.c: Processing variable arguments */
#include <stdio.h>
#include <stdarg.h>

double AddDouble(int x, ...);

main ()
{
    double d1 = 1.5;
    double d2 = 2.5;
    double d3 = 3.5;
    double d4 = 4.5;

    printf("Given an argument: %2.1f\n", d1);
    printf("The result returned by AddDouble() is: %2.1f\n\n",
    AddDouble(1, d1));
    printf("Given arguments: %2.1f and %2.1f\n", d1, d2);
    printf("The result returned by AddDouble() is: %2.1f\n\n",
    AddDouble(2, d1, d2));
    printf("Given arguments: %2.1f, %2.1f and %2.1f\n", d1, d2, d3);
    printf("The result returned by AddDouble() is: %2.1f\n\n",
    AddDouble(3, d1, d2, d3));
    printf("Given arguments: %2.1f, %2.1f, %2.1f, and %2.1f\n", d1, d2, d3, d4);
    printf("The result returned by AddDouble() is: %2.1f\n",
    AddDouble(4, d1, d2, d3, d4));
    return 0;
    }
    /* definition of AddDouble() */
    double AddDouble(int x, ...)
    {
    va_list arglist;
    int i;
    double result = 0.0;

    printf("The number of arguments is: %d\n", x);
    va_start (arglist, x);
    for (i=0; i<x; i++)
    result += va_arg(arglist, double);
    va_end (arglist);
    return result;
}

Pointer subtraction

/* 1602.c: Pointer subtraction */
#include <stdio.h>

main()
{
    int *ptr_int1, *ptr_int2, i;

    printf("The position of ptr_int1: 0x%p\n", ptr_int1);
    ptr_int2 = ptr_int1 + 5;
    printf("The position of ptr_int2 = ptr_int1 + 5: 0x%p\n", ptr_int2);
    printf("The subtraction of ptr_int2 - ptr_int1: %d\n", ptr_int2 - ptr_int1);
    ptr_int2 = ptr_int1 - 5;
    printf("The position of ptr_int2 = ptr_int1 - 5: 0x%p\n", ptr_int2);
    printf("The subtraction of ptr_int2 - ptr_int1: %d\n", ptr_int2 - ptr_int1);

    return 0;
}

Functions with no arguments

/* 1502.c: Functions with no arguments */
#include <stdio.h>
#include <time.h>

void GetDateTime(void);

main()
{
    printf("Before the GetDateTime() function is called.\n");
    GetDateTime();
    printf("After the GetDateTime() function is called.\n");
    return 0;
}
/* GetDateTime() definition */
void GetDateTime(void)
{
    time_t now;

    printf("Within GetDateTime().\n");
    time(&now);
    printf("Current date and time is: %s\n", asctime(localtime(&now)));
}

Using the register specifier

/* 1404.c: Using the register specifier */
#include <stdio.h>

int main()
{
    /* block scope with the register specifier */
    register int i;
    int MAX_NUM=10;
    for (i=0; i<MAX_NUM; i++){
        printf("Value of i:%d\n",i);
    }
    return 0;
}

Using the volatile variable

/* 1405.c: Using the volatile variable */
#include <stdio.h>

int main()
{
    read_keyboard();
    return 0;
}

void read_keyboard()
{
volatile char keyboard_ch[100];
printf("Enter a string:\n");
scanf("%s", keyboard_ch);
printf("Keyboard read = %s\n",keyboard_ch);
}

Making function calls

/* 1501.c: Making function calls */
#include <stdio.h>

int function_1(int x, int y);
double function_2(double x, double y)
{
printf("Within function_2.\n");
return (x - y);
}

main()
{
    int x1 = 80;
    int y1 = 10;
    double x2 = 100.123456;
    double y2 = 10.123456;

    printf("Pass function_1 %d and %d.\n", x1, y1);
    printf("function_1 returns %d.\n", function_1(x1, y1));
    printf("Pass function_2 %f and %f.\n", x2, y2);
    printf("function_2 returns %f.\n", function_2(x2, y2));
    return 0;
}
/* function_1() definition */
int function_1(int x, int y)
{
    printf("Within function_1.\n");
    return (x + y);
}

Scopes in nested block

/* 1401.c: Scopes in nested block */
#include <stdio.h>

main()
{
    int i = 32; /* block scope 1*/

    printf("Within the outer block: i=%d\n", i);

    { /* the beginning of the inner block */
    int i, j; /* block scope 2, int i hides the outer int i*/

    printf("Within the inner block:\n");
    for (i=0, j=10; i<=10; i++, j--)
    printf("i=%2d, j=%2d\n", i, j);
    } /* the end of the inner block */
    printf("Within the outer block: i=%d\n", i);
    return 0;
}

program scope

/* 1402.c: Program scope vs block scope */
#include <stdio.h>

int x = 1234; /* program scope */
double y = 1.234567; /* program scope */

void function_1()
{
    printf("From function_1:\n x=%d, y=%f\n", x, y);
}

main()
{
    int x = 4321; /* block scope 1*/

    function_1();
    printf("Within the main block:\n x=%d, y=%f\n", x, y);
    /* a nested block */
    {
    double y = 7.654321; /* block scope 2 */
    function_1();
    printf("Within the nested block:\n x=%d, y=%f\n", x, y);
    }
    return 0;
}

Using the static specifier

/* 1403.c: Using the static specifier */
#include <stdio.h>
/* the add_two function */
int add_two(int x, int y)
{
    static int counter = 1;

    printf("This is the function call of %d,\n", counter++);
    return (x + y);
}
/* the main function */
main()
{
    int i, j;

    for (i=0, j=5; i<5; i++, j--)
    printf("the addition of %d and %d is %d.\n\n",i, j, add_two(i, j));
    return 0;
}

Using gets() and puts()

/* 1304.c: Using gets() and puts() */
#include <stdio.h>

main()
{
    char str[80];
    int i, delt = 'a' - 'A';

    printf("Enter a string less than 80 characters:\n");
    gets( str );
    i = 0;
    while (str[i]){
    if ((str[i] >= 'a') && (str[i] <= 'z'))
    str[i] -= delt; /* convert to upper case */
    ++i;
    }
    printf("The entered string is (in uppercase):\n");
    puts( str );
    return 0;
}

Using scanf()

/* 1305.c: Using scanf() */
#include <stdio.h>

main()
{
    char str[80];
    int x, y;
    float z;

    printf("Enter two integers separated by a space:\n");
    scanf("%d %d", &x, &y);
    printf("Enter a floating-point number:\n");
    scanf("%f", &z);
    printf("Enter a string:\n");
    scanf("%s", str);
    printf("Here are what you've entered:\n");
    printf("%d %d\n%f\n%s\n", x, y, z, str);
    return 0;
}

Initializing strings

/* 1301.c: Initializing strings */
#include <stdio.h>

main()
{
    char str1[] = {'A', ' ',
    's', 't', 'r', 'i', 'n', 'g', ' ',
    'c', 'o', 'n', 's', 't', 'a', 'n', 't', '\0'};
    char str2[] = "Another string constant";
    char *ptr_str;
    int i;

    /* print out str2 */
    for (i=0; str1[i]; i++)
    printf("%c", str1[i]);
    printf("\n");
    /* print out str2 */
    for (i=0; str2[i]; i++)
    printf("%c", str2[i]);
    printf("\n");
    /* assign a string to a pointer */
    ptr_str = "Assign a string to a pointer.";
    for (i=0; *ptr_str; i++)
    printf("%c", *ptr_str++);
    return 0;
}

Measuring string length

/* 1302.c: Measuring string length */
#include <stdio.h>
#include <string.h>

main()
{
    char str1[] = {'A', ' ',
    's', 't', 'r', 'i', 'n', 'g', ' ',
    'c', 'o', 'n', 's', 't', 'a', 'n', 't', '\0'};
    char str2[] = "Another string constant";
    char *ptr_str = "Assign a string to a pointer.";

    printf("The length of str1 is: %d bytes\n", strlen(str1));
    printf("The length of str2 is: %d bytes\n", strlen(str2));
    printf("The length of the string assigned to ptr_str is: %d bytes\n",
    strlen(ptr_str));
    return 0;
}

Copying strings

/* 1303.c: Copying strings */
#include <stdio.h>
#include <string.h>

main()
{
    char str1[] = "Copy a string.";
    char str2[15];
    char str3[15];
    int i;

    /* with strcpy() */
    strcpy(str2, str1);
    /* without strcpy() */
    for (i=0; str1[i]; i++)
    str3[i] = str1[i];
    str3[i] = '\0';
    /* display str2 and str3 */
    printf("The content of str2: %s\n", str2);
    printf("The content of str3: %s\n", str3);
    return 0;
}

Stopping at the null character

/* 1205.c: Stopping at the null character */
#include <stdio.h>

main()
{
    char array_ch[15] = {'C', ' ' ,
    'i', 's', ' ',
    'p', 'o', 'w', 'e', 'r',
    'f', 'u', 'l', '!', '\0'};
    int i;
    /* array_ch[i] in logical test */
    for (i=0; array_ch[i] != '\0'; i++)
    printf("%c", array_ch[i]);
    return 0;
}

Printing out a 2-D array

/* 1206.c: Printing out a 2-D array */
#include <stdio.h>

main()
{
    int two_dim[3][5] = {1, 2, 3, 4, 5,
    10, 20, 30, 40, 50,
    100, 200, 300, 400, 500};
    int i, j;

    for (i=0; i<3; i++){
        printf("\n");
        for (j=0; j<5; j++)
        printf("%6d", two_dim[i][j]);
    }
    return 0;
}

Initializing unsized arrays

/* 1207.c: Initializing unsized arrays */
#include <stdio.h>

main()
{
    char array_ch[] = {'C', ' ' ,
    'i', 's', ' ' ,
    'p', 'o', 'w', 'e', 'r',
    'f', 'u', 'l', '!', '\0'};
    int list_int[][3] = {
    1, 1, 1,
    2, 2, 8,
    3, 9, 27,
    4, 16, 64,
    5, 25, 125,
    6, 36, 216,
    7, 49, 343};

    printf("The size of array_ch[] is %d bytes.\n", sizeof (array_ch));
    printf("The size of list_int[][3] is %d bytes.\n", sizeof (list_int));
    return 0;
}

Total bytes of an array

/* 1202.c: Total bytes of an array */
#include <stdio.h>

main()
{
    int total_byte;
    int list_int[10];

    total_byte = sizeof (int) * 10;
    printf( "The size of int is %d-byte long.\n", sizeof (int));
    printf( "The array of 10 ints has total %d bytes.\n", total_byte);
    printf( "The address of the first element: 0x%x\n", &list_int[0]);
    /* also first element address can be obtained by */
    printf( "The address of the first element: 0x%x\n", &list_int);
    printf( "The address of the last element: 0x%x\n", &list_int[9]);
    return 0;
}

Referencing an array with a pointer

/* 1203.c: Referencing an array with a pointer */
#include <stdio.h>

main()
{
    int *ptr_int;
    int list_int[10];
    int i;

    for (i=0; i<10; i++)
    list_int[i] = i + 1;
    ptr_int = list_int;
    printf( "The start address of the array: 0x%p\n", ptr_int);
    printf( "The value of the first element: %d\n", *ptr_int);
    ptr_int = &list_int[0];
    printf( "The address of the first element: 0x%p\n", ptr_int);
    printf( "The value of the first element: %d\n", *ptr_int);
    ptr_int = ptr_int + 1;
    printf( "The second address of the array: 0x%p\n", ptr_int);
    printf( "The value of the second element: %d\n", *ptr_int);

    return 0;
}

Printing out an array of characters

/* 1204.c: Printing out an array of characters */
#include <stdio.h>

main()
{
    char array_ch[7] = {'H', 'e', 'l', 'l', 'o', '!', '\0'};
    int i;

    for (i=0; i<7; i++)
    printf("array_ch[%d] contains: %c\n", i, array_ch[i]);
    /*--- method I ---*/
    printf( "Put all elements together(Method I):\n");
    for (i=0; i<7; i++)
    printf("%c", array_ch[i]);
    /*--- method II ---*/
    printf( "\nPut all elements together(Method II):\n");
    printf( "%s\n", array_ch);

    return 0;
}

Changing values via pointers

/* 1103.c: Changing values via pointers */
#include <stdio.h>

main()
{
    char c, *ptr_c;

    c = 'A';
    printf("c: address=0x%p, content=%c\n", &c, c);
    ptr_c = &c;
    printf("ptr_c: address=0x%p, content=0x%p\n", &ptr_c, ptr_c);
    printf("*ptr_c => %c\n", *ptr_c);
    *ptr_c = 'B';
    printf("ptr_c: address=0x%p, content=0x%p\n", &ptr_c, ptr_c);
    printf("*ptr_c => %c\n", *ptr_c);
    printf("c: address=0x%p, content=%c\n", &c, c);
    return 0;
}

Pointing to the same thing

/* 1104.c: Pointing to the same thing */
#include <stdio.h>

main()
{
    int x;
    int *ptr_1, *ptr_2, *ptr_3;

    x = 1234;
    printf("x: address=0x%p, content=%d\n", &x, x);
    ptr_1 = &x;
    printf("ptr_1: address=0x%p, content=0x%p\n", &ptr_1, ptr_1);
    printf("*ptr_1 => %d\n", *ptr_1);
    ptr_2 = &x;
    printf("ptr_2: address=0x%p, content=0x%p\n", &ptr_2, ptr_2);
    printf("*ptr_2 => %d\n", *ptr_2);
    ptr_3 = ptr_1;
    printf("ptr_3: address=0x%p, content=0x%p\n", &ptr_3, ptr_3);
    printf("*ptr_3 => %d\n", *ptr_3);
    return 0;
}

Initializing an array

/* 1201.c: Initializing an array */
#include <stdio.h>

main()
{
    int i;
    int list_int[10];

    for (i=0; i<10; i++){
    list_int[i] = i + 1;
    printf( "list_int[%d] is initialized with %d.\n", i, list_int[i]);
    }
    return 0;
}

Obtaining addresses

/* 1101.c: Obtaining addresses */
#include <stdio.h>

main()
{
    char c;
    int x;
    float y;

    printf("c: address=0x%p, content=%c\n", &c, c);
    printf("x: address=0x%p, content=%d\n", &x, x);
    printf("y: address=0x%p, content=%5.2f\n", &y, y);
    c = 'A';
    x = 7;
    y = 123.45;
    printf("c: address=0x%p, content=%c\n", &c, c);
    printf("x: address=0x%p, content=%d\n", &x, x);
    printf("y: address=0x%p, content=%5.2f\n", &y, y);
    return 0;
}

Declaring and assigning values to pointers

/* 1102.c: Declaring and assigning values to pointers */
#include <stdio.h>

main()
{
    char c, *ptr_c;
    int x, *ptr_x;
    float y, *ptr_y;

    c = 'A';
    x = 7;
    y = 123.45;
    printf("c: address=0x%p, content=%c\n", &c, c);
    printf("x: address=0x%p, content=%d\n", &x, x);
    printf("y: address=0x%p, content=%5.2f\n", &y, y);
    ptr_c = &c;
    printf("ptr_c: address=0x%p, content=0x%p\n", &ptr_c, ptr_c);
    printf("*ptr_c => %c\n", *ptr_c);
    ptr_x = &x;
    printf("ptr_x: address=0x%p, content=0x%p\n", &ptr_x, ptr_x);
    printf("*ptr_x => %d\n", *ptr_x);
    ptr_y = &y;
    printf("ptr_y: address=0x%p, content=0x%p\n", &ptr_y, ptr_y);
    printf("*ptr_y => %5.2f\n", *ptr_y);
    return 0;
}

Using the continue statement

Tuesday, October 14, 2014
/* 1007.c: Using the continue statement */
#include <stdio.h>

main()
{
    int i, sum;

    sum = 0;
    for (i=1; i<8; i++){
        if ((i==3) || (i==5))
        continue;
        sum += i;
        }
    printf("The sum of 1, 2, 4, 6, and 7 is: %d\n", sum);
    return 0;
}

Using the switch statement

/* 1004.c Using the switch statement */
#include <stdio.h>

main()
{
    int day;

    printf("Please enter a single digit for a day\n");
    printf("(within the range of 1 to 3):\n");
    day = getchar();
    switch (day){
        case '1':
        printf("Day 1\n");
        case '2':
        printf("Day 2\n");
        case '3':
        printf("Day 3\n");
        default: ;
        }
    return 0;
}

Adding the break statement

/* 1005.c Adding the break statement */
#include <stdio.h>

main()
{
    int day;

    printf("Please enter a single digit for a day\n");
    printf("(within the range of 1 to 7):\n");
    day = getchar();
    switch (day){
    case '1':
        printf("Day 1 is Sunday.\n");
        break;
    case '2':
        printf("Day 2 is Monday.\n");
        break;
    case '3':
        printf("Day 3 is Tuesday.\n");
        break;
    case '4':
        printf("Day 4 is Wednesday.\n");
        break;
    case '5':
        printf("Day 5 is Thursday.\n");
        break;
    case '6':
        printf("Day 6 is Friday.\n");
        break;
    case '7':
        printf("Day 7 is Saturday.\n");
        break;
    default:
        printf("The digit is not within the range of 1 to 7.\n");
        break;
    }
    return 0;
}

Breaking an infinite loop

/* 1006.c: Breaking an infinite loop */
#include <stdio.h>

main()
{
    int c;

    printf("Enter a character:\n(enter x to exit)\n");
    while (1) {
        c = getc(stdin);
        if (c == 'x')
        break;
        }
    printf("Break the infinite while loop. Bye!\n");
    return 0;
}

Using the if statement

/* 1001.c Using the if statement */
#include <stdio.h>

main()
{
    int i;

    printf("Integers that can be divided by both 2 and 3\n");
    printf("(within the range of 0 to 100):\n");
    for (i=0; i<=100; i++){
        if ((i%2 == 0) && (i%3 == 0))
        printf(" %d\n", i);
    }
    return 0;
}

Using the if-else statement

/* 1002.c Using the if-else statement */
#include <stdio.h>

main()
{
    int i;

    printf("Even Number Odd Number\n");
    for (i=0; i<10; i++)
        if (i%2 == 0)
            printf("%d", i);
        else
            printf("%14d\n", i);

    return 0;
}

Using nested if statements

/* 1003.c Using nested if statements */
#include <stdio.h>

main()
{
    int i;

    for (i=-5; i<=5; i++){
        if (i > 0)
            if (i%2 == 0)
                printf("%d is an even number.\n", i);
            else
                printf("%d is an odd number.\n", i);
        else if (i == 0)
            printf("The number is zero.\n");
        else
            printf("Negative number: %d\n", i);
        }
    return 0;
}

Using %hd, %ld, and %lu specifiers

/* 0903.c: Using %hd, %ld, and %lu specifiers */
#include <stdio.h>

main()
{
    short int x;
    unsigned int y;
    long int s;
    unsigned long int t;

    x = 0xFFFF;
    y = 0xFFFFU;
    s = 0xFFFFFFFFl;
    t = 0xFFFFFFFFL;
    printf("The short int of 0xFFFF is %hd.\n", x);
    printf("The unsigned int of 0xFFFF is %u.\n", y);
    printf("The long int of 0xFFFFFFFF is %ld.\n", s);
    printf("The unsigned long int of 0xFFFFFFFF is %lu.\n", t);
    return 0;
}

Using sin(), cos(), and tan() functions

/* 0904.c: Using sin(), cos(), and tan() functions */
#include <stdio.h>
#include <math.h>

main()
{
    double x;

    x = 45.0; /* 45 degree */
    x *= 3.141593 / 180.0; /* convert to radians */
    printf("The sine of 45 is: %f.\n", sin(x));
    printf("The cosine of 45 is: %f.\n", cos(x));
    printf("The tangent of 45 is: %f.\n", tan(x));
    return 0;
}

Using pow() and sqrt() functions

/* 0905.c: Using pow() and sqrt() functions */
#include <stdio.h>
#include <math.h>

main()
{
    double x, y, z;

    x = 64.0;
    y = 3.0;
    z = 0.5;
    printf("pow(64.0, 3.0) returns: %7.0f\n", pow(x, y));
    printf("sqrt(64.0) returns: %2.0f\n", sqrt(x));
    printf("pow(64.0, 0.5) returns: %2.0f\n", pow(x, z));
    return 0;
}

Using the ?: operator

/* 0807.c: Using the ?: operator */
#include <stdio.h>

main()
{
    int x;

    x = sizeof(int);
    printf("%s\n",
        (x == 2) ? "The int data type has 2 bytes." : "int doesn't have 2 bytes.");
    printf("The maximum value of int is: %d\n",
        (x != 2) ? ~(1 << x * 8 - 1) : ~(1 << 15) );
    return 0;
}

Using signed and unsigned modifiers

/* 0901.c: Using signed and unsigned modifiers */
#include <stdio.h>

main()
{
    signed char ch;
    int x;
    unsigned int y;

    ch = 0xFF;
    x = 0xFFFF;
    y = 0xFFFFu;
    printf("The decimal of signed 0xFF is %d.\n", ch);
    printf("The decimal of signed 0xFFFF is %d.\n", x);
    printf("The decimal of unsigned 0xFFFFu is %u.\n", y);
    printf("The hex of decimal 12345 is 0x%X.\n", 12345);
    printf("The hex of decimal -12345 is 0x%X.\n", -12345);
    return 0;
}

Using short and long modifiers

/* 0902.c: Using short and long modifiers */
#include <stdio.h>

main()
{
    printf("The size of short int is: %d.\n",
    sizeof(short int));
    printf("The size of long int is: %d.\n",
    sizeof(long int));
    printf("The size of float is: %d.\n",
    sizeof(float));
    printf("The size of double is: %d.\n",
    sizeof(double));
    printf("The size of long double is: %d.\n",
    sizeof(long double));
    return 0;
}

Using shift operators

/* 0806.c: Using shift operators */
/*
The operation of the shift-right operator (>>) is equivalent to dividing by powers of 2. In other words,
the following:
x >> y
x / 2y
Here x is a non-negative integer.
On the other hand, shifting to the left is equivalent to multiplying by powers of 2; that is,
x << y
x * 2y
*/

#include <stdio.h>

main()
{
    int x, y, z;

    x = 255;
    y = 5;
    printf("Given x = %4d, i.e., 0X%04X\n", x, x);
    printf(" y = %4d, i.e., 0X%04X\n", y, y);
    z = x >> y;
    printf("x >> y returns: %6d, i.e., 0X%04X\n", z, z);
    z = x << y;
    printf("x << y returns: %6d, i.e., 0X%04X\n", z, z);
    return 0;
}

Using the logical OR operator

/* 0803.c: Using the logical OR operator */
#include <stdio.h>

main()
{
    int num;

    printf("Enter a single digit that can be divided\nby both 2 and 3:\n");
    for (num = 1; (num%2 != 0) || (num%3 != 0); )
        num = getchar() - 48;
        printf("You got such a number: %d\n", num);
    return 0;
}

Using the logical negation operator

/* 0804.c: Using the logical negation operator */
#include <stdio.h>

main()
{
    int num;

    num = 7;
    printf("Given num = 7\n");
    printf("!(num < 7) returns: %d\n", !(num < 7));
    printf("!(num > 7) returns: %d\n", !(num > 7));
    printf("!(num == 7) returns: %d\n", !(num == 7));
    return 0;
}

Using bitwise operators

/* 0805.c: Using bitwise operators */
#include <stdio.h>

main()
{
    int x, y, z;

    x = 4321;
    y = 5678;
    printf("Given x = %u, i.e., 0X%04X\n", x, x);
    printf(" y = %u, i.e., 0X%04X\n", y, y);
    z = x & y;
    printf("x & y returns: %6u, i.e., 0X%04X\n", z, z);
    z = x | y;
    printf("x | y returns: %6u, i.e., 0X%04X\n", z, z);
    z = x ^ y;
    printf("x ^ y returns: %6u, i.e., 0X%04X\n", z, z);
    printf(" ~x returns: %6u, i.e., 0X%04X\n", ~x, ~x);
    return 0;
}

Using the sizeof operator

/* 0801.c: Using the sizeof operator */
#include <stdio.h>

main()
{
    char ch = ' ';
    int int_num = 0;
    float flt_num = 0.0f;
    double dbl_num = 0.0;

    printf("The size of char is: %d-byte\n", sizeof(char));
    printf("The size of ch is: %d-byte\n", sizeof ch );
    printf("The size of int is: %d-byte\n", sizeof(int));
    printf("The size of int_num is: %d-byte\n", sizeof int_num);
    printf("The size of float is: %d-byte\n", sizeof(float));
    printf("The size of flt_num is: %d-byte\n", sizeof flt_num);
    printf("The size of double is: %d-byte\n", sizeof(double));
    printf("The size of dbl_num is: %d-byte\n", sizeof dbl_num);
    return 0;
}

Using the logical AND operator

/* 0802.c: Using the logical AND operator */
#include <stdio.h>

main()
{
    int num;

    num = 0;
    printf("The AND operator returns: %d\n",
    (num%2 == 0) && (num%3 == 0));
    num = 2;
    printf("The AND operator returns: %d\n",
    (num%2 == 0) && (num%3 == 0));
    num = 3;
    printf("The AND operator returns: %d\n",
    (num%2 == 0) && (num%3 == 0));
    num = 6;
    printf("The AND operator returns: %d\n",
    (num%2 == 0) && (num%3 == 0));

return 0;
}

Using a do-while loop

/* 0706.c: Using a do-while loop */
#include <stdio.h>

main()
{
    int i;

    i = 65;
    do {
        printf("The numeric value of %c is %d.\n", i, i);
        i++;
    }while (i<72);
    return 0;
}

Demonstrating nested loops

/* 0707.c: Demonstrating nested loops */
#include <stdio.h>

main()
{
    int i, j;

    for (i=1; i<=3; i++) { /* outer loop */
        printf("The start of iteration %d of the outer loop.\n", i);
        for (j=1; j<=4; j++) /* inner loop */
            printf(" Iteration %d of the inner loop.\n", j);
        printf("The end of iteration %d of the outer loop.\n", i);
        }
    return 0;
}

Multiple expressions

/* 0702.c: Multiple expressions */
#include <stdio.h>

main()
{
    int i, j;

    for (i=0, j=8; i<8; i++, j--)
        printf("%d + %d = %d\n", i, j, i+j);
    return 0;
}

Conditional loop

/* 0704.c: Conditional loop */
#include <stdio.h>

main()
{
    int c;

    printf("Enter a character:\n(enter x to exit)\n");
    for ( c=' '; c != 'x'; ) {
        c = getc(stdin);
        putchar(c);
    }
    printf("\nOut of the for loop. Bye!\n");
    return 0;
}

Using a while loop

/* 0705.c: Using a while loop */
#include <stdio.h>

main()
{
    int c;

    c = ' ';
    printf("Enter a character:\n(enter x to exit)\n");
    while (c != 'x') {
    c = getc(stdin);
    putchar(c);
    }
    printf("\nOut of the while loop. Bye!\n");
    return 0;
}

Using relational operators

/* 0603.c: Using relational operators */
#include <stdio.h>

main()
{
    int x, y;
    double z;

    x = 7;
    y = 25;
    z = 24.46;
    printf("Given x = %d, y = %d, and z = %.2f,\n", x, y, z);
    printf("x >= y produces: %d\n", x >= y);
    printf("x == y produces: %d\n", x == y);
    printf("x < z produces: %d\n", x < z);
    printf("y > z produces: %d\n", y > z);
    printf("x != y - 18 produces: %d\n", x != y - 18);
    printf("x + y != z produces: %d\n", x + y != z);
    return 0;
}

Using the cast operator

/* 0604.c: Using the cast operator */
#include <stdio.h>

main()
{
    int x, y;

    x = 7;
    y = 5;
    printf("Given x = %d, y = %d\n", x, y);
    printf("x / y produces: %d\n", x / y);
    printf("(float)x / y produces: %f\n", (float)x / y);
    return 0;
}

Converting 0 through 15 to hex numbers

/* 0701.c: Converting 0 through 15 to hex numbers */
#include <stdio.h>

main()
{
    int i;

    printf("Hex(uppercase) Hex(lowercase) Decimal\n");
    for (i=0; i<16; i++){
        printf("%X %x %d\n", i, i, i);
    }
    return 0;
}

Using arithmetic assignment operators

/* 0601.c: Using arithmetic assignment operators */
#include <stdio.h>

main()
{
    int x, y, z;

    x = 1; /* initialize x */
    y = 3; /* initialize y */
    z = 10; /* initialize z */
    printf("Given x = %d, y = %d, and z = %d,\n", x, y, z);

    x = x + y;
    printf("x = x + y assigns %d to x;\n", x);

    x = 1; /* reset x */
    x += y;
    printf("x += y assigns %d to x;\n", x);

    x = 1; /* reset x */
    z = z * x + y;
    printf("z = z * x + y assigns %d to z;\n", z);

    z = 10; /* reset z */
    z = z * (x + y);
    printf("z = z * (x + y) assigns %d to z;\n", z);

    z = 10; /* reset z */
    z *= x + y;
    printf("z *= x + y assigns %d to z.\n", z);

    return 0;
}

pre- or post-increment(decrement) operators

/* 0602.c: pre- or post-increment(decrement) operators */
#include <stdio.h>

main()
{
    int w, x, y, z, result;

    w = x = y = z = 1; /* initialize x and y */
    printf("Given w = %d, x = %d, y = %d, and z = %d,\n", w, x, y, z);

    result = ++w;
    printf("++w gives: %d\n", result);
    result = x++;
    printf("x++ gives: %d\n", result);
    result = --y;
    printf("--y gives: %d\n", result);
    result = z--;
    printf("z-- gives: %d\n", result);
    return 0;
}

Aligning output

/* 0507.c: Aligning output */
#include <stdio.h>

main()
{
    int num1, num2, num3, num4, num5;

    num1 = 1;
    num2 = 12;
    num3 = 123;
    num4 = 1234;
    num5 = 12345;
    printf("%8d %-8d\n", num1, num1);
    printf("%8d %-8d\n", num2, num2);
    printf("%8d %-8d\n", num3, num3);
    printf("%8d %-8d\n", num4, num4);
    printf("%8d %-8d\n", num5, num5);
    return 0;
}

Using precision specifiers

/* 0508.c: Using precision specifiers */
#include <stdio.h>

main()
{
    int int_num;
    double flt_num;

    int_num = 123;
    flt_num = 123.456789;
    printf("Default integer format: %d\n", int_num);
    printf("With precision specifier: %2.8d\n", int_num);
    printf("Default float format: %f\n", flt_num);
    printf("With precision specifier: %-10.2f\n", flt_num);
    return 0;
}

Converting to hex numbers

/* 0505 Converting to hex numbers */
#include <stdio.h>

main()
{
    printf("Hex(uppercase) Hex(lowercase) Decimal\n");
    printf("%X %x %d\n", 0, 0, 0);
    printf("%X %x %d\n", 1, 1, 1);
    printf("%X %x %d\n", 2, 2, 2);
    printf("%X %x %d\n", 3, 3, 3);
    printf("%X %x %d\n", 4, 4, 4);
    printf("%X %x %d\n", 5, 5, 5);
    printf("%X %x %d\n", 6, 6, 6);
    printf("%X %x %d\n", 7, 7, 7);
    printf("%X %x %d\n", 8, 8, 8);
    printf("%X %x %d\n", 9, 9, 9);
    printf("%X %x %d\n", 10, 10, 10);
    printf("%X %x %d\n", 11, 11, 11);
    printf("%X %x %d\n", 12, 12, 12);
    printf("%X %x %d\n", 13, 13, 13);
    printf("%X %x %d\n", 14, 14, 14);
    printf("%X %x %d\n", 15, 15, 15);
    return 0;
}

Specifying minimum field width

/* 0506 Specifying minimum field width */
#include <stdio.h>

main()
{
int num1, num2;

num1 = 12;
num2 = 12345;
printf("%d\n", num1);
printf("%d\n", num2);
printf("%5d\n", num1);
printf("%05d\n", num1);
printf("%2d\n", num2);
return 0;
}

Outputting a character with putc()

/* 0503 Outputting a character with putc() 0503 */
#include <stdio.h>

main()
{
    int ch;

    ch = 65; /* the numeric value of A */
    printf("The character that has numeric value of 65 is:\n");
    putc(ch, stdout);
    return 0;
}

Reading input by calling getchar()

/* 0502.c Reading input by calling getchar() */
#include <stdio.h>

main()
{
    int ch1, ch2;

    printf("Please type in two characters together:\n");
    ch1 = getc( stdin );
    ch2 = getchar( );
    printf("The first character you just entered is: %c\n", ch1);
    printf("The second character you just entered is: %c\n", ch2);
    return 0;
}

Reading input by calling getc()

/* Reading input by calling getc() 0501 */
#include <stdio.h>

main()
{
    int ch;

    printf("Please type in one character:\n");
    ch = getc( stdin );
    printf("The character you just entered is: %c\n", ch);
    printf("The character you just entered is: %d\n", ch);
    return 0;
}

format specifiers that can be used in printf()

  • The following are all the format specifiers that can be used in printf():
    • %c The character format specifier.
    • %d The integer format specifier.
    • %i The integer format specifier (same as %d).
    • %f The floating-point format specifier.
    • %e The scientific notation format specifier (note the lowercase e).
    • %E The scientific notation format specifier (note the uppercase E).
    • %g Uses %f or %e, whichever result is shorter.
    • %G Uses %f or %E, whichever result is shorter.
    • %o The unsigned octal format specifier.
    • %s The string format specifier.
    • %u The unsigned integer format specifier.
    • %x The unsigned hexadecimal format specifier (note the lowercase x).
    • %X The unsigned hexadecimal format specifier (note the uppercase X).
    • %p Displays the corresponding argument that is a pointer.
    • %n Records the number of characters written so far.
    • %% Outputs a percent sign (%).

Integer vs floating-point divisions

/* Integer vs floating-point divisions 0404 */

#include <stdio.h>
main()
{
    int int_num1, int_num2, int_num3; /* Declare integer variables */
    float flt_num1, flt_num2, flt_num3; /* Declare floating-point variables */

    int_num1 = 32 / 10; /* Both divisor and dividend are integers */
    flt_num1 = 32 / 10;
    int_num2 = 32.0 / 10; /* The divisor is an integer */
    flt_num2 = 32.0 / 10;
    int_num3 = 32 / 10.0; /* The dividend is an integer */
    flt_num3 = 32 / 10.0;

    printf("The integer divis. of 32/10 is: %d\n", int_num1);
    printf("The floating-point divis. of 32/10 is: %f\n", flt_num1);
    printf("The integer divis. of 32.0/10 is: %d\n", int_num2);
    printf("The floating-point divis. of 32.0/10 is: %f\n", flt_num2);
    printf("The integer divis. of 32/10.0 is: %d\n", int_num3);
    printf("The floating-point divis. of 32/10.0 is: %f\n", flt_num3);
    return 0;
}

Converting numeric values back to characters

/* Converting numeric values back to characters 0402 */

#include <stdio.h>
main()
{
    char c1;
    char c2;

    c1 = 65;
    c2 = 97;

    printf("The character that has the numeric value of 65 is: %c.\n", c1);
    printf("The character that has the numeric value of 97 is: %c.\n", c2);

    return 0;
}

Showing the numeric values of characters

/* Showing the numeric values of characters 0403 */

#include <stdio.h>
main()
{
    char c1;
    char c2;

    c1 = 'A';
    c2 = 'a';
    printf("Convert the value of c1 to character: %c.\n", c1);
    printf("Convert the value of c2 to character: %c.\n", c2);

    printf("The numeric value of A is: %d.\n", c1);
    printf("The numeric value of A is: %d.\n", c2);

    return 0;
}

Printing out characters and ASCII

/* Printing out characters and ASCII 0401 */

#include <stdio.h>
main()
{
    char c1;
    char c2;

    c1 = 'A';
    c2 = 'a';
    printf("Convert the value of c1 to character: %c.\n", c1);
    printf("Convert the value of c2 to character: %c.\n", c2);

    printf("The numeric value of A is: %d.\n", c1);
    printf("The numeric value of A is: %d.\n", c2);

    return 0;
}

Calculate an addition and print out the result

/* Calculate an addition and print out the result  0301 */

#include<stdio.h>
int integer_add( int x, int y )
{
    int result;
    result = x + y;
    return result;
}

void main()
{
    int sum;
    sum = integer_add(5,6);
    printf("The addition of 5 and 6 is %d.\n",sum);
}

C Programming Language

Monday, October 13, 2014
  • C language was first developed in 1972 by Dennis Ritchie at AT&T Bell Labs.
  • C is a high-level programming language.
  • The most important features of C language are:
    • Readability: Programs are easy to read.
    • Maintainability: Programs are easy to maintain.
    • Portability: Programs are easy to port across different computer platforms.
  • Other features are:
    • Modularity: Large programs breakdown into smaller modules.
    • Case Sensitivity: Differentiates upper and lower case letters.
    • Flexibility: Reserved words gives control to the programmer.
    • Compactness: Precise use of reserved words.
    • Extensibility: Programs can add new features at any time.

    Outlands

    The Outlands is a journey outside the boundaries. To know the basics and to create the new race of programs. Welcome aboard!!!
    Copyright @ 2015 Tron!

    Labels

    Blog Archive