Sona Programming Club

Sona Programming Club

Share

SProC ( Sona Programming Club ) has been started in order to improve the programming skills of the students studying in Sona College of Technology.

07/03/2014

A C program without semicolon
C program to display “India” without using ';' in the whole program:

For writing this program, we should first understand what printf returns and its prototype. The function declaration of printf is
int printf ( const char *, ... ) ;
It takes variable number of arguments, displays the string till a NULL character is found and returns the number of characters
printed.

Now, the program:
# include
void main()
{
if ( printf ( “India” ) )
{
}
}

The printf () returns 5 in this case. Hence, the 'if' condition is true, but, we are not doing anything even if the condition is true.

02/03/2014

A C program without main:
As soon as you see the title, you might be wondered whether this is possible. But, it's actually possible.

# include

# define modify(t, r, a, i, n, s) i # # t # # a # # r
# define change modify(a, n, i, m, a, l)

int change()
{
printf("C without main!\n");
return 0;
}

If you still don't trust this program, compile and execute it. Shocked ? In fact, there is a main in this program, but hidden. This is based on the concept of token pasting. What happens here is, change is initially replaced by modify(a, n, i, m, a, l). The arguments are then replaced as follows
t = a
r = n
a = i
i = m
n = a
s = l

The statement i # # t # # a # # r gets changed to m # # a # # i # # n, which is then concatenated as main. Eventually, 'change' is changed to 'main'.

26/02/2012

Maximum and minimum:
Problem:
Write a C program to find the largest and smallest value in the given numbers.

Input:
First line contains the number of inputs, n.
After that 'n' lines follow each representing a number.

Output:
Largest and smallest values in the given set of numbers separated by a space.

Constrains:
1 < n < 20000
Each value ranges between 0 and 10000 inclusive.

Sample input:
5
3
5
0
4
1

Sample output:
5 0

Note:
Time and space complexities will not be considered, however, the best ( most efficient ) program will get the best prize.

Deadline to solve:
March 31, 2012

Contact:
For any doubts or queries, just comment.

16/02/2012

Date of post: 16.02.2012
Category: Questions
Question number: 3
Post content:
Sona College of Technology
Programming Club ( SProC )
“Let us C beyond the C”
Answers for questions asked on 15.2.12:
1. b) 5 6
2. a) float should be changed to int
b) a[2] should be changed to a[3]
c) “,%d” should be added in printf
3. Only s[strlen(s)] should be changed to s[strlen(s) – 1]
s[0] is internally interpreted as *(s + 0) [which means take the value at the location after moving 0 positions from the base address s]. By the commutative property of addition, s + 0 = 0 + s. Therefore *(s+0) = *(0+s) which is also equal to 0[s].
Hence, s[0] = *(s+0) = *(0+s) = 0[s].
4. Garbage value
5. b) ,H ( Null character is stored at the 8th position )

Questions for 16.2.12 ( input and output statements ):
1 Output of the program will be: (1)
main() { int a = 40 ; printf("%d %d", a ); }
a. 40, 40 b. 40, (garbage) c. (garbage), 40 d.
______________________________________________________________
2 What is the Output of the code: (1)
main() { int i; printf("%d", &i)+1; }
a. address of i b. address of i + 1. c. Compile error d. None of the above
______________________________________________________________
3 Output of the code will be: (2)
main() { int i = 100, j = 50 ; printf("%d", i, j ); }
a. 100 b. 50 c. 100 50 d.
______________________________________________________________
4 Output of the program will be: (1)
main() { int i=0, j=0; scanf("%d"+scanf("%d %d", &i, &j));
printf("%d %d", i, j); }
a. Runtime error b. 0, 0 c. Compile error d. Input values
______________________________________________________________
5 How will you print % character? (2)
a. printf(“\%”) b. printf(“\\%”) c. printf(“%%”) d. printf(“\%%”)
______________________________________________________________

Note:
1. Some of these questions may be useful for your internal exams
2. Those who answer todays questions will be getting bonus credits.
3. Send your feedback and suggestions to [email protected]

Tips:
1. Always try to write programs with indentation.
2. Try to compile programs with gcc compiler which is the current standard.
3. Have meaningful variable names so that anyone can understand the purpose of the variable by just looking at its name. Eg: instead of a, b or s, use val1, val2 or sum.
Happy programming !

Want your school to be the top-listed School/college in Salem?
Click here to claim your Sponsored Listing.

Website

Address


Salem
636005