What is the purpose of the gate2011 Program in the C programming language?

In summary, the code snippet provided is using pointer arithmetic to print the string "2011" by finding the address of the fourth character in the string and adding it to the address of the string itself. This is made possible by the fact that in C++, arrays and pointers are equivalent, allowing for simple manipulation of memory addresses.
  • #1
sam topper.
14
0
#include<stdio.h>
void main()
{
char c[]="gate2011";
char *p=c;
printf("%s",p+p[3]-p[1]);
}
 
Technology news on Phys.org
  • #2
Doesn't look like rocket science. What do you think will happen? What actually happens when you run it?
 
  • #3
It apparently takes the adress of the string + 65-61= adr + 4. Now I don't know but this seems not aligned, but it means maybe adr + 4 bytes, hence it maybe prints '2011', but I have not checked this for real.
 
Last edited:
  • #4
The trick is the way that pointers and arrays are equivalent in C++.
If you have an array
Code:
int[] a;
then a is a pointer to an int, and &a[4] and *(a + 4) both point to address of the 5th array element.
So the pointer p points at the beginning of the string, p[4] - p[1] is the length of the memory block used by the first four characters and the sum is a pointer to the place in memory where the '2' is stored. Since strings are terminated by a null (\0) the statement is equivalent to
Code:
    char p[] = { '2', '0', '1', '1', '\0' };
    printf("%s", p);

You could actually reach the same thing through
Code:
#include<stdio.h>
void main()
{
    char c[]="gate2011";
    // No need to introduce a new name, c is already a char*
    // char* p = c; 
    printf("%s", (c + 4));
}
 
  • Like
Likes 1 person
  • #5


The "gate2011" Program is a program written in the C programming language that utilizes the concept of pointer arithmetic to print out a specific string. The program first declares a character array named "c" with the value "gate2011". Then, a pointer variable "p" is assigned to point to the first element of the array. Using pointer arithmetic, the program accesses the elements of the array and prints out the string "gate2011" by adding the value of the third element of the array (which is 'e') to the address of the pointer and subtracting the value of the second element of the array (which is 'a'). This results in the pointer pointing to the fourth element of the array, which is 't'. This clever use of pointer arithmetic allows the program to print out the desired string without explicitly using the string itself.
 

FAQ: What is the purpose of the gate2011 Program in the C programming language?

What is "gate2011" Program?

"gate2011" Program stands for Graduate Aptitude Test in Engineering 2011. It is an entrance examination conducted by the Indian Institute of Technology (IIT) and Indian Institute of Science (IISc) for admission to postgraduate courses in engineering, technology, and architecture in various institutes in India.

Who can apply for the "gate2011" Program?

Students who have completed or are in their final year of Bachelor's degree in engineering, technology, or architecture can apply for the "gate2011" Program. There is no age limit to apply for this exam.

What is the exam format for "gate2011" Program?

The "gate2011" Program is a computer-based test and consists of 65 questions with a total duration of 3 hours. The question paper will have both multiple-choice questions (MCQs) and numerical answer type (NAT) questions. There is negative marking for wrong answers in MCQs, but no negative marking for NAT questions.

What is the eligibility criteria for "gate2011" Program?

The eligibility criteria for "gate2011" Program varies depending on the subject chosen by the candidate. Generally, candidates must have a Bachelor's degree in engineering, technology, or architecture or a Master's degree in Science, Mathematics, Statistics, or Computer Applications. Candidates in their final year of these courses are also eligible to apply.

How can I prepare for the "gate2011" Program?

To prepare for the "gate2011" Program, candidates can refer to previous years' question papers, study materials, and online mock tests. It is also recommended to have a thorough understanding of the core concepts and to practice regularly. Time management is also crucial for success in this exam.

Back
Top