Skip to main content

Posts

Showing posts from March, 2013

Sample C Program to Print any Print Statement without using Semicolon

# include <stdio.h>

void main()
{

    if(printf("Hi.. Welcome to sanfoundry"))
    {
    }

}


OUTPUT:

Hi.. Welcome to sanfoundry



Sample C Program To Display Its Own Source Code As Its Output.

# include <stdio.h>

int main()
{

    FILE *fp;
    char ch;
    fp = fopen(__FILE__,"r");

    do
    {
        ch = getc(fp);
        putchar(ch);
     }

     while (ch != EOF);
     fclose(fp);
     return 0;

}


OUTPUT:


#include

int main()
{
    FILE *fp;
    char ch;

    fp = fopen(__FILE__,"r");
    do
    {
        ch = getc(fp);
        putchar(ch);
     }
     while (ch != EOF);
     fclose(fp);
     return 0;
}

Sample C Program To Display Function Without Using The Main Function.

# include <stdio.h>
#define decode(s,t,u,m,p,e,d) m##s##u##t
#define begin decode(a,n,i,m,a,t,e)

int begin()
{
    printf(" helloworld ");
}

OUTPUT:

helloworld




Sample C Program To Get IP Address.

# include <stdio.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/ioctl.h>
#include <netinet/in.h>
#include <net/if.h>
#include <unistd.h>
#include

int main()
{

    int n;
    struct ifreq ifr;
    char array[] = "eth0";
    n = socket(AF_INET, SOCK_DGRAM, 0);

//Type of address to retrieve - IPv4 IP address

    ifr.ifr_addr.sa_family = AF_INET;

  //Copy the interface name in the ifreq structure

    strncpy(ifr.ifr_name , array , IFNAMSIZ - 1);

    ioctl(n, SIOCGIFADDR, &ifr);
    close(n);

    //display result
    printf("IP Address is %s - %s\n" , array , inet_ntoa(( (struct sockaddr_in *)&ifr.ifr_addr )->sin_addr) );

    return 0;
}


OUTPUT:

IP Address is eth0 - 192.8.5.1

Sample C Program To Illustrate The Concept Of Unions.

# include <stdio.h>

void main()
{

    union number
   {
        int  n1;
        float n2;
    };
    union number x;

    printf("Enter the value of n1: ");
    scanf("%d", &x.n1);

    printf("Value of n1 = %d", x.n1);
    printf("\nEnter the value of n2: ");

    scanf("%f", &x.n2);
    printf("Value of n2 = %f\n", x.n2);
}

OUTPUT:

Enter the value of n1: 10
Value of n1 = 10
Enter the value of n2: 50
Value of n2 = 50.000000



Sample C Program To Find The Size Of A Union.


# include <stdio.h>

void main()
{

    union sample
   {
        int   m;
        float n;
        char  ch;
    };
    union sample u;
    printf("The size of union = %d\n", sizeof(u));
    /*  initialization */
    u.m = 25;
    printf("%d %f %c\n", u.m, u.n, u.ch);
    u.n = 0.2;
    printf("%d %f %c\n", u.m, u.n, u.ch);
    u.ch = 'p';
    printf("%d %f %c\n", u.m, u.n, u.ch);
}


OUTPUT:

The size of union = 4
25 0.000000
10452…

Sample C Program to convert given number of days to a measure of time given in years, weeks and days.

For example 375 days is equal to 1 year 1 week and 3 days (ignore leap year).

# include <stdio.h>
#define DAYSINWEEK 7

void main()

{
    int ndays, year, week, days;
    printf("Enter the number of daysn");
    scanf("%d", &ndays);
    year = ndays / 365;
    week =(ndays % 365) / DAYSINWEEK;
    days =(ndays % 365) % DAYSINWEEK;
    printf ("%d is equivalent to %d years, %d weeks and %d days", ndays, year, week, days);
}


Output:

Enter the number of days
29
29 is equivalent to 0 years, 4 weeks and 1 days

Enter the number of days
1000
1000 is equivalent to 2 years, 38 weeks and 4 days

Sample C Program to Print a Semicolon without using a Semicolon anywhere in the code.

# include <stdio.h>

int main(void)
{
// 59 is the ascii value of semicolumn
    if (printf("%c ", 59))
    {
    }
    return 0;
}


Output:
;



Sample C program to accept an integer and reverse it.

# include <stdio.h>

void main()
{
    long  num, reverse = 0, temp, remainder;
    printf("Enter the number\n");
    scanf("%ld", &num);
    temp = num;
    while (num > 0)
    {
        remainder = num % 10;
        reverse = reverse * 10 + remainder;
        num /= 10;
    }
    printf("Given number = %ld\n", temp);
    printf("Its reverse is = %ld\n", reverse);
}


Output:

Enter the number
567865
Given number   = 567865
Its reverse is = 568765