C Programming - Functions - Discussion

Discussion Forum : Functions - General Questions (Q.No. 2)
2.
What is the notation for following functions?
1.  int f(int a, float b)
    {
        /* Some code */
    }

2.  int f(a, b)
    int a; float b;
    {
        /* Some code */
    }
1. KR Notation
2. ANSI Notation
1. Pre ANSI C Notation
2. KR Notation
1. ANSI Notation
2. KR Notation
1. ANSI Notation
2. Pre ANSI Notation
Answer: Option
Explanation:
KR Notation means Kernighan and Ritche Notation.
Discussion:
59 comments Page 6 of 6.

Sabari said:   7 years ago
Which one is correct? Please explain the correct answer.

Mahesh Gawali said:   7 years ago
@Rahul.

When the string value is directly assigned to a pointer, in most of the compilers, it's stored in a read-only block (generally in data segment) that is shared among functions.

char *p = "Hai Friends";
In the above line "Hai Friends" is stored in a shared read-only location, but pointer p is stored in a read-write memory. You can change p to point something else but cannot change value at present p. So this kind of string should only be used when we don't want to modify string at a later stage in the program.

In your program #include<stdio.h> Header file missing,
3rd line after literal string constant Hai Friends Semicolon (; ) Missing ,
you write *p1; which gives error compile time as "Undefined p1 "
You can't write this ++*p++;
case 1 : ++*p; not allowed before printf() it gives segmentation fault.
case 2: But *p++ allowed before printf () which is correct but 1st letter "H" will not display.
case 3: ++*p; will allowed after printf() which is quit correct way to written program.
case 4: *p++ allowed after printf () which is mostly recommended.

You have to write program like this
#include<stdio.h>
int main()
{
char *p="hai friends";
char *p1;
p1=p;

while(*p!='\0')
{
printf("%s %s",p,p1);
*p++;
}
}

Satya said:   7 years ago
Here, What is the difference between option A & C?

NITIN said:   7 years ago
Please, anyone, explain the notations with proper examples.

Sachin said:   7 years ago
What is Pre ANSI notation?

Harish Mahajan said:   7 years ago
What is KR, pre-ANSI notation?

PVB said:   5 years ago
Thanks @Mahendra.

Jenisha Roshan said:   5 years ago
K&R C is an old de facto standard for the C programming language. It was established when the C language creators, Kernighan and Ritchie, wrote the definitive book on the language.

ANSI C is another standard that was created when C got popular enough, to establish a real standard (created by an actual standards institute) and also to improve upon the language. You might also hear this standard referred to as C89, since it was published in 1989. Newer standards such as C99 and C11 also exist today, but they are all much more similar to each other (and to C89) than to the original K&R C.

One huge reason for this is a very important change in the way functions are declared. In K&R C, for instance, if you want to write a function that takes in two arguments, one of type int and one of type double, and returns nothing, you would write

void foo(a, b)
int a;
double b;
{
// implementation
}
Meanwhile, in any standard C89 or later, you would write

void foo(int a, double b)
{
// implementation
}
This second style is known as the function prototype.

The first style is useful and maybe interesting to know if you are reading really old code, but you should never ever use it yourself. Your compiler will likely yell at you, and even if it doesn't, functions written in the old style are not subject to the same type-checking rules as the function prototype.

Raghav said:   4 years ago
K&R C is an old de facto standard for the C programming language. It was established when the C language creators, Kernighan and Ritchie, wrote the definitive book on the language.

ANSI C is another standard that was created when C got popular enough, to establish a real standard (created by an actual standards institute) and also to improve upon the language. You might also hear this standard referred to as C89, since it was published in 1989. Newer standards such as C99 and C11 also exist today, but they are all much more similar to each other (and to C89) than to the original K&R C.

One huge reason for this is a very important change in the way functions are declared. In K&R C, for instance, if you want to write a function that takes in two arguments, one of type int and one of type double, and returns nothing, you would write;


void foo(a, b)
int a;
double b;
{
// implementation
}
Meanwhile, in any standard C89 or later, you would write

void foo(int a, double b)
{
// implementation
}

This second style is known as the function prototype.

The first style is useful and maybe interesting to know if you are reading really old code, but you should never ever use it yourself. Your compiler will likely yell at you, and even if it doesn't, functions written in the old style are not subject to the same type-checking rules as the function prototype.
(2)


Post your comments here:

Your comments will be displayed after verification.