Can we output a picture in C?

13,841

Solution 1

You can generate a web page from a C program by using the Common Gateway Interface (CGI). The C program is compiled and runs on the server, this is different from Javascript which runs on the browser.

You can also generate images via CGI too. Just set the content type appropriately, eg. image/jpeg for a JPEG image. You can use libjpeg to generate the actual image.


Here is an example C program to generate a page with a JPEG image:

#include <stdio.h>

main()
{
   char *pageTitle = "Look, a JPEG!";
   char *urlImage  = "/myimage.jpeg";

// Send HTTP header.
   printf("Content-type: text/html\r\n\r\n");

// Send the generated HTML.
   printf("<html><head><title>%s</title></head>\r\n"
          "<body>\r\n"
          "<h1>%s</h1>\r\n"
          "<img src=\"%s\">\r\n"
          "</body></html>\r\n",
          pageTitle, pageTitle, urlImage);
}

Solution 2

That's a lot of questions!

You can generate JPEG and PNG files from C using libjpeg and libpng, respectively. (Yes, I'm dodging your GIF question on purpose. You don't need to do that; PNG is well-supported by mainstream browsers now and should be preferred. :-P)

Browsers generally only support JavaScript for client-side scripting. Using anything else will only destroy the portability of your webpages. Yes, like you say you can use plugins, but some people are averse to installing plugins, and others are behind corporate policies that don't allow such things.

Solution 3

  • To save jpeg files, you will need libjpeg. I am sure that there is a similar library for GIFs too.
  • C is not a scripting language. You cannot write scripts in C like in Javascript. You can write server-side code in with CGI if you dare but this is really not a good idea. There are a couple of better tools to write server side components, including C#/ASP.NET, Java, Python, Perl, Ruby (with Rails) or PHP. Even Coldfusion is better for webpages than C.
  • C is not an interpreted language. There is no browser that going to run your C code and I doubt there ever will be one, for millions of reasons. Also, there is no plugin for that. This is something really unfeasable. C is a great language for a lot of stuff, but the web is one of the areas where it's rarely used (except if you want to write a browser or a http server in C. But even for those tasks, C++ is usually better.) If you want client-side scripting in webpages, use Javascript. Some browsers will interpret VBScript too, but Javascript is the preferred browser scripting language.
Share:
13,841
Manoj Doubts
Author by

Manoj Doubts

Click here for my usefull books and links and games collection Right now trying to develop a simple language

Updated on June 04, 2022

Comments

  • Manoj Doubts
    Manoj Doubts almost 2 years

    can we output a .jpg image or .gif image in C?

    I mean can we print a picture as output with the help of a C program?

    Aslo can we write a script in C language for HTML pages as can be written in JavaScript?

    Can the browsers operate on it?

    If not possible is there any plugin for any of the browsers?

    Any example code or links please?

  • Manoj Doubts
    Manoj Doubts over 15 years
    thank u.I was just curious to know whether ther is any provision in c to do that? then how to do that? i need some example code. please find me one.once again thank u for ur response?