Tuesday, 3 August 2010

Characters , Integers and arrays.

Consider the statement
char c='5'
printf(“%d”,c);
The result that will be printed is 53, which is the integer value of the character constant '5' ,stored in the machine.
Now if we need to get 5 as the output,we need to use “%c” in 'printf'.
Character constants are integers written as one character within single quotes.
Now consider :
int c=getchar();
printf(“%d”,c);
The result would be the integer value representation of the entered character.
“%c” in 'printf' would print the character in the terminal.
Now consider the case of a character array
char array[]={5,6,7}.
If we print array[0],using “%d”, then '5' would be printed. Nothing would be printed if “%c” is used in the printf statement.
Here 5 itself is a constant. Now if we need to enter alphabets into a character and print it we need to do the following:
char array[]={'a','b','c'};
Now print it using “%c”.The alphabets would be printed.
If printed using “%d”, then the integer representations would be printed(97,98,99).The characters that entered into a character array are stored as integers inside the array.
If we do
char array[]={a,b,c}
The result would be an error as such:
'a' undeclared....'c' undeclared...etc..

Ensuring the correct data types while using Functions

Some of the most common errors that are produced in a program are caused due to the lack of interest ensuring that the data types used are correct. Some of these errors will be displayed while some will not and its up to the user to search the program and find out whats wrong with it. Therefore the user must be very careful while using the type of the functions, the variables that are used etc...An example below shows the use of the data type of the functions and variables...
Consider a program which is used perform the functions of a calculator: The functions that are used in the program are the follows:
push(),pop(),getop(),atof(),main()(These are just the function names)
Now each of these functions have got a specific task.
The 'getop' function is used to get the character that is entered by the user. The value that will be returned by the function would be of an integer type:
hence 'Int getop( char []);' (A string is passed as the parameter)
'push' is used to enter a value into the stack. The value would be of type 'double'. Hence the argument that is passed to the function 'push' would be of type 'double'. Nothing is returned by 'push'.
Hence 'void push(double);'
'pop' returns a double value and nothing is passed to the 'pop' as an argument because it just takes up the value from the array(array[] ,declared externally).
Hence 'double pop(void)'
'atof' takes the string as the argument and returns a double value:
hence 'double atof(char[]);'
The data type of 'array[]' should again be double because it handles 'double' values.
Now the program looks like this:

#include
#define MAX 50
int getop(char s[]);
double atof(char s[]);
void push(double);
double pop(void);
double arr[MAX];
int top=0;
main()


Now if the data types of any of the above declarations are not according to the problem you wouldn't get the expected results.
For example if the data type of array[] is not double(say integer) , you would get the result as some meaningless value. Same is the case with all of the above functions and variables declared.
For the purpose of add,subtract,multiply and divide the use of 'double' values is good(infact a necessity if you want the exact output).
But when the '%'(modulus) operator is used ,then the problem arises. If the modulus operator is used with two double values the resulting error would be as follows:
error: invalid operands to binary % (have ‘double’ and ‘double’) :
Hence the values needs to be converted to type 'int'.
Int op=pop();
push((int)pop()%op);
The type is converted from double into int. Now since pop returns a double value, this will again be converted back to a double value.

A note on External variables: Here array[] and top are declared as external variables and so can be accessed by the functions that are mentioned. Hence these values need not be passed as an argument to the functions. The fact that is to be taken into account that is that when external variables are used , the variable names cannot be changed within a function as is the case while passing arguments.

Errors,warnings and unexpected results...

While compiling C programs we might often come across errors,messages and unexpected results as our output.. The following shows a few of those :
Sometimes u might get certain unexpected values like -1098763408 as your result.
This might be due to the fact that u may not have initialized the variable used in the expression producing the result to a starting value(say i=0).
The same doing (i.e a variable not set to its initial value) might produce a 'segmentation fault' in the program. Segmentation fault may also be produced due to the fact that the alloted size for the particular string array is less than what you have just tried to fill in.
For eg. If
#define MAX 10
char string[MAX];
The size is 10.Now if u try to fill in more than 10 characters a case of segmentation fault might occur.
Another case exists where you might get an error as the following one:
error: conflicting types for ‘some_function_name’
This might be because of the fact that the function name that you have used is the name of the function that is already present in one of the header files.
For common functions like 'strlen' ,'strcat' etc this might not occur because we wont use them as our function names. But this might occur sometimes for certain function names that we may not be familiar with.
For eg. The error message would be displayed when you use function names as
'index' or 'remove' etc...even if your declaration,definition and the call might be correct.
Don't keep pondering over it,you just need to change your function name to another one that does not exist in the system's headers. For eg..change 'the name index' to 'ind' or something else.

Another common case where you may not obtain the result might be because of an error in the 'printf' statement. For eg you may have used “%d” to print a float value even though “%f”should have been be used.

It is important to check that the type of 1)the called function and 2) the variable that is used in the calling function in order to receive the value returned from the called function has to be same .If this is not the case ,an error would be produced (that would happen only if both the functions are in the same source file. But if the functions are in different source files, then the case becomes more complicated because the error would not be displayed.
In case you find that the output you obtain has got some definite value but is meaningless according to the problem, then a solution might to have a look through all the data types used in the program and see if its appropriate and matching. Such cases might occur in programs involving many functions

Sunday, 1 August 2010

Local and External variables

Local variables are variables that come into existence only when the function is called and disappears thereafter whereas external variables are accessible to all functions of the program.
To illustrate the difference between these two kinds of variables ,consider a function ‘getline()’
At first we consider the case where local variables are used. The call and the function definition in this case would be as follows:
Function call:
getline(line,MAXLENGTH);
Function definition:
int getline(char s[],int lim)
{
}
Here there is a requirement to pass the arguments from the caller function to the called function through the parenthesis as the variables used are local to the function. The value of the variable 'line' is internal to the caller function.
Whereas when we consider the case of external variables ,the situation changes. Consider the case of declaring variables externally:
#include
#define MAXLINE 100
char line[MAXLINE];
/*external variable declaration/*

The variables line has been declared externally.
Hence the call and definition of the function ‘getline()’ could be modified as follows:
Function call:
getline()
Function definition:
int getline(void)
{
}

The modified values of the variables would be available to all the functions.

Functions in C

C provides a lot of built in functions. Apart from that users define their own functions knows as user-defines functions. The function has got three parts
The declaration,the call and the definition.
Consider a function 'getline' which is used to obtain each line from the input.
The three parts of the function would be as follows:
Declaration:returntype getline(parameters);
call:getline(arguments);
definition:returntype getline(parameters)
{
declarations;
statements;
}

The function definition may well contain a 'return' statement to transfer the control back to the caller function. In such cases the return type of the function must not be 'void'.

Input,output and type conversion.

Certain basic features of the C programming language are written here.
A text stream is a sequence of characters.
The getchar() statement in C is used so to obtain the next character from a text stream that is inputted by the user.
c=getchar()
assigns the input character to c
The type of c is declared to be int(when EOF case is used).
putchar(c)
prints the character ,that has been inputted, on the terminal.
Now consider what happens when we enter a statement
putchar(c-'0')
where c=getchar() and the character inputted is 'r'.
The result is B.The reason for the result being obtained is the the automatic type conversion that is performed in C.
The type of '0' is char and is converted to int,which is the type of 'c'.Hence the integer value of '0' is subtracted from the integer value of 'r' (i.e 114-48=65)which is equal to the integer value of 'B'.Hence we obtain the result as B.
Similar type conversions can be seen in C in different examples.
If an operator has one float and one integer as its operands ,then the integer will be automatically converted to float.
We express the decimal number 16 as a floating point constant as 16.0.The same number would be represented as an integer as 16.
If we perform 16.0-16,the integer operand (i.e 16)would automatically be converted into floating point.
The generalization of this is that a narrower operand is converted into a wider operand.
For eg int->float,float->double,double->long double etc....

Now we again move onto the input/output portion.
Consider a limit for the input is up to the EOF(end of file)
read input upto end of file is reached.
We set the condition as
(c=getchar())!=EOF) .This implies that the characters are fetched onto 'c' until the End of file is reached. EOF is an integer defined in as -1.
The paranthesis in (c=getchar())!=EOF is quite important. Without the paranthesis c would obtain values '0' or '1' depending upon the character not being EOF .This is because the precedence of != is greater than that of the assignment operator(=).

TATA DOCOMO INTERNET

Inorder to set up an internet connection using TATA DOCOMO in debian using USB ,u need to do the following steps:
Install 'wvdial' ----U will need Debian 5.0 -1 & 2 DVDs to do so.
Install it using
apt-get install wvdial
Once u have finished installing wvdial create a file 'wvdial.conf' in the /etc directory.Edit the settings for your TATA DOCOMO connection in the file 'wvdial.conf' .
The settings for the connection would be as follows:



[Dialer Defaults]

Init1 = ATZ

Init2 = ATQ0 V1 E1 S0=0 &C1 &D2 +FCLASS=0

Password = MYPASSWORD

Phone = MYISPPHONENUM

Modem Type = USB Modem

Stupid Mode = yes

Baud = 460800

Dial Command = ATDT

Modem = /dev/ttyACM0

ISDN = 0

Username = MYUSERNAME

Carrier Check = yes

Auto Reconnect = yes



[Dialer Docomo]

Stupid Mode = yes

Password = Docomo

Auto Reconnect = yes

Username = Docomo

Phone = *99***1#

Save the file after editing,dial using
wvdial docomo
Certain messages will displayed in the terminal and you will obtain your
primary DNS address :
secondary DNS address:
Now open 'resolv.conf' in the /etc directory and edit the file and place your DNS addresses in the file as
nameserver:primary DNS address
nameserver:secondary DNS address

Now save the file and do wvdial docomo and you will obtain your internet connection.