You are on page 1of 21

Chapter 2 C Programming Basics

Questions:
1. List any four applications of c language.

2. Write any four important features of c language.


Ans:

Features of c-language

1. Modularity(Structure Oriented or Function oriented)

2. Extendability

3. Portability

4. Robustness

5. Flexibility

6. Fast execution (speed)

7. Reusability

3. Write the character set of c programming language.


4. List C Tokens.
5. Describe the different types of tokens used in c.

6. List the various types of data in c and give examples.

7. What are the constants? What are the different types of constants inc?
Ans: Constants:If we declare any variable as a constant the value of the variable will not be changed
or modified

Syntax: Data type const variable=value;

8. (1) List and explain the four basic types of constants in c. (2) List the basic data types available in
c language and give example for each.
Ans:Data type

9. What is the character constant? How do characters constant differ from numeric constants? Do
character constants represent numeric values?

10. What is a string constant? How do string constants differ from character constants? Do string
constants represent numeric values?

11. What is a key word? List at least four key words in c.


Ans:
Keywords: At the time of designing the c-language some words are used to do a specific task called
keywords. There are 32 keywords are available in c-language.

EX: int,float,char,double,unsigned,auto,const,while,if,do,else,static,long,
short, register signed, default, static, for,break,continue,goto, volatile , extern etc..,
12. What is a variable? What are the rules for naming a variable? (Or) what are the rules to be
observed in naming an identifier in c?

Ans: 1. Variables:An variable is an identifier(user defined word) It is used to store the data the variable
can stores one value

Syntax: datatype variable;

Ex:

int a;

a=10;

Rules for the Identifier :

1.It should starts with alphabetical letters only

2.It should not starts with numerical values, special symbols.

3.It should not exit more than 32 characters.

4. Keywords are not used as variables.

14. What are the different types of variable? What are the rules for naming a variable?
rules for the variable are

1.It should starts with alphabetical letters only

2.It should not starts with numerical values, special values.

3.It should not exit more than 32 characters.

4. Keywords are not used as variables.

15. Explain the declaration of variables in detail.(Or) what is the purpose of type declaration?

16. (1) List and explain the four basic (primary) data types in c.(Or) name the different fundamental
data types that C supports and explain them in detail.(2) explain different data types in c-language with
examples. How are constants declared in c- language?

Ans: (a): Basic Data types or Fundamental Data types or Primitive Data types are:

Name of Size Range


datatype
int 2 32, 767 to
+32,767

float 4 7.8390 with


6decimal

char 1 -128 to +127

double 8 3.123456789123

long int 4 -2147483648 to


+21147483648

long double 10 3.4E-4932 to


1.1E+4932

The Constants are declare as below:


Syntax: const datatype variable=value;
const int a=10;

17. Classify data kinds in c and explain the primary data types in c with examples. (Or) discuss in detail
about various kinds of data manipulated by a computer program.

18. List the arithmetic operators in c.


Ans:

operators meanings

+ Addition

- Subtraction

* Multiplication

% Percentage

division

19 operators and Expressions


19. What is an Operators? Describe several different types of operators included in c.

20. List the arithmetic operators in c (Or) Explain different arithmetic operations in C programming
language

21. What is meant by operator precedence and associativity of an operator?


Ans:
For operators, associativity means that when the same operator appears in a row, then to which
direction the evaluation binds to. In the following,
Usually + and - have the same precedence. Consider the expression 7 4 + 2. The result could be
either (7 4) + 2 = 5 or 7 (4 + 2) = 1. The former result corresponds to the case when + and are left-
associative, the latter to when + and - are right-associative.

22. What is meant by operator precedence and state the hierarchy of arithmetic operators?

23. What is the precedence of relational operators in C language?

24. Differentiate between = and== in C language.

= ==

1. It is a assignment 1.it is a relational operator


operator

2. It is is used to assign the 2. It is used to compare the


value value to the left hand side
variable.
to the variable
Eg : int a=5;
Eg: int a=10;
a=10;

3.The value 10 is string into 3.The value 10 is compare to


the variable a the left hand side variable.

25. (1) List and state the hierarchy of logical operators.(2) explain the logical operators in c language
and their precedence.

26. What is the modulus operator and how does it work in C?

27. What is meant by assosiativity? What is the assosiativity of the arithmetic, relational and logical
operators?

28. Define an expression and show how to evaluate an expression with an example.

29. With the help of an example explain how a logical expression is evaluated.

30. Name and describe relational, logical and assignment operators.

31. Summarize the standard escape sequences in C.describe them.(or) what is an escape sequence?
What is its purpose? List the standard escape sequences in C.
Ans: Escape sequences is consider as character motion . it is also known as back slash character

Escape sequences Meaning

/n Next line

/t Tabular space

/b Back space
/a alarm (beap sound)

/0 Null character

32. List assignment or compound operators in C.

33. Explain any four assignment operators with examples.

34. What is a unary operator? List the various unary operators in C.

35. What are the commonly used input/output functions in C? How are they accessed? (Or) explain
input and output in C language with examples.

(or)

List and give the syntax of I/O statements used in C.


Ans:Input output statements in c

Input and output functions are divided into two parts

1. Formatted functions

2. Unformatted functions

Again formatted and unformatted functions are divided into few types

They are:

Formatted functions:

printf()

scanf()

Unformatted functions:

putchar()

getchar()

puts()

gets()

37. Describe the formatted input and output functions.

Ans:
printf(): It is used to output function. It is used to display the message or output on the monitor

It is a call by value function. It is predefined in <stdio.h>

Syntax: printf(control string, variable);


printf(message);

scanf(): it is used to a input function. It is used to the data from the keyboard at run time.

It is also predefined in <stdio.h>

Syntax:

scanf (control,&var1,&var2);

Eg:

scanf (%d%d,&a,&b);

It is call by address function.

The value or data read from the keyboard and converts by using the format specifiers and stores into
variable address.

38. List the commonly used math library functions in C.

39. Explain the usage of comma operator. (Or) what is comma operator? Explain its syntax with
suitable example.

40. Explain the pre increment and post increment operators inc with examples.

41. (1) Explain increment and decrement operators in C language with examples. (Or) illustrate the
usage of increment and decrement operators in detail. (2) Differentiate between pre
increment/decrement operators and post increment/decrement operators inc.

Ans:increment and decrement operators

42. (1) State the importance of (a) increment and decrement operators (b) conditional operators.

43. List the various type conversion techniques used in C and explain them in brief.

44. Distinguish between getch (), and getchar () functions with suitable examples.

45. Explain the following (1) increment and decrement operators (2) assignment operators (3)
arithmetic operators (4) relational operators.

46. What is conditional (ternary) operator? Explain with an example.

47. precedence of operators in C


Ans:
48. Explain the importance of a C Preprocessor Directives

49. Explain the pre processor features(1)#include(2)#define

50. Describe the use of the conditional operator to form conditional expressions.
51. (1) Explain the functions printf() and scanf(). (Or) explain the syntax
of scanf() () and printf() functions. (2) Explain the role of scanf() and printf() functions.

52. How comments can be included within a C program?

53. Why is the main () function special in a C program?

54. List any four standard headers that can be included in c.

55. Explain the Structure of a C Program in detail.

56. What is a null statement in C language?

57. Explain the null statement and coma operator.

58. Which of the following are invalid variable names and why?

(1) Profit (14) counter-3

(2) Last name (15) annual report

(3) 2002INCOME (16) TOTAL-REVENUE

(4) dist. (17) engg.college

(5) Float (18) last-named

(6) #tax (19) -count

(7) Stud add (20) $balance

(8) 2nd_strret (21) new-word

(9) 123

(10) Short

(11) Hyderabad

(12) Count

(13) Int_rate

59. Find errors, if any, in the following declaration statements

Int fact

Float=x, y,z;

Long int, a;

Char ch;d;

Large area;
Long float root1,root2;

60. Convert the following equations into corresponding C statements

Ans:
(v): -b+sqrt b*b-4*a*c/2*a

-b-sqrt b*b-4*a*c/2*a

61. Evaluate the expression (A/B-C+A%B) for A=20, B=4, C=2 by following operator hierarchy.

62. Write a program to ready any two floating-point numbers from the keyboard and to display their
addition, subtraction, multiplication and division.

63. (i) Write a C program to accept five real numbers from the keyboard and to find out their sum and
average. (ii) Write a C program to calculate average of three numbers.

64. Write a C program to calculate the total marks and the percentage marks obtained by the student
in five subjects. Assume that the maximum marks that can be obtained by the student in each subject
is100.

65. Write a program to calculate the perimeter and area of a rectangle.

66. Write program that calculate area of the triangle given the base and the height.

67. Two numbers are input through the keyboard into two locations Cand D .write a program to
interchange the contents of C and D.
68. Write a c program to convert temperature degrees into centigrade to degrees Fahrenheit.

69. Write a program that calculates the volume and surface area of the sphere, given the radius.

70. Write a C program to read an integer number of days and convert it into months and days.

71. Write a program to calculate the simple interest and compound interest for a given principle (p),
rate of interest(r), time (t) and compounding per year (n) using the formulas

72. Write a program that demonstrate the difference between predecrementing and post
decrementing using the decrement operator .

73. Write a C program to find the largest of two numbers using conditional operator.

74. Write a C program to print the following.

***

*****

*******

*********

75. Write a program to print the word C PROGRAM in the following manner.

CP

CPR

CPRO

CPROG

CPROGR

CPROGRA

CPROGRAM
--------------------------------------------------------------------------------------------------------

Interview Questions

C is a structural or high level or middle level language which one is correct answer

What is the difference between #include <file> and #include ?file??

how to create love flames program using c language

Can a file other than a .h file be included with #include?

How can you avoid including a header more than once?

Is it possible to execute code even after the program exits the main() function?

What is the difference between NULL and NUL?

Write the equivalent expression for x%8?

How are portions of a program disabled in demo versions?

Is there anything you can do in C++ that you cannot do in C ?

What will be printed as the result of the operation below: main() { char *ptr = ? Cisco Systems?; *ptr++;
printf(?%sn?,ptr); ptr++; printf(?%sn?,ptr); }

Is it possible to print graphics output in C?

What is the benefit of using #define to declare a constant ?

How to write a C program for displaying a sentence without output command?

What are the different storage classes in C ?

What is the use of semicolon at the end of every statement?

Difference between :- 1) C and C++ 2) main() in c and main() in c++ ?

How can we use data connectivity in 'c' language?

The following variable is available in file1.c, who can access it?: static int average;

1. What are the different data types available in C?


There are four basic data types available in C.
1. int
2. float
3. char
4. double
2. What are Keywords?
Keywords are certain reserved words that have standard and pre-defined meaning in
C. These keywords can be used only for their intended purpose.

3. What is an Operator and Operand?


An operator is a symbol that specifies an operation to be performed on operands.
Example: *, +, -, / are called arithmetic operators.
The data items that operators act upon are called operands.
Example: a+b; In this statement a and b are called operands.

4. What is Ternary operators or Conditional operators?


Ternary operators is a conditional operator with symbols ? and :
Syntax: variable = exp1 ? exp2 : exp3
If the exp1 is true variable takes value of exp2. If the exp2 is false, variable takes
the value of exp3.

5. What are the Bitwise operators available in C?


& - Bitwise AND
| - Bitwise OR
~ - Ones Complement
>> - Right shift
<< - Left shift
^ - Bitwise XOR are called bit field operators
Example: k=~j; where ~ take ones complement of j and the result is stored in k.

6. What are the logical operators available in C?


The logical operators available in C are
&& - Logical AND
|| - Logical OR
! - Logical NOT

7. What is the difference between Logical AND and Bitwise AND?


Logical AND (&&): Only used in conjunction with two expressions, to test more
than one condition. If both the conditions are true the returns 1. If false then return
0.
AND (&): Only used in Bitwise manipulation. It is a unary operator.
8. What is the difference between = and == operator?
Where = is an assignment operator and == is a relational operator.
Example:
while (i=5) is an infinite loop because it is a non zero value and while (i==5) is true
only when i=5.

9. What is type casting?


Type casting is the process of converting the value of an expression to a particular
data type.
Example:
int x,y;
c = (float) x/y; where a and y are defined as integers. Then the result of x/y is
converted into float.

10. What is conversion specification?


The conversion specifications are used to accept or display the data using the
INPUT/OUTPUT statements.

11. What is the difference between a and a?


a is a character constant and a is a string.

12. What is the difference between if and while statement?


if while
(i) It is a conditional statement (i) It is a loop control statement
(ii) If the condition is true, it executes (ii) Executes the statements within the
some statements. while block if the condition is true.
(iii) If the condition is false then it (iii) If the condition is false the control is
stops the execution the statements. transferred to the next statement of the loop.

13. What is the difference between while loop and dowhile loop?
In the while loop the condition is first executed. If the condition is true then it
executes the body of the loop. When the condition is false it comes of the loop. In the
dowhile loop first the statement is executed and then the condition is checked. The
dowhile loop will execute at least one time even though the condition is false at the
very first time.

14. What is a Modulo Operator?


% is modulo operator. It gives the remainder of an integer division
Example:
a=17, b=6. Then c=%b gives 5.

15. How many bytes are occupied by the int, char, float, long int and double?
int - 2 Bytes
char - 1 Byte float - 4 Bytes long int - 4 Bytes double - 8 Bytes

16. What are the types of I/O statements available in C?


There are two types of I/O statements available in C.
Formatted I/O Statements
Unformatted I/O Statements

17. What is the difference between ++a and a++?


++a means do the increment before the operation (pre increment) a++ means do the
increment after the operation (post increment) Example:
a=5;
x=a++; /* assign x=5*/
y=a; /*now y assigns y=6*/
x=++a; /*assigns x=7*/

18. What is a String?


String is an array of characters.

19. What is a global variable?


The global variable is a variable that is declared outside of all the functions. The
global variable is stored in memory, the default value is zero. Scope of this variable is
available in all the functions. Life as long as the programs execution doesnt come to
an end.

20. What are the Escape Sequences present in C


\n - New Line
\b - Backspace
\t - Form feed
\ - Single quote
\\ - Backspace
\t - Tab
\r - Carriage return
\a - Alert
\ - Double quotes

21. Construct an infinite loop using while?


while (1)
{
}
Here 1 is a non zero, value so the condition is always true. So it is an infinite loop.

22. What will happen when you access the array more than its dimension?
When you access the array more than its dimensions some garbage value is
stored in the array.

23. Write the limitations of getchar( ) and sacnf( ) functions for reading strings
(JAN 2009)
getchar( )
To read a single character from stdin, then getchar() is the appropriate.
scanf( )
scanf( ) allows to read more than just a single character at a time.

24. What is the difference between scanf() and gets() function?


In scanf() when there is a blank was typed, the scanf() assumes that it is an end.
gets() assumes the enter key as end. That is gets() gets a new line (\n) terminated
string of characters from the keyboard and replaces the \n with \0.
25. What is a Structure?
Structure is a group name in which dissimilar datas are grouped together.

26. What is meant by Control String in Input/Output Statements?


Control Statements contains the format code characters, specifies the type of data that
the user accessed within the Input/Output statements.

27. What is Union?


Union is a group name used to define dissimilar data types. The union occupies only
the maximum byte of the data type. If you declare integer and character, then the
union occupies only 2 bytes, whereas structure occupies only 3 bytes.

28. What is the output of the programs given below?


main() main()
{{
float a; float a;
int x=6, y=4; int x=6, y=4;
a=x\y; a=(float) x\y;
printf(Value of a=%f, a); printf(Value of a=%f,a);
}}
Output: Output:
1. 1.500000

29. Declare the Structure with an example?


struct name
{
char name[10];
int age;
float salary;
} e1, e2;

30. Declare the Union with an example?


union name
{
char name[10];
int age;
float salary;
} e1, e2;

31. What is the output of the following program when, the name given with
spaces?
main()
{
char name[50]; printf(\n name\n); scanf(%s, name); printf(%s,name);
}
Output:
Lachi (It only accepts the data upto the spaces)

32. What is the difference between while(a) and while(!a)?


while(a) means while(a!=0)
while(!a) means while(a==0)

33. Why we dont use the symbol & symbol, while reading a String through
scanf()?
The & is not used in scanf() while reading string, because the character variable
itself specifies as a base address.
Example: name, &name[0] both the declarations are same.

34. What is the difference between static and auto storage classes?
Static Auto
Memory
Zero Memory
Local to the block in which Garbage value
Storage the variables is defined Local to the block in which the
Initial value Value of the variable persists variable is defined.
Scope between different function The block in which the
Life calls. variable is defined.
35. What is the output of the program?
main() increment()
{ {
increment(); static int i=1;
increment(); printf(%d\n,i)
increment(); i=i+1;
} }
OUTPUT:
12 3

36. Why header files are included in C programming?


This section is used to include the function definitions used in the program.
Each header file has h extension and include using # include directive at
the beginning of a program.

37. List out some of the rules used for C programming.


All statements should be written in lower case letters. Upper case letters are only for
symbolic constants.
Blank spaces may be inserted between the words. This improves the readability of
statements.
It is a free-form language; we can write statements anywhere between { and }. a
= b + c;
d = b*c;
(or)
a = b+c; d = b*c;
Opening and closing braces should be balanced.

38. Define delimiters in C.


Delimiters Use
: Colon
; Semicolon Parenthesis Square Useful for label
()[] Bracket Curly Brace Hash Terminates Statement
{} Comma Used in expression and functions
# Used for array declaration Scope of
, statement Preprocessor directive Variable
Separator

39. What do you mean by variables in C?


A variable is a data name used for storing a data value.
Can be assigned different values at different times during program execution.
Can be chosen by programmer in a meaningful way so as to reflect its function in the
program.
Some examples are: Sum percent_1 class_total

40. List the difference between float and double datatype.


S No Float Double Float / Double
Occupies 8 bytes in memory
Range : 1.7 e-308 to 1.7e+308
Format Specifier: % lf
Example : double y;
1 Occupies 4 bytes in memory There exists long double having a range
2 Range : 3.4 e-38 to 3.8e+38 of 3.4 e -4932 to 3.4 e +4932 and
3 Format Specifier: % f occupies 10 bytes in memory.
4 Example : float a; Example: long double k;

41. Differentiate break and continue statement


S No break continue
1 Exits from current block / loop Loop takes next iteration
2 Control passes to next statement Control passes to beginning of loop
3 Terminates the program Never terminates the program

42. List the types of operators.


S No Operators Types Symbolic Representation
1 Arithmetic operators = , - , * , / and %
2 Relational operators > , < , == , >=, <= and !=
3 Logical operators && , || and !
4 Increment and Decrement ++ and
5 operators =,+=,-=,*=,/=,^=,;=,&=
6 Assignment operators Bitwise & , | , ^ , >> , << , and ~
7 operators Comma operator ,
8 Conditional operator ?:

43. Distinguish between while..do and do..while statement in C. (JAN 2009)


While..DO DO..while
(i) Executes the statements within the
while block if only the condition is (i) Executes the statements within the
true. while block at least once.
(ii) The condition is checked at the (ii) The condition is checked at the end of
starting of the loop the loop

44. Compare switch( ) and nestedif statement.


S No switch( ) case nested if
Test for equality ie., only constant It can equate relational (or)
values are applicable. logical expressions.
No two case statements in same Same conditions may be repeated
switch. for a number of times.
1 Character constants are Character constants are
2 automatically converted to integers. automatically converted to integers.
3 In switch( ) case statement nested if In nested if statement switch case
4 can be used. can be used.

45. Distinguish Increment and Decrement operators.


S No Increment ++ Decrement --
1 Adds one to its operand Subtracts one from its operand
2 Equivalent x = x + 1 Equivalent x = x - 1
3 Either follow or precede operand Either follow or precede operand
4 Example : ++x; x++; Example : --x; x--;
46. Give the syntax for the for loop statement
for (Initialize counter; Test condition; Increment / Decrement)
{
statements;
}
Initialization counter sets the loop to an initial value. This statement is executed only
once.
The test condition is a relational expression that determines the number of iterations
desired or it determines when to exit from the loop. The for loop continues
to execute as long as conditional test is satisfied. When condition becomes false, the
control of program exists the body of the for loop and executes next statement after
the body of the loop.
The increment / decrement parameter decides how to make changes in the loop.
The body of the loop may contain either a single statement or multiple statements.

47. What is the use of sizeof( ) operator?


The sizeof ( ) operator gives the bytes occupied by a variable.
No of bytes occupied varies from variable to variable depending upon its data types.
Example:
int x,y;
printf(%d,sizeof(x));
Output:
2

48. What is a loop control statement?


Many tasks done with the help of a computer are repetitive in nature. Such tasks
can be done with loop control statements.

49. What are global variable in C?


This section declares some variables that are used in more than one function. such
variable are called as global variables.
It should be declared outside all functions.

50. Write a program to swap the values of two variables (without temporary
variable).
#include <stdio.h>
#include <conio.h>
void main( )
{
int a =5; b = 10;
clrscr( );
prinf(Before swapping a = %d b = %d , a , b);
a = a + b; B = a b;
a = a b;
prinf(After swapping a = %d b = %d, a,b);
getch( );
}
Output:
Before swapping a = 5 b = 10
After swapping a = 10 b = 5

51. Write short notes about main ( ) function in C program. (MAY 2009)
Every C program must have main ( ) function.
All functions in C, has to end with ( ) parenthesis.
It is a starting point of all C programs.
The program execution starts from the opening brace { and ends with closing brace
}, within which executable part of the program exists.

You might also like