Question.61 (main function), Question.62 (define vs declare),Question.63 (formal vs actual arguments) test whether the candidate knows basics of C programming. Question.64 (getchar vs scanf) tests basics of io. Question.65 (break vs continue) tests whether candidate knows the subtle differences using ‘break’ and ‘continue’ in loops.
- What is the purpose of main() function?
- What is the difference between declaring a variable and defining a variable?
- What are the differences between formal arguments and actual arguments?
- What is difference between getchar and scanf functions for reading strings?
- What is difference between ‘break’ and ‘continue’ statements?
- What is the purpose of main() function? In C, program execution starts from the main() function. Every C program must contain a main() function. The main function may contain any number of statements. These statements are executed sequentially in the order which they are written.
- What is the difference between declaring a variable and defining a variable?
- What are the differences between formal arguments and actual arguments?
- What is the difference between getchar and scanf functions for reading strings?
- What is the difference between ‘break’ and ‘continue’ statements?
The main function can in-turn call other functions. When main calls a function,it passes the execution control to that function. The function returns control to main when a return statement is executed or when end of function is reached.
In C, the function prototype of the ‘main’ is one of the following:
int main(); //main with no arguments
int main(int argc, char *argv[]); //main with arguments
The parameters argc and argv respectively give the number and value of the program’s command-line arguments.
Example:
#include <stdio.h>/* program section begins here */
int main() {// opening brace - program execution starts here
printf("Welcome to the world of C");
return 0;
}// closing brace - program terminates here
Output:
Welcome to the world of C.
Back to top
Declaration of a variable in C hints the compiler about the type and size of the variable in compile time. Similarly, declaration of a function hints about type and size of function parameters. No space is reserved in memory for any variable in case of declaration.
Example: int a;
Here variable ‘a’ is declared of data type ‘int’
Defining a variable means declaring it and also allocating space to hold it.
We can say “Definition = Declaration + Space reservation”.
Example: int a = 10;
Here variable “a” is described as an int to the compiler and memory is allocated to hold value 10.
Back to top
Argument:An argument is an expression which is passed to a function by its caller (or macro by its invoker) in order for the function(or macro) to perform its task. It is an expression in the comma-separated list bound by the parentheses in a function call expression.
Actual arguments:
The arguments that are passed in a function call are called actual arguments. These arguments are defined in the calling function.
Formal arguments:
The formal arguments are the parameters/arguments in a function declaration. The scope of formal arguments is local to the function definition in which they are used. Formal arguments belong to the called function. Formal arguments area copy of the actual arguments. A change in formal arguments would not be reflected in the actual arguments.
Example:
#include <stdio.h>
void sum(int i, int j, int k);/* calling function */
int main() {
int a = 5; // actual arguments
sum(3, 2 * a, a);
return 0;
} /* called function */
/* formal arguments*/
void sum(int i, int j, int k) {
int s;
s = i + j + k;
printf("sum is %d", s);
}
Here 3, 2*a, a are actual arguments and i,j,k are formal arguments. Read more about actual arguments vs formal arguments.
Back to top
Differences between getchar and scanf functions for reading strings:
| scanf | getchar |
|---|---|
| 1. Entering of each character should be followed by return key. | 1. Need not type return key. |
| 2. Continuous stream of characters cannot be directly supplied using scanf function. | 2. Continuous stream of characters can be directly supplied using getchar function |
| 3. Scanf function can be used to provide data at execution time irrespective of its type(int, char, float). Example: #include<stdio.h>
int main() {
char a[10];
printf("Enter a: \n");
scanf("%s",a);
return 0;
}
|
3. getchar() function is used only with character type. Example: #include<stdio.h>
int main() {
char a;
printf("Enter any character: \n");
a = getchar();
printf("Character entered:%c \n",a);
return 0;
}
|
| 4. scanf() returns the number of items read successfully. A return value 0 indicates that no fields were read. EOF(end of file) is returned in case of an error or if end-of-file/end-of-string character is encountered. | 4. getchar() returns the character entered as the value of the function. It returns EOF in case of an error. It is recommeded to use getchar instead of scanf. |
Differences between ‘break’ and ‘continue’ statements:
| break | continue |
|---|---|
| 1. break is a keyword used to terminate the loop or exit from the block.The control jumps to next statement after the loop or block. | 1. continue is a keyword used for skipping the current iteration and go to next iteration of the loop |
2.Syntax:
{
Statement 1;
Statement 2;
Statement n;
break;
}
|
2.Syntax:
{
Statement 1;
continue;
Statement 2;
}
|
| 3. break can be used with for, while, do- while, and switch statements.When break is used in nested loops i.e. within the inner most loop then only the innermost loop is terminated. | 3. This statement when occurs in a loop does not terminate it but skips the statements after this continue statement. The control goes to the next iteration. continue can be used with for, while and do-while. |
4. Example:
i = 1, j = 0;
while(i<=5){
i=i+1;
if(i== 2)
break;
j=j+1;
}
|
4. Example:
i = 1, j = 0;
while(i<=5){
i=i+1;
if(i== 2)
continue;
j=j+1;
}
|
| Previous | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 |