RK's C

RK's C

Share

I ll go to explain abt some imoprtent topics related to CSE papers

Define C tokens 06/12/2011

In a C source program, the basic element recognized by the compiler is the "token." A token is source-program text that the compiler does not break down into component elements.

Syntax

token:
keyword

identifier constant string-literal operator punctuator

The keywords, identifiers, constants, string literals, and operators described in this section are examples of tokens. Punctuation characters such as brackets ([ ]), braces ({ }), parentheses ( ( ) ), and commas (,) are also tokens.

Read more: http://wiki.answers.com/Q/Define_C_tokens

Define C tokens In a C source program, the basic element recognized by the compiler is the "token." A token is source-program text that the compiler does not break down into component elements.

05/11/2011

C program to swap two numbers with and without using third variable, swapping in c using pointers and functions (Call by reference) , swapping means interchanging. For example if in your c program you have taken two variable a and b where a = 4 and b = 5, then before swapping a = 4, b = 5 after swapping a = 5, b = 4
In our c program to swap numbers we will use a temp variable to swap two numbers. Swapping is used in sorting that is when we wish to arrange numbers in a particular order either in ascending order or in descending order.
Swaping of two numbers in c




main()
{
int x, y, temp;

printf("Enter the value of x and y ");
scanf("%d%d",&x, &y);

printf("Before Swapping\nx = %d\ny = %d\n",x,y);

temp = x;
x = y;
y = temp;

printf("After Swapping\nx = %d\ny = %d\n",x,y);

getch();
return 0;
}

Swapping of two numbers without third variable

You can also swap two numbers without using temp or temporary or third variable. In that case c program will be as shown :-



main()
{
int a, b;

printf("Enter two numbers to swap ");
scanf("%d%d",&a,&b);

a = a + b;
b = a - b;
a = a - b;

printf("a = %d\nb = %d\n",a,b);
return 0;
}

To understand above logic simply choose a as 7 and b as 9 and then do what is written in program. You can choose any other combination of numbers as well. Sometimes it's a good way to understand a program.
Swap two numbers using pointers



main()
{
int x, y, *a, *b, temp;

printf("Enter the value of x and y ");
scanf("%d%d",&x,&y);

printf("Before Swapping\nx = %d\ny = %d\n", x, y);

a = &x;
b = &y;

temp = *b;
*b = *a;
*a = temp;

printf("After Swapping\nx = %d\ny = %d\n", x, y);

return 0;
}

Swapping numbers using call by reference

In this method we will make a function to swap numbers.



void swap(int*, int*);

main()
{
int x, y;

printf("Enter the value of x and y\n");
scanf("%d%d",&x,&y);

printf("Before Swapping\nx = %d\ny = %d\n", x, y);

swap(&x, &y);

printf("After Swapping\nx = %d\ny = %d\n", x, y);

return 0;
}

void swap(int *a, int *b)
{
int temp;

temp = *b;
*b = *a;
*a = temp;
}

02/10/2011

Long integer values occupy twice the space in memory.These long integer would occupy 4 bytes of memory and are declared using the keyword log.
eg;:long int i;

Short int :-Short int is but an ordinary int.So they can be declared as int j;

Sometimes we come across situations where the constant is small enough to be an int, but still we want to give it as much storage as a long. In such cases we add a suffix ‘L’ or ‘l’ at the end of the number.

23 an integer occupies 2 bytes where as 23 L a long integer occupies 4 bytes

Integer Signed and Unsigned

When a value is declared it by default takes as +ve value.When we declare the variable to be unsigned the size of the range which is from –32768 to 32767 shifts to 0-65535. It almost double the size.This so happens because as declaring the integer as unsigned the 16th bit is now free and is not used to store the sign of the number.An unsigned integer still occupies 2 bytes.An unsigned int is nothing but a short unsigned int.

02/10/2011

Data Types

Ansi C supports 4 classes of data types
1.Primary or fundamental data types
2.User defined data types
3.Derived data types
4.Empty dataset

All C compilers supports 4 fundamental data types
Data type meaning size(bytes) range of values
Char a character 1 -128 to 127
Int an integer 2 -32768 to 32768
Float a single 4 3.4e-38 to 3.4e+38
precession
real no.
Double a double 8 1.7e-308 to 1.7e+308
precession
real no.
Void valueless 0

29/09/2011

Simple c program

/* Filename: hello.c
Author: Brian Kernighan & Dennis Ritchie
Date written: ?/?/1978
Description: This program prints the greeting “Hello, World!”
*/


int main ( void )
{
printf ( “Hello, World!\n” ) ;
return 0 ;
}

29/09/2011

Escape Charecters

\n : new line
\t : H. tab
\v : V.tab
\b : back space
\0 : null
\a : alert

27/09/2011

Expressions and Classes

arithmetic: + – * / %
comparison: == != < >=
bitwise logical: & | ^ ~
shifting: >
lazy logical: && || !
conditional: ? :
assignment: = += -=
increment/decrement: ++ --
sequencing: ,
pointer: * -> & []

27/09/2011

Continued.......... to Tockens
Names:-

# Sometimes called identifiers.
# Can be of any length, but on the first 31 are significant (too long is as bad as too short).
Are case sensitive:
# abc is different from ABC
# Must begin with a letter and the rest can be letters, digits, and underscores.
# Must follow the standards for this course!

Punctuations:-

# Semicolons, colons, commas, apostrophes, quotation marks, braces, brackets, and parentheses.
# ; : , ‘ “ [ ] { } ( )

25/09/2011

Hash Table (Closed Hasing/Open addressing)

1. The keys 12,18,13,2,3,23,5 and 15 are inserted into an initially empty hash table of length 10 using open addressing with hash function h (k) =k mod 10 and linear probing. What is the resultant hash table? (2009)

Explanation:
1. h(12)=12mod10=2 so 12 is in 2nd position
2. h(18)=18mod10=8 so 18 is in 8th position
3. h(13)=13mod10=3 so 13 is in 3rd position
4. h(2)=2mod10=2 so 2 is in 2nd position but 2nd and 3rd positions are full so it will goes to 4th position.
5. h(3)=3mod10=3 so 3 is in 3rd position but 3rd and 4th positions are full so it will goes to 5th position.
6. h(23)=23mod10=3 so 23 is in 3rd position but 3rd ,4th and 5th positions are full so it will goes to 6th position.
7. h(5)=5mod10=5 so 5 is in 5th position but 5th and 6th positions are full so it will goes to 7th position.
8. h(15)=15mod10=5 so 15 is in 5th position but 5th, 6th ,7th and 8th positions are full so it will goes to 9th position.
From the above steps 2nd to 9th

2. Consider a hash table of size seven, with starting index zero, and a hash function (3X+4) mod 7. Assuming the hash table is initially empty, which of the following is the contents of the table with the sequence 1, 3,8,10 is inserted into the table using closed hashing? Note that ‘_’ denotes an empty location in the table. (2007)

Explanation:
H(x) = (3x+4) mod7
1. If x=1 , 7 mod 7=0 so it is in 0th position
2. If x=3 , 3 mod 7=6 so it is in 6th position
3. If x=8 , 28 mod 7=0 so it is in 0th position but it is already full so it will goes to the 1st position
4. If x=10 ,34 mod 7=6 so it is in 6th position but it is already full so it will goes to the 0th ,1st position but it is also full so it goes to 2nd place.

3. Hash table size=13 how many collisions are occur for the key 10,100,32,45,58,126,
3,29,200,400,0.
Explanation:
Table size is 13 then it is mod 13 values so
10 100 32 45 58 126 3 29 200 400 0
10 9 6 6 6 9 3 3 5 10 0
1 2 3 4 5 collisions
From the above explanation there are 5 collisions occur for the given key.
4. Which of the following is the correct Hash Function to have range from 1 to 23?
(A) x mod 23 (B) x+1 mod 23 (C) x mod 23+1 (D) x(mod 23)+1
Explanation:

1. For (A) H(x)=0 to 22
2. For (B) H(x)=0 to 22
3. For (C) H(x)=0 to 23
4. For (D) H(x)=1 to 23
So from the above steps (D) i

25/09/2011

1. What is the out put of the program for the following input 5 2 * 3 3 2 + * +.

Sol:
5 2 * 3 3 2 + * +

5*2=10 3+2=5
10 3 5 * +

3*5=15
10 15 +

10+15=25
From the above calculations the value of the given expression is 25

2.Calculate the value of the given expression 5 2 * 3 4 + 5 2 * * +.

Sol:
5 2 * 3 4 + 5 2 * * +

5*2=10 3+4=7 5*2=10
10 7 10 * +

7*10=70
10 70 +

10+70=80
From the above calculations the value of the given expression is 80

3. The following postfix expression with single digit operands is evaluated using a stack 8 2 3 ^ / 2 3 * + 5 1 * -

Note that ^ is the exponentiation operator the top two elements of the stack after the first ‘*’ is evaluated are: (2007)

Sol:
8 2 3 ^ / 2 3 * + 5 1 * -

2^3=8 / 2*3=6 + 5*1=5 -
8 8 / 6 + 5 -
8/8=1 6 + 5 -

1+6=7 5 - 7-5=2

After the first ‘*’ operation is evaluated then we get (8/8, 6)= 1, 6 4. Find the value of given prefix expression + - * 2 3 5 / Î 2 3 4

(A) + - * 2 3 5 / Î 2 3 4
2* 3=6 2³=8
+ - 6 5 / 8 4
6-5=1 8/4=2
+ 1 2

1+2=3
The answer is 3.
5. Find the value of given postfix expression 7 2 3 * - 4 Î 9 3 / +

(A) 7 2 3 * - 4 Î 9 3 / +
2*3=6 9/3=3
7 6 - 4 Î 3 +
7-6=1
1 4 Î 3 +
1Î 4=1
1 3 +

1+3=4
The answer is 4.

22/09/2011

Numeric constants :-
Numeric constants are an uninterrupted sequence of digits (and may contain a period). They never contain a comma.
Examples:
123
98.6
1000000

Charecter constants :-

Singular!
One character defined character set.
Surrounded on the single quotation mark.
Examples:
‘A’
‘a’
‘$’
‘4’

String constants :-

A sequence characters surrounded by double quotation marks.
Considered a single item.
Examples:
“UMBC”
“I like ice cream.”
“123”
“CAR”
“car”

Keywords:-

Sometimes called reserved words.
Are defined as a part of the C language.
Can not be used for anything else!
Examples:
int
while
for

20/09/2011

Tokens can be:

* Numeric constants
* Character constants
* String constants
* Keywords
* Names (identifiers)
* Punctuation
* Operators

Want your school to be the top-listed School/college in Secunderabad?

Click here to claim your Sponsored Listing.

Location

Category

Website

Address


Secunderabad
500082