ss_blog_claim=726e70d7c87c20ae33aa7a61f06eb8aa

Friday, November 28, 2008

The usage of ‘gets’ statement in C Programming

Well we have already discussed about the usage of ‘fflush’ statement, let us now discuss about the usage of ‘gets’ statement. String is an array of characters and we use the format specifier ‘%s’ to directly get these array of characters, which is not possible in the case of array of integers. In the case of array of integers we use the ‘for’ loop to get the values since we don't have any format specifier for array of integers.

To get array of characters we use the format specifier ‘%s’ as shown below

puts(“Enter the string”);
scanf(“%s”,ex_str);
puts(ex_str);

Output:

Enter a string
sample text
sample

What is wrong in the output?? We know that ‘Space’ and ‘Enter’ key act as a delimiter. So if you execute this program and give input as ‘sample text’ the value stored in ‘ex_str’ is ‘sample’ and not ‘sample text’ this is because the space in between the words ‘sample’ and ‘text’ acts as a delimiter thus storing only ‘sample’ in the character array (string). To overcome this problem we use the ‘gets’ statement. The ‘gets’ statement takes only the ‘Enter’ key as delimiter. The below part of the code shows how to use ‘gets’ statement.

puts(“Enter a string”);
gets(ex_str);
puts(ex_str);

Output:

Enter a string
sample text

sample text

Just think of getting a name of a person which contains first name, last name and middle name so this ‘gets’ statement is much useful in those situations. Happy Programming!!

Saturday, November 15, 2008

The usage of ‘fflush’ statement in C programming

The keyboard has an input buffer in it and every time you read data from the keyboard the value stored in the input buffer is read. For every key press there will be a value loaded into the input buffer which means that the ‘Enter’ key, ‘Tab’ key also has a value being stored in the buffer when pressed.


We know that the ‘Enter’ key and the ‘Tab’ key acts as a delimiter while accepting data. Consider the below C Program

void main()
{
int num;
char ch;
clrscr();
printf(“Enter a number\n”);
scanf(“%d”,&num);
printf(“Enter a character\n”);
scanf(“%c”,&ch);
printf(“The value of num is %d and ch is %c”,num,ch);
getch();
}

The output of the program is shown below

Enter a number
12
Enter a character
The value of num is 12 and ch is

Wonder why this happens? Well let us look at the problem. When you type the number 12 and press ‘Enter’ key, the value corresponding to key press ‘1’ and ‘2’ is read from the input buffer and gets assigned to the variable ‘num’ once you press the ‘Enter’ key(delimiter). Now the value of the ‘Enter’ key is stored in the input buffer. So when you want to read a character, the value of the ‘Enter’ key which is stored in the input buffer is assigned to the character variable ‘ch’. This results in giving a wrong output. This is where the ‘fflush’ command comes into picture. The ‘fflush’ clears the specified buffer, so after getting the integer value we need to clear the input buffer (which stores the ‘Enter’ key value) before getting the character value. So the C program is changed as shown below.

printf(“Enter a number\n”);
scanf(“%d”,&num);
fflush(stdin);
printf(“Enter a character\n”);
scanf(“%c”,&ch);

Hope you know understood the importance of ‘ffush’ statement

Monday, November 10, 2008

Types of ROM's

We have been hearing the terms volatile memory and non-volatile memory from our 1st year of college. Here is a brush up of what we have heard so far.
The OTP (one time programmable) are used for firmware applications.
The ROM is also known as the program memory or code memory and RAM is also known as the data memory.
The EEPROM are electrically erasable PROM and needs electrical supply to clear the data.
The UVPROM requires UV light to pass through the window to clear the data.
The FLASH ROM are the advanced ones where data is loaded and retrieved in blocks, where each block is greater than 2 bytes, so the operation is faster in the case of FLASH ROM, whereas in the case of EEPROM data is loaded and retrieved byte by byte so it would take much time to access data when compared to FLASH ROM.

Saturday, November 1, 2008

Hectic week approaching...

The rain was troubling me much last month, because of it I had to delay every jobs of mine. I even failed to attend two of my classes at AU PERS center. Those two classes covered array of pointers and structures, It was bad that I missed the classes on array of pointers which was an interesting and tough topic. However I hope I could get my doubts cleared on it. The upcoming week I have one week practicals and I have to complete twenty programs within this week and get it signed from the lab incharge. The twenty programs seems to be pretty tough and time consuming I tried to complete only three and it took me two hours and I spent most of my brain work there, now trying to get some relaxation. Hope that I could complete the programs on time and I also have to prepare for the test after this week on C module. I hope that this week is a hectic one and I have to plan well to face it.