3

I am very new to programming. Now I am learning C. Please look at this following code snippet -

printf("StudentId: %d CGPA: %f", id, cgpa); 
printf("Name: %s StudentId: %d CGPA: %f", name, id, cgpa);   

I know function takes argument. So I think, the both the first and second printf() methods takes only two arguments -

1. first argument inside the double quote that is - "StudentId: %d CGPA: %f" or "Name: %s StudentId: %d CGPA: %f"
2. and second one is outside the quote that is - id, cgpa or name, id, cgpa

Now my question is - am I right while I'm thinking that prinf() methods takes only two arguments no matter how many variable (that is id, cgpa, name or even department) are placed after the double quotes?

Or If printf() method takes multiple argument then how it is handled in C?

0

5 Answers 5

4

No, it's not correct to say that printf always takes 2 arguments. In your first case, it takes 3 arguments. In the second case, it takes 4 arguments.

printf is a variadic function. It takes a variable number of arguments. In C, functionality for such functions is provided using the stdarg.h (or varargs.h) header file.

Sign up to request clarification or add additional context in comments.

Comments

4

Printf can take as many arguments as you want.

In the man page you can see a ... at the end, which stands for a var args.

If you got 96 times %s in your first argument, you'll have 97 arguments (The first string + the 96 replaced strings ;) )

Comments

2

printf can take any number of inputs. This is what printf prototype looks like:

int printf ( const char * format, ... );

As you can see, ... is an indication of variable number of arguments.

An example is:

 printf("%i %d %f %c %s", int_var, int_var, float_var, char_var, string_var);

These are format specifiers: %i, %d, %f, %c, %s, and these correspond to the variables in order: int_var, int_var, float_var, char_var, string_var

Comments

2

NO it takes variable number of arguments.

int printf(const char *format, ...) takes variable no of arguments

format − This is the string that contains the text to be written to stdout. It can optionally contain embedded format tags that are replaced by the values specified in subsequent additional arguments and formatted as requested.

The printf function uses its first argument to determine how many arguments will follow and of what types they are. If you don’t use enough arguments or if they are of the wrong type than printf will get confuses, with as a result wrong answers.

And rest of the arguments are the variables for the format tags that you gave in the first argument (as a string).

Pleas read here

Comments

1

Please Look:

printf("StudentId: %d CGPA: %f", id, cgpa); //3 arguments 
printf("Name: %s StudentId: %d CGPA: %f", name, id, cgpa); // 4 arguments 

The printf() can takes variable length of arguments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.