hello. this is my first post in here :)
im new to programming and im learning C as my first language.

I need help on this problem, i am making a running clock and it prints out in this format:

1:1:11

but i would like to make it look like in this format:

01:01:11

here is my code so far, i am using Turbo C :P

i really want to google this problem, but i don't know what to search for. :D

#include <stdio.h>
main()
{
	int sec, min, hour;
	clrscr();

	for (hour=0; hour<=24; hour++)
	{

		for (min=0; min<=60; min++)
		{
			for (sec=0; sec<=60; sec++)
			{
			printf("%d:%d:%d", hour, min, sec);
			delay(100000000);
			clrscr();
			}
		}
	}


getch();

Recommended Answers

All 11 Replies

%d -> %02d

#include <stdio.h>

int main(void)
{
   int h = 1, m = 1, s = 1;
   printf("%02d:%02d:%02d", h, m, s);
   return 0;
}

/* my output
01:01:01
*/

that works! thanks Dave. :)

i have a question, what do you call the process of gathering a data from a web page or website, then those gathered data will be used in your program.

for example i want to get the time from a website, then my program will work base on that website's time.

I wanna learn about it but i don't know what its called, so i really can't google it :p is that possible on C?

Most people call it HTML Scraping. The programs are called HTML Scrapers. Easy to do with Python, don't know about C.

im not sure if this is called html scraping. here is i wanna do. i hope that this is possible in C.

im not really asking for the code, but i hope someone can point me to the right direction, what programming language is this best to perform, what topic is this? what process is this called (connecting my program to mozilla).

this program will ask me for a keyword that i wanna search for, let's say "Dogs". then after entering the keyword, it will automatically open a browser, let say the Mozilla Firefox, it will open several tabs, one is google, the other one is yahoo, and any web search. then it will input the keyword "Dogs" on those web search's search text box. then it will press search.

it should work like this way:

1. Enter Keyword to search (dogs)
2. Open Mozilla Browser
3. Open Multiple tabs
4. Go to URLs of Google, Yahoo, WebSearch
5. Input the keyword (dogs) to those websearch's search text box
6. then press the "search button"
7. those Google, yahoo will then search for "dogs" at the same time.

In Python it would be very simple, the url string for the search being the most complicated part ...

# search google with the default web browser (new tab)
# tested with Python 3.1.1

import webbrowser

search = 'dog'

url = "http://www.google.com/search?hl=en&source=hp&q=%s\
&btnG=Google+Search" % search

webbrowser.open_new_tab(url)

Okay, I got the message, I will post more stuff in the every so lively C forum!!!!

commented: Actually this is relevant as the user asked what language might be best to achieve this and that python snippet looks short and sweet. +24

Actually, your Python program works like a charm!

%d -> %02d

#include <stdio.h>

int main(void)
{
   int h = 1, m = 1, s = 1;
   printf("%02d:%02d:%02d", h, m, s);
   return 0;
}

/* my output
01:01:01
*/

Dave,

I didnt quite get where exactly the clock is running. If this program is executed, then I expect the result always to be /* 01:01:01 */, could you also explain how to run the clock? How to acquire the present time is what I would want to know.

In Python it would be very simple, the url string for the search being the most complicated part ...

# search google with the default web browser (new tab)
# tested with Python 3.1.1

import webbrowser

search = 'dog'

url = "http://www.google.com/search?hl=en&source=hp&q=%s\
&btnG=Google+Search" % search

webbrowser.open_new_tab(url)

Bro, could you give the C variation of this one?

Dave,

I didnt quite get where exactly the clock is running. If this program is executed, then I expect the result always to be /* 01:01:01 */, could you also explain how to run the clock? How to acquire the present time is what I would want to know.

he just showed me how to put zeros on single digits.

i think you can use time.h for getting the system clock, not sure though.

I didnt quite get where exactly the clock is running. If this program is executed, then I expect the result always to be /* 01:01:01 */, could you also explain how to run the clock?

I don't imagine you'd find anything about a running clock in the code I posted. I wasn't answering that, I was answering the question about the formatting.

How to acquire the present time is what I would want to know.

time() I'd post a snippet, but with the latest site search, I have no idea how to find the link.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.