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 1 of 6.

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)

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.

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++;
}
}

Mahmoud said:   10 years ago
Write a compete C program that prompts the user to enter an observed boiling point of a substance.

In °C (Celsius), then the program identifies the substance depending on its observed boiling point within the error rate er% lower or higher of the expected boiling point.

If the data input (observed boiling point) is not close within er% higher or lower than any of the boiling points in the table below, the program should output the message unknown substance.

Your program also should define and call a function called within_ percent that takes three. Variables as input arguments and return an integer value as an output.

int within_percent (int reference, int data, double percent_val).

This function works as follows:

It returns 1 (true) if data is within percent_val % of reference.

In other words:

(reference " percent% * reference) % data % (reference + percent % * reference).

Otherwise the function return zero (false).

For example: Calling within_percent (357, 352, 7.5) would return 1 (true), since 7.5%.

Of 357 is 26.7 and 352 falls between 330.22 and 383.7.

Substance Normal boiling point (°C) Error rate (er%).

Water 100 5.1.

Mercury 357 7.5.

Copper 1187 6.2.

Silver 2193 6.9.

Gold 2660 5.3.

Hareesh said:   9 years ago
One of the aims of the ANSI C standardization process was to produce a superset of K&R C (the first published standard) incorporating many of the unofficial features subsequently introduced. However, the standards committee also included several new features, such as function prototypes (borrowed from the C++ programming language), and a more capable preprocessor. The syntax for parameter declarations was also changed to reflect the C++ style.

Mahender said:   1 decade ago
In KR notation first we need to give prototype declaration then function call then comes function definition where as in ASCII notation 1st we need to define the the function defination and then function call.

Ex: KR notation:
---------------------
void display(__);
main()
{
display();
}
void display()
{
}

ASCII notation:
-------------------
void display(__)
{
}
main()
{
display();
}

Bhanu said:   1 decade ago
Ex: KR notation:
---------------------
void display(__);
main()
{
display();
}
void display()
{
}

ASCII notation:
-------------------
void display(__)
{
}
main()
{
display();

Pre-ANCI Notation:
------------------

1)Main(net, err)
int *net;
double *err;
{

}

N.Muthu Kumar said:   1 decade ago
There are many standard notations there like Ansi, Pre-Ansi, KR notations. These are nothing but, the specification or representations of methods in a standard format developed at MIT's. That's it. C compiler supports both of these.

K.Chaitanya said:   1 decade ago
ANSI notation means that must be initialized in biteen ().Like fun(int a,float b).But,
In the times of Dennis Ritche ,he initialized out side of ().Like
fun();
int a;float b;

Rahul said:   1 decade ago
main()
{
char *p="hai friends",
*p1;
p1=p;
while(*p!='\0') ++*p++;
printf("%s %s",p,p1);
}

What will be the o/p explain with steps please?


Post your comments here:

Your comments will be displayed after verification.