1) A Variable name consists of any combination of alphabets, digits and underscores. Some compiler allows variable names whole length could be up to 247 characters. Still it would be safer to stick to the rule of 31 characters. 2) The first character of the variable name must either be alphabet or underscore. It should not start with the digit.3) No commas and blanks are allowed in the variable name.4) No special symbols other than underscore are allowed in the variable name.
We need to declare the type of the variable name before making use of that name in the program. Type declaration can be done as follows:
To declare a variable as integer, follow the below syntax:
int variable_name;
Here int is the type of the variable named variable_name. ‘int’ denotes integer type.
Following are the examples of type declaration statements:
E.g.: int p, n;
float r; Q9) Explain the comments in C program? A well-documented program is a good practice as a programmer. It makes a program more readable and error finding become easier. One important part of good documentation is Comments.
float rs, grosssal ;
char name, code ;
float a = 1.5, b = 1.99 + 2.4 * 1.44 ;
is same as
int j = 25, i = 10 ;However,float a = 1.5, b = a + 3.1 ;
is alright, but
float b = a + 3.1, a = 1.5 ;is not. This is because here we are trying to use a even before defining it.
a = b = c = 10 ;However, the following statement would not work.int a = b = c = d = 10 ;Once again we are trying to use b (to assign to a) before defining it.Q11) Explain type conversions in assignment? Type conversions depend on the specified operator and the type of the operand or operators. Type conversions are performed in the following cases:
1 2 3 4 5 6 7 8 9 10 11 12 | #include<stdio.h>
void main() { int j=32; char cha='p'; float a=123.2; char str[20]; sprintf(str,"%d %c %f",j,cha,a); printf("%sn",str);
} |
sscanf() is the counter part of sprintf() function. It allows the programmer to store the characters of string in some other variable. These two functions are used very rarely in C. Q17) Explain unformatted console I/O operation? Unformatted Input/Output Functions in CFunctions like getch(), getche() and getchar() comes under this category. These functions store only one character. Till now we have used scanf() function to store values. Unfortunately we have to press enter key while using scanf() function to store the values in memory. In a condition when we have to store only one character these unformatted function comes very handy.The header file used for these three functions is conio.h.getch() functionThis function is used to store only one character in memory. It does not echo or display that character on the screen while program execution.getche() functionThis function works similar to getch function. However it just echo or display that character on screen.getchar() functionThis function works entirely similar to getche function. It stores one character and display it on the screen. But we have to press the enter key to store one character while using this function.Consider below example to understand these functions.
1 2 3 4 5 6 7 8 9 10 11 12 13 | #include<stdio.h> #include<conio.h>
void main() { char ch1,ch2,ch3; ch1=getch(); // it does not echo the character on screen ch2=getche(); //echo's character on screen ch3=getchar(); // Use Enter to store the value
printf("%c %c %cn",ch1,ch2,ch3);
} |
Output
if (condition)
statement1;
statement2;
In the above syntax, “if” is the keyword and condition in parentheses must evaluate to true or false. If the condition is satisfied (true) then compiler will execute statement1 and then statement2. If the condition is not satisfied (false) then compiler will skip statement1 and directly execute statement2. Q19) Explain the use of logical operator ?Sr. No. | Operator | Description | Example |
1. | && | This is logical AND, it returns true when both the values are non zero | Result of (A&&B) is true |
2. | || | This is logical OR, it returns true when any of two value is non zero | Result of (A||B) is true |
3. | ! | This is logical NOT operator, it is used to reverse the logical state of operand | Result of !(A&&B) is false |
{
Statement 1
Statement 2 and so on
}else if (condition)
{
Statement 1
Statement 2 and so on
}else if (condition)
{
Statement 1
Statement 2 and so on
}else
{
Statement 1
Statememt 2 and so on
}