Posts Tagged ‘Turbo C’

How to use scanf to read Strings with Spaces ?

June 10, 2009

I knew this for two years now, but it dint strike me before to put it up in the blog; don’t know why ?

Its easy and simple, I don’t think I need to explain. Only the syntax needs to be remembered.

scanf(” %[^\n]s”,a);

May be you get a question in your mind. What does ‘[^\n]‘ do ? Does it set the delimiter to ‘\n’. Well, you on the right track. Yes, it does. Instead on ‘\n’, if you had put a ‘\t’ it would consider all words with spaces as string until you press a ‘Tab’. You can even have a  ‘  ‘(a space). The scanf can be expanded as

scanf(” %[^ ]s”,a);  // Note – There is a space after ^

The only differece between scanf(” %s”,a) and scanf(” %[^ ]s”,a) is that, when you enter a string with two words, the former considers each word as a new string whereas the latter consider only the first word as a string and the other word is ignored.

As a Example, consider the string “Hello World“, the former reads “Hello” and “World” as two strings (if you had called ‘scanf’ twice) and the latter reads only the first word “Hello” (even if you had called ‘scanf’ twice) ! Go ahead and experiment with other delimiters !! :D

How to create header files in C/C++ ?

February 25, 2009

First of all, does creating a header file useful at all ? If yes how ?

It obviously is or else we wouldn’t have had soemthing called a ‘header files’.

Next, can we create our own header files ?

Yes, you can. It is pretty simple and its going to make your program simple and customizable.

Let us start with creating a header file in the traditional TurboC. Define the contents of the header file (meaning the functions you would like to include in the file) and then save it as a ‘.h’ file in the ‘include’ directory.

The path could be C:/TC/INCLUDE if you have installed Turbo C directly in ‘drive C’.

Your are done !! :) Include the header file in the program by just including the file like any other file.

#include “headerfilename.h” or #include<headerfilename.h>

Creating one in GCC is a bit more difficult. Define the file in the same way as stated above and save it in a ‘directory where you are going to save the program’ (NOTE: This is important. Both the header file and the program must be in the same directory, if not the program will not be able to detect your header file ). Header File successfully created ! :) But, unlike Turbo C the header file cannot be included by

#include<headerfilename.h>

The only way to include the header file is to treat the filename in the same way u treat a string.

#include “headerfilename.h”

Now that you have created your header files, you can create the function like sort, factorial etc. and store them in the header file. When you intend to use them, include the header file in the program and just call the function you stored in the header file.

Hasn’t it made life simple ? :D

Follow

Get every new post delivered to your Inbox.

Join 1,545 other followers

%d bloggers like this: