May 17, 2016
Articles on 'const' and 'volatile' qualifiers
February 25, 2016
Bit Fields in C
#include <stdio.h>
{
int a:5;
int b:10;
int c: 12;
int d;
} n1;
int main()
{
n1.d = 0x12345678;
printf("a 0x%x b 0x%x c 0x%x d 0x%x\n", n1.a, n1.b, n1.c, n1.d);
}
Output:
a 0xffff fff8 b 0xffff fe78 c 0x678 d 0x12345678
Functions with variable number of arguments
2. The function prototype should have atleast one known argument followed by ellipsis (set of 3 dots) which represent any unknown number of arguments passed. The ellispsis should only appear as the last aruments.
void function(char var1, int var2, ...);
3. This feature is implemented using a set of macros va_list, va_start, va_arg, va_end, va_copy defined in stdarg.h. All the above macros were defined since C89 except va_copy which is defined since C99.
4. va_list is a type used to declare a pointer to be used by other macros.
va_list ap;
5. va_start is a macro which initializes ap to point to the first anonymous argument (unknown argument) which is stored in stack memory. The known argument just before the ellipsis is used by va_start as a reference in the memory to locate the unknown arguments. va_start should be invoked before va_arg.
va_start(ap, var2);
6. va_arg is a macro which retuns an unknown argument on every call and steps ap to the next argument. var_arg uses type name as its second parameter to determine the return type and step size.
temp = va_arg(ap, );
The used can be one of the fully promoted types (int, unsigned int, double), its pointer types and void *. The behaviour of va_arg is undefined if the is not compatible with the actual type if there is no next argument.
7. va_copy is a macro that creates a copy of ap such that the current state of ap is copied.
va_copy(ap, ap_copy);
8. va_end clears the ap so that is no more usable. It must be called before returning from the function.
va_end(ap);
#include <stdio.h>
#include <stdarg.h>
int add(int c, ...);
int main()
{
int res1, res2, res3, res4;
res1 = add(3, 1, 2, 3);
res2 = add(5, 1, 2, 3, 4, 5);
res3 = add(7, 1, 2, 3, 4, 5, 6, 7);
res4 = add(9, 1, 2, 3, 4, 5, 6, 7, 8, 9);
printf("res1 = %d\nres2 = %d\nres3 = %d\nres4 = %d\n",
res1, res2, res3, res4);
}
int add(int count, ...)
{
int sum = 0, sum2 = 0;
va_list valist, valist2;
va_start(valist, count);
while (count) {
if (count == 2)
va_copy(valist2, valist);
sum += va_arg(valist, int *);
if (count <=2)
sum2 += va_arg(valist2, int);
--count;
}
return sum;
}
December 18, 2015
Cloud & Cloud Computing
- Cloud in generalAccumulation of objects at a place
- Cloud in Computing
Collection of servers
- Data on Cloud
Example: Google drive, iCloud, Dropbox, SkyDrive
- Application on Cloud
Example: Wordpress, Drupal, Zoomla, mySql
Cloud and a Photographer
- Without Cloud
- Photographer takes photos using his camera
- Moves to his office
- Connects his camera to his workstation (with good configuration)
- Copies the files, edit it.
- Takes edited copy to a photo lab to get it printed.
- With Cloud (Example: Adobe’s Creative Cloud)
- Photographer takes photos using his android camera
- Automatically uploaded to the Creative Cloud (Adobe)
- Logs in his online cloud application (Photoshop) using a web browser or a middleware (Creative Cloud)
- Edits the uploaded photo and Print using Google Cloud Print
- Types
- Public Cloud
- Private Cloud
- Hybrid Cloud
- Services Provided
- Infrastructure as a Service (IaaS)
- Platform as a Service (PaaS)
- Software as a service (Saas)
- Cloud Types and Services
Example: Google Compute Cloud, Salesforce, Microsoft Azure, Amazon Elastic Cloud, Rackspace
- Pros
- Scale and Cost
- Choice
- Redundancy
- Disaster Recovery
- Increased flexibility
- Cons
- Internet connectivity is essential
- Security & Privacy
- Lack of Control and Ownership
- How Cloud Computing Works - YouTube.com
- The Three Ways to Cloud Compute - YouTube.com
- Cloud Computing - Wikipedia.org
- Adobe Creative Cloud
- Cloud Computing - tutorialspoint.com
- thecloudtutorial.com
December 11, 2015
C Code | Tic Tac Toe game
C Code | Hangman game
C Code | Master mind or Bulls and Cows game
C Code | Arithmetic quiz game
October 15, 2015
C Code | Guess a number game
September 09, 2015
C Programming guidelines and standards
January 28, 2015
Training Programme Calendar (MSME-PPDC)
November 24, 2014
Centre for Development and Advanced Computing (C-DAC) - Education and Training programmes
| Post Graduate Diploma Programmes
|
Formal Training Programmes
|
Faculty Development Programmes
C-DAC conducts advanced faculty training programmes...
|
||
| Corporate Training Programmes
C-DAC offers various specialized training...
|
Education Technologies
|
Other Initiatives
Capacity Building Initiatives...
|
||
| International Training Program
|
||||
September 28, 2014
GATE 2015 | How to crack through GATE? Must watch Video
Mandatory for all streams
1. Mathematics
2. General Aptitude
CS
3. Data Structures and Algorithms
4. Operating Systems
5. DBMS
6. Computer Networks
EC
3. Networks
4. Electronic Devices
5. Analog Circuits
6. Communication
EE (Vast Syllabus)
3. Network Theory
4. Control Systems
5. Electrical Machines
6. Power Electronics
IE (Minimal syllabus)
3. Network
4. Control Systems
5. Transducers
ME
3. Strength of Materials
4. Thermal Science
5. Manufacturing Engineering
January 23, 2014
CS2302 - Computer Networks - 2 & 16 Marks with answers
July 12, 2013
December 11, 2012
How do hardware and software interact?
Reference:
Andrews, Jean (2006). A+ guide to managing and maintaining your pc fifth edition. Canada: Thomson Course Technology
October 19, 2009
Multiline TextBox Controls - Visual Basic 6 TextBox Control
You create multiline TextBox controls by setting the MultiLine property to True and the ScrollBars property to 2-Vertical or 3-Both. A vertical scroll bar causes the contents of the control to automatically wrap when a line is too long for the control's width, so this setting is most useful when you're creating memo fields or simple word processor-like programs. If you have both a vertical and a horizontal scroll bar, the TextBox control behaves more like a programmer's editor, and longer lines simply extend beyond the right border. I've never found a decent use for the other settings of the ScrollBars property (0-None and 1-Horizontal) in a multiline TextBox control. Visual Basic ignores the ScrollBars property if MultiLine is False.
Both these properties are read-only at run time, which means that you can't alternate between a regular and a multiline text box, or between a word processor-like multiline field (ScrollBars = 2-Vertical) and an editorlike field (ScrollBars = 3-Both). To tell the whole truth, Visual Basic's support for multiline TextBox controls leaves much to be desired. You can do very little with such controls at run time, except to retrieve and set their Text properties. When you read the contents of a multiline TextBox control, it's up to you to determine where each line of text starts and ends. You do this with a loop that searches for carriage return (CR) and line feed (LF) pairs, or even more easily using the new Split string function:
' Print the lines of text in Text1, labeling them with their line numbers.
Dim lines() As String, i As Integer
lines() = Split(Text1.Text, vbCrLf)
For i = 0 To UBound(lines)
Print (i + 1) & ": " & lines(i)
Next
The support offered by Visual Basic for multiline TextBox controls ends here. The language doesn't offer any means for learning such vital information as at which point each line of text wraps, which are the first visible line and the first visible column, which line and column the caret is on, and so on. Moreover, you have no means of programmatically scrolling through a multiline text box. The solutions to these problems require Microsoft Windows API programming. In my opinion, however, Visual Basic should offer these features as built-in properties and methods.
You should account for two minor issues when including one or more multiline TextBox controls on your forms. When you enter code in a word processor or an editor, you expect that the Enter key will add a newline character (more precisely, a CR-LF character pair) and that the Tab key will insert a tab character and move the caret accordingly. Visual Basic supports these keys, but because both of them have special meaning to Windows the support is limited: The Enter key adds a CR-LF pair only if there isn't a default push button on the form, and the Tab key inserts a tab character only if there aren't other controls on the form whose TabStop property is set to True. In many circumstances, these requirements can't be met, and some of your users will find your user interface annoying. If you can't avoid this problem, at least add a reminder to your users that they can add new lines using the Ctrl+Enter key combination and insert tab characters using the Ctrl+Tab key combination. Another possible approach is to set the TabStop property to False for all the controls in the form in the multiline TextBox's GotFocus event and to restore the original values in the LostFocus event procedure.
Auto-Tabbing Fields and Formatting Text - Visual Basic 6 TextBox Control
Auto-Tabbing Fields
Users aren't usually delighted to spend all their time at the keyboard. Your job as a programmer is to make their jobs easier, and so you should strive to streamline their everyday work as much as possible. One way to apply this concept is to provide them with auto-tabbing fields, which are fields that automatically advance users to the next field in the Tab order as soon as they enter a valid value. Most often, auto-tabbing fields are those TextBox controls whose MaxLength property has been assigned a non-null value. Implementing such an auto-tabbing field in Visual Basic is straightforward:
Private Sub Text1_Change()
If Len(Text1.Text) = Text1.MaxLength Then SendKeys "{Tab}"
End Sub
The trick, as you see, is to have your program provide the Tab key on behalf of your user. In some cases, this simple approach doesn't work—for example, when you paste a long string into the field. You might want to write code that works around this and other shortcomings. Auto-tabbing is a nice feature but not vital to the application, so whether you write a workaround or not isn't a real problem in most cases.
Formatting Text
Many business applications let you enter data in one format and then display it in another. For example, numeric values can be formatted with thousand separators and a fixed number of decimal digits. Currency values might have a $ symbol (or whatever your national currency symbol is) automatically inserted. Phone numbers can be formatted with dashes to split into groups of digits. Credit-card numbers can be made more readable with embedded spaces. Dates can be shown in long-date format ("July 22, 2007"). And so on.
The LostFocus event is an ideal occasion to format the contents of a TextBox control as soon as the input focus leaves it. In most cases, you can perform all your formatting chores using the Format function. For example, you can add thousand separators to a numeric value in the txtNumber control using this code:
Private Sub txtNumber_LostFocus()
On Error Resume Next
txtNumber.Text = Format(CDbl(txtNumber.Text), _
"#,###,###,##0.######")
End Sub
When the field regains the focus, you'll want to get rid of those thousand separators. You can do it easily using the CDbl function:
Private Sub txtNumber_GotFocus()
' On Error is necessary to account for empty fields.
On Error Resume Next
txtNumber.Text = CDbl(txtNumber.Text)
End Sub
In some cases, however, formatting and unformatting a value isn't that simple. For example, you can format a Currency value to add parentheses around negative numbers, but there's no built-in Visual Basic function able to return a string formatted in that way to its original condition. Fear not, because nothing prevents you from creating your own formatting and unformatting routines. I have built two general-purpose routines for you to consider.
The FilterString routine filters out all unwanted characters in a string:
Function FilterString(Text As String, validChars As String) As String
Dim i As Long, result As String
For i = 1 To Len(Text)
If InStr(validChars, Mid$(Text, i, 1)) Then
result = result & Mid$(Text, i, 1)
End If
Next
FilterString = result
End Function
FilterNumber builds on FilterString to strip down all formatting characters in a number and can also trim trailing decimal zeros:
Function FilterNumber(Text As String, TrimZeros As Boolean) As String
Dim decSep As String, i As Long, result As String
' Retrieve the decimal separator symbol.
decSep = Format$(0.1, ".")
' Use FilterString for most of the work.
result = FilterString(Text, decSep & "-0123456789")
' Do the following only if there is a decimal part and the
' user requested that nonsignificant digits be trimmed.
If TrimZeros And InStr(Text, decSep) > 0 Then
For i = Len(result) To 1 Step -1
Select Case Mid$(result, i, 1)
Case decSep
result = Left$(result, i - 1)
Exit For
Case "0"
result = Left$(result, i - 1)
Case Else
Exit For
End Select
Next
End If
FilterNumber = result
End Function
The feature I like most in FilterNumber is that it's locale-independent. It works equally well on both sides of the Atlantic ocean (and on other continents, as well.) Instead of hard-coding the decimal separator character in the code, the routine determines it on the fly, using the Visual Basic for Applications (VBA) Format function. Start thinking internationally now, and you won't have a nervous breakdown when you have to localize your applications in German, French, and Japanese.
The Format function lets you retrieve many locale-dependent characters and separators.
Format$(0.1, ".") ' Decimal separator
Format$(1, ",") ' Thousand separator
Mid$(Format(#1/1/99#, "short date"), 2, 1) ' Date separator
You can also determine whether the system uses dates in "mm/dd/yy" (U.S.) format or "dd/mm/yy" (European) format, using this code:
If Left$(Format$("12/31/1999", "short date"), 2) = 12 Then
' mm/dd/yy format
Else
' dd/mm/yyyy format
End If
There's no direct way to determine the currency symbol, but you can derive it by analyzing the result of this function:
Format$(0, "currency") ' Returns "$0.00" in US
It isn't difficult to write a routine that internally uses the information I've just given you to extract the currency symbol as well as its default position (before or after the number) and the default number of decimal digits in currency values. Remember, in some countries the currency symbol is actually a string of two or more characters.
To illustrate these concepts in action, I've built a simple demonstration program that shows how you can format numbers, currency values, dates, phone numbers, and credit-card numbers when exiting a field, and how you can remove that formatting from the result when the input focus reenters the TextBox control. Follwoing figure shows the formatted results.
Formatting and unformatting the contents of TextBox controls makes for more professional-looking applications
Private Sub txtNumber_GotFocus()
' Filter out nondigit chars and trailing zeros.
On Error Resume Next
txtNumber.Text = FilterNumber(txtNumber.Text, True)
End Sub
Private Sub txtNumber_LostFocus()
' Format as a number, grouping thousand digits.
On Error Resume Next
txtNumber.Text = Format(CDbl(txtNumber.Text), _
"#,###,###,##0.######")
End Sub
Private Sub txtCurrency_GotFocus()
' Filter out nondigit chars and trailing zeros.
' Restore standard text color.
On Error Resume Next
txtCurrency.Text = FilterNumber(txtCurrency.Text, True)
txtCurrency.ForeColor = vbWindowText
End Sub
Private Sub txtCurrency_LostFocus()
On Error Resume Next
' Show negative values as red text.
If CDbl(txtCurrency.Text) < 0 Then txtCurrency.ForeColor = vbRed
' Format currency, but don't use parentheses for negative numbers.
' (FormatCurrency is a new VB6 string function.)
txtCurrency.Text = FormatCurrency(txtCurrency.Text, , , vbFalse)
End Sub
Private Sub txtDate_GotFocus()
' Prepare to edit in short-date format.
On Error Resume Next
txtDate.Text = Format$(CDate(txtDate.Text), "short date")
End Sub
Private Sub txtDate_LostFocus()
' Convert to long-date format upon exit.
On Error Resume Next
txtDate.Text = Format$(CDate(txtDate.Text), "d MMMM yyyy")
End Sub
Private Sub txtPhone_GotFocus()
' Trim embedded dashes.
txtPhone.Text = FilterString(txtPhone.Text, "0123456789")
End Sub
Private Sub txtPhone_LostFocus()
' Add dashes if necessary.
txtPhone.Text = FormatPhoneNumber(txtPhone.Text)
End Sub
Private Sub txtCreditCard_GotFocus()
' Trim embedded spaces.
txtCreditCard.Text = FilterNumber(txtCreditCard.Text, True)
End Sub
Private Sub txtCreditCard_LostFocus()
' Add spaces if necessary.
txtCreditCard.Text = FormatCreditCard(txtCreditCard.Text)
End Sub
Instead of inserting the code that formats phone numbers and credit-card numbers right in the LostFocus event procedures, I built two distinct routines, which can be more easily reused in other applications, as shown in the code below.
Function FormatPhoneNumber(Text As String) As String
Dim tmp As String
If Text <> "" Then
' First get rid of all embedded dashes, if any.
tmp = FilterString(Text, "0123456789")
' Then reinsert them in the correct position.
If Len(tmp) <= 7 Then
FormatPhoneNumber = Format$(tmp, "!@@@-@@@@")
Else
FormatPhoneNumber = Format$(tmp, "!@@@-@@@-@@@@")
End If
End If
End Function
Function FormatCreditCard(Text As String) As String
Dim tmp As String
If Text <> "" Then
' First get rid of all embedded spaces, if any.
tmp = FilterNumber(Text, False)
' Then reinsert them in the correct position.
FormatCreditCard = Format$(tmp, "!@@@@ @@@@ @@@@ @@@@")
End If
End Function
Unfortunately, there isn't any way to create locale-independent routines that can format any phone number anywhere in the world. But by grouping all your formatting routines in one module, you can considerably speed up your work if and when it's time to convert your code for another locale.
The CausesValidation Property and the Validate Event - Visual Basic 6 TextBox Control
Let's try a practical example. Imagine that you have five controls on a form: a required field (a TextBox control, txtRequired, that can't contain an empty string), a numeric field, txtNumeric, that expects a value in the range 1 through 1000, and three push buttons: OK, Cancel, and Help. (See the figure below.) You don't want to perform validation if the user presses the Cancel or Help buttons, so you set their CausesValidation properties to False. The default value for this property is True, so you don't have to modify it for the other controls. Run the sample program on the companion CD, type something in the required TextBox, and then move to the second field. Because the second field's CausesValidation property is True, Visual Basic fires a Validate event in the first TextBox control:
' Check that field is not empty.
If txtRequired.Text = "" Then
MsgBox "Please enter something here", vbExclamation
Cancel = True
End If
End Sub
A demonstration program that lets you experiment with the new Visual Basic Validate features
If Not IsNumeric(txtNumeric.Text) Then
Cancel = True
ElseIf CDbl(txtNumeric.Text) < 1 Or CDbl(txtNumeric.Text) > 1000 Then
Cancel = True
End If
If Cancel Then
MsgBox "Please enter a number in range [1-1000]", vbExclamation
End If
End Sub
' You can't close this form without validating the current field.
If UnloadMode = vbFormControlMenu Then
On Error Resume Next
ValidateControls
If Err = 380 Then
' The current field failed validation.
Cancel = True
End If
End If
End Sub
Visual Basic 6's validation scheme has two flaws, though. If your form has a CommandButton whose Default property is set to True, pressing the Enter key while the input focus is on another control results in a click on the CommandButton control but doesn't fire a Validate event, even if the CausesValidation property of the CommandButton control is set to True. The only way to solve this problem is to invoke the ValidateControls method from within the default CommandButton control's Click event procedure.
The second flaw is that the Validate event doesn't fire when you're moving the focus from a control whose CausesValidation property is False, even if the control that receives the focus has its CausesValidation property set to True.
The new Visual Basic 6 validation mechanism is simple and can be implemented with little effort. But it isn't the magic answer to all your validation needs. In fact, this technique can only enforce field-level validation; it does nothing for record-level validation. In other words, it ensures that one particular field is correct, not that all fields in the form contain valid data. To see what I mean, run the demonstration program, enter a string in the first field, and press Alt+F4 to close the form. Your code won't raise an error, even if the second field doesn't contain a valid number! Fortunately, it doesn't take much to create a generic routine that forces each control on the form to validate itself:
' You can't close this form without validating all the fields on it.
If UnloadMode = vbFormControlMenu Then
On Error Resume Next
Dim ctrl As Control
' Give the focus to each control on the form, and then
' validate it.
For Each ctrl In Controls
Err.Clear
ctrl.SetFocus
If Err = 0 Then
' Don't validate controls that can't receive input focus.
ValidateControls
If Err = 380 Then
' Validation failed, refuse to close.
Cancel = True: Exit Sub
End If
End If
Next
End If
End Sub
One Visual Basic operator has great potential when it comes time to validate complex strings but is neglected by most Visual Basic developers. Let's say you have a product code that consists of two uppercase characters followed by exactly three digits. You might think that you need some complex string functions to validate such a string until you try the Like operator, as follows:
Validation Routines for Numbers - Visual Basic 6 TextBox Control
Although trapping invalid keys in the KeyPress or KeyDown event procedures seems a great idea at first, when you throw your application to inexperienced users you soon realize that there are many ways for them to enter invalid data. Depending on what you do with this data, your application can come to an abrupt end with a run-time error or—much worse—it can appear to work correctly while it delivers bogus results. What you really need is a bullet-proof method to trap invalid values.
Before I offer you a decent solution to the problem, let me explain why you can't rely solely on trapping invalid keys for your validation chores. What if the user pastes an invalid value from the clipboard? Well, you might say, let's trap the Ctrl+V and Shift+Ins key combinations to prevent the user from doing that! Unfortunately, Visual Basic's TextBox controls offer a default edit menu that lets users perform any clipboard operation by simply right-clicking on them. Fortunately, there's a way around this problem: Instead of trapping a key before it gets to the TextBox control, you trap its effect in the Change event and reject it if it doesn't pass your test. But this makes the structure of the code a little more complex than you might anticipate:
' Form-level variables
Dim saveText As String
Dim saveSelStart As Long
Private Sub Text1_GotFocus()
' Save values when the control gets the focus.
saveText = Text1.Text
saveSelStart = Text1.SelStart
End Sub
Private Sub Text1_Change()
' Avoid nested calls.
Static nestedCall As Boolean
If nestedCall Then Exit Sub
' Test the control's value here.
If IsNumeric(Text1.Text) Then
' If value is OK, save values.
saveText = Text1.Text
saveSelStart = Text1.SelStart
Else
' Prepare to handle a nested call.
nestedCall = True
Text1.Text = saveText
nestedCall = False
Text1.SelStart = saveSelStart
End If
End Sub
Private Sub Text1_KeyUp(KeyCode As Integer, Shift As Integer)
saveSelStart = Text1.SelStart
End Sub
Private Sub Text1_MouseDown(Button As Integer, _
Shift As Integer, X As Single, Y As Single)
saveSelStart = Text1.SelStart
End Sub
Private Sub Text1_MouseMove(Button As Integer, _
Shift As Integer, X As Single, Y As Single)
saveSelStart = Text1.SelStart
End Sub
If the control's value doesn't pass your tests in the Change event procedure, you must restore its previous valid value; this action recursively fires a Change event, and you must prepare yourself to neutralize this nested call. You might wonder why you also need to trap the KeyUp, MouseDown, and MouseMove events: The reason is that you always need to keep track of the last valid position for the insertion point because the end user could move it using arrow keys or the mouse.
The preceding code snippet uses the IsNumeric function to trap invalid data. You should be aware that this function isn't robust enough for most real-world applications. For example, the IsNumeric function incorrectly considers these strings as valid numbers:
123,,,123
345-
$1234 ' What if it isn't a currency field?
2.4E10 ' What if I don't want to support scientific notation?
To cope with this issue, I have prepared an alternative function, which you can modify for your particular purposes. (For instance, you can add support for a currency symbol or the comma as the decimal separator.) Note that this function always returns True when it's passed a null string, so you might need to perform additional tests if the user isn't allowed to leave the field blank:
Function CheckNumeric(text As String, DecValue As Boolean) As Boolean
Dim i As Integer
For i = 1 To Len(text)
Select Case Mid$(text, i, 1)
Case "0" To "9"
Case "-", "+"
' Minus/plus signs are only allowed as leading chars.
If i > 1 Then Exit Function
Case "."
' Exit if decimal values not allowed.
If Not DecValue Then Exit Function
' Only one decimal separator is allowed.
If InStr(text, ".") < i Then Exit Function
Case Else
' Reject all other characters.
Exit Function
End Select
Next
CheckNumeric = True
End Function
If your TextBox controls are expected to contain other types of data, you might be tempted to reuse the same validation framework I showed you previously—including all the code in the GotFocus, Change, KeyUp, MouseDown, and MouseMove event procedures—and replace only the call to IsNumeric with a call to your custom validation routine. Things aren't as simple as they appear at first, however. Say that you have a date field: Can you use the IsDate function to validate it from within the Change event? The answer is, of course, no. In fact, as you enter the first digit of your date value, IsDate returns False and the routine therefore prevents you from entering the remaining characters, and so preventing you from entering any value.
This example explains why a key-level validation isn't always the best answer to your validation needs. For this reason, most Visual Basic programmers prefer to rely on field-level validation and test the values only when the user moves the input focus to another field in the form. I explain field-level validation in the next section.
Receive all updates via Facebook. Just Click the Like Button Below▼
▼
You can also receive Free Email Updates:
Copyright © 2008 -2012 dgpride. All rights reserved.













