Fundamentals of Computer Programming - Old Questions
3. Define printf() function, header file and main function. Find the value of following expression. Use the value initially assigned to the variables for each expressions. [4.5+1.5]
int a = 8, b = 5;
float x = 0.005, y = -0.01;
1) 2 * ((a / 5) + 4 * (b – 3)) % (a + b - 2));
2) (x > y) && (a > 0) || (b < 5);
3) (a > b)?a:b;
Answer
AI is thinking...
printf() function
printf is an output function which has been defined in stdio.h file. What ever is put inside the function printf between two double quotes is printed on the screen.
E.g. printf(“Welcome to C programming”);
Output: Welcome to C programming
Header file
A header file is a file with extension .h which contains C function declarations and macro definitions to be shared between several source files. There are two types of header files: the files that the programmer writes and the files that comes with your compiler.
You request to use a header file in your program by including it with the C preprocessing directive #include, like you have seen inclusion of stdio.h header file, which comes along with your compiler.
main function
Every C-programs needs to have the main function. Each main function contains 2 parts.
Declaration part: The declaration part declares all the variables used in the executable part.
Executable part: There is at least one statement in the executable part. These two parts must appear between the opening and closing braces. The program execution begins at the opening brace and ends at the closing brace. The closing brace of the main function is the logical end of the program.
E.g.
int main( )
{
int a=10;
printf(“%d”, a);
return 0;
}
Now,
Given,
int a = 8, b = 5;
float x = 0.005, y = -0.01;
1) 2 * ((a / 5) + 4 * (b – 3)) % (a + b - 2));
= 2*((8/5)+4*(5-3))%(8+5-2)
= 2*(1+4*2)%11
= 2*9 % 11
= 18 % 11
= 7
2) (x > y) && (a > 0) || (b < 5);
= (0.005>-0.01)&&(8>0)||(5<5)
= (1)&&(1)||(0)
= 1|| 0
= 1
3) (a > b)?a:b;
= (8>5)?8:5;
= 8 [Using ternary principle)