Your Ad Here

Monday, June 2, 2008

Exercise-1

[A] Point out the invalid variable names:

volley-ball

CRICKET

dog and the bone

chessandchecker

tenn.



[B] Point out the invalid 'C' constants:

369.123

0005

0xbc30

0Ydf60

0ztitt

-51e-31



[C] Convert the following equations into 'C' statements:

(i) Z= 8.8(a+b) 2/c-0.5+2a/(q+r)

(a+b) * (1/m)

(ii) X=-b+(b*b)+2-4ac

2a

(iii) R=2v+6.22(c+d)

g+v



[D] Find out the Outputs:

(1) main( )

{

printf("%d%o%x",72,72,72);

}



(2) main( )

{

int i=1,j=2,sum=0;

sum=sum+j;

j=j+i;

i=i*j;

printf("%d%d%d",sum,j,i);

}



(3) main()

{

float a=1, b=3;

int c;

c=b%a;

printf("%d",c);

}



(4) main( )

{

int p=500*500/500;

printf("g=%d",g);

}



[E] Write a program to interchange the contents of a and b. Two variables a and b contain values 2 and 5.



All the best..!! Get set go.. Next time we'll discuss the solution of Exercise-1.

Wednesday, May 21, 2008

Comments in 'C' lang

I guess after reading the program, many things are clear to most of the people. Now, I would like to move your focus on the double backslash ( //), which plays an important role while writing complex programs in C language.

We humans are in a habit to keep a note of everything, which remind us to keep a follow-up of all the things we need to keep a track off. In the same way, C gives us flexibility to put a comment at any place where there is a possibility of confusion. Now, following are few useful tips to write a comment.

- There are two formats to write a comment in C lang. They are as follows:
a) put double backslash ( // ) in every line before the starting of the comment.
For example: // C lang is cool
// I am learning C lang just for fun

OR b) enclose a comment within /* */.
Start a comment with a single backslash with an asterick ( /* ) and then at the end of the
comment put an asterick with a single backslash ( */ ). A comment can be split over
more than one line. for e.g.
/* My
name is
Samiha */
- We can add any number of comments in a single program. But they cannot be nested.
For example: /* C lang /* is very easy */ */ - ( It is Invalid)

I hope a simple way of adding a comment to your program will help you to make your program user friendly & this will save your time to get the hold of the program whenever you try to figure out what the program does.

Tuesday, May 13, 2008

Q & A Session contd...

Question 3: What is printf() function?
Answer: printf() is a function defined in stdio.h library file and is used for printing output on the standard output device which is Monitor. This function takes 2 parameters:
1. The format string
2. The Variable

e.g.
printf("%d",num);

Here %d is the format string used, the format string tells the compiler which type of data needs to be shown on the standard output device. Some of the format specifiers are:

%d for integer values
%c for character values
%s for strings
%f for float values (these are the values with decimal point e.g 123.45)

Variable is the memory location in the primary memory where we store the value.

For more on printf() visit the links available.

Question 4: What is scanf() function?
Answer: scanf() is used for reading input from the standard input device which is keyboard. This function takes 2 parameters:
1. The format string
2. The address of the variable where the value will be stored.

e.g. scanf(“%d”, &num);

%d is used to tell the compiler that the value to be read is an interger value and the other parameter &num tells the compiler the memory location to put the value in. The “&” operator is used for accessing the address of a defined variable.

Some of the format specifiers are:

%d for integer values
%c for character values
%s for strings
%f for float values (these are the values with decimal point e.g 123.45)

For more on scanf() visit the links available.

Sunday, April 27, 2008

Our First 'C' Program

Today, we will create a simple 'C' program which would read a number from the keyboard and would display it on the screen.

#include<stdio.h>//Include these files for using standard input and output functions
#include<conio.h>
void main(void)
{
int num; //Define an integer variable which would store a number
printf("Enter a number:"); //This would print on the screen "Eenter a number:"
scanf("%d", &num); //This would allow the user to enter a number
printf("Number : %d", num); //Prints the entered number back to the screen
getch();
}

There are many things which can be noticed in this sample program and which can confuse us, lets focus on a few things.

Question 1: Why do we have a semi colon ; after every statement inside main function?
Answer: 'C' compiler marks an end of the statement wherver it finds a semicolon. It would be easier if if take it this way, in English whenever we want to mark the end of a sentence, we put a full stop (.), but in case of 'C' language we put a (;) semi colon.

Question 2: Why do we have "int" written before num, what does it denote?
Answer: 'C' language has got certain data types, these data types can store different types of data in them, for instance integers, floating point numbers, characters, strings etc. The syntax of using these data types is:

datatype variablename;
e.g.
int num; // As in our example

Now, for storing values between -32768 to +32767 we use int data type, this data type requires 2 bytes of memory and the keyword in 'C' for this is 'int'.


In the next post I will answer a few more questions on this sample program we made today.

Saturday, April 19, 2008

Skeleton of a 'C' Program

C is a structured language and everything which need to be coded or programmed must be structured in nature, for those who don't know what is structured programming can follow one of the links availbale.

We will start with understanding the basic programming construct in 'C language'.

Sample Program:
#include
#include
void main()
{
printf("Hello World");
}

Point # 1:The most important thing in this program is "#include", these are called preprocessor directives in C language, it is denoted by # symbol. 'C' does not support dynamic linking, statis linking means everything which needs to be included becomes part of the executable file. So, when I wrote #include, there is a process called preprocessing which happens before compilation in 'C', it copies the content of the file specifed at the place of this statement, In our case the content of the file stdio.h will be placed in the place of #include. After the precprocessing is complete, compilation start which eventually creates the executable.

Ponit #2:
What is main() here, main is a function, a function is a small block of code which executes together. But why do we name it main?, we could have had 'small', 'big' etc. but why 'main', the answer is every function needs to be called for execution and because every application runs under the Operating System, OS is the one who starts the execution, but the OS is programmed to call only function named 'main', we can say 'main' function is the entry to the application. All other function written are called by main as and when required.

In the next post we would discuss about the standard input and output functions.

Saturday, April 12, 2008

Let’s Begin

7 Years ago when I decided to change and join IT industry, the first programming language I came across was C language. I was told by others that this would be my ultimate test of intelligence as they were finding it pretty difficult to cope with. Whatever I learnt till that point of time was kind of a walkover for me.

Then the day arrived when we were supposed to attend our first C language session, the session began and the first line from our tutor made it clear for me that how important it was to know C language to succeed in Software industry. He said, “This language is mother of all the High Level Languages that have existed and will be in the future as well.” Everyone knows how valid those comments were, all the languages which I have worked on till date, seem to have something from this language. C++, JAVA, VB 6.0, C#, pick anyone you like, you would feel as if it has got it roots in C.

We will start our first technical discussion from the next post.

Sunday, April 6, 2008

C language, no longer a mirage...

Hey people,
For the last 4 years I have been into IT industry and have met a lot of people across the globe. I have found one thing common in all of them, especially the beginners, they are scared of 'C' language or we can say that 'C' language is like mirage to them, the closer they get, more difficult they find it to understand.

My purpose of starting this blog is to create a network or a central place where we could share our knowledge on 'C', especially for those who are new to this industry and find it challenging to break the intial barrier.

I would be posting a lesson every day on this blog and you will also find some really usefull links to other sites.

I welcome you all,
Sankalp