Introduction to Programming in C
1. What is C?
C is a general-purpose programming language created by Dennis Ritchie at the Bell
Laboratories in 1972.It is a very popular language, despite being old. The main reason for its
popularity is because it is a fundamental language in the field of computer science. C is strongly
associated with UNIX, as it was developed to write the UNIX operating system.
2. Why C is called as a procedural language?
A procedure is known as a function, method, routine, subroutine, etc. A procedural
language specifies a series of steps for the program to solve the problem. A procedural
language breaks the program into functions, data structures, etc. C is a procedural language. In C,
variables and function prototypes must be declared before being used.
3. Why C is called C as a structured programming language ?
A structured programming language is a subset of the procedural language. Structure
means to break a program into parts or blocks so that it may be easy to understand.In the C
language, we break the program into parts using functions. It makes the program easier to
understand and modify.
4. Why C is called as a mid-level programming language?
C is considered as a middle-level language because it supports the feature of both
low-level and high-level languages. C language program is converted into assembly code, it
supports pointer arithmetic (low-level), but it is machine independent (a feature of high-
level).A Low-level language is specific to one machine, i.e., machine dependent. It is machine
dependent, fast to run. But it is not easy to understand. A High-Level language is not specific to
one machine, i.e., machine independent. It is easy to understand.
5. Write down the features of C Language.
C is the widely used language. It provides many features that are given below.
i. Mid-level programming language: Although, C is intended to do low-level
programming. It is used to develop system applications such as kernel, driver, etc.
It also supports the features of a high-level language. That is why it is known as
mid-level language.
ii. Structured programming language: C is a structured programming language in the
sense that we can break the program into parts using functions. So, it is easy to
understand and modify. Functions also provide code reusability.
iii. Pointer: C provides the feature of pointers. We can directly interact with the
memory by using the pointers. We can use pointers for memory, structures,
functions, array, etc.
R.S ComputeR ClaSS
Location: Shibnagar, Agartala
Mob:+91-9774148558
6. What is Identifiers?
C identifiers represent the name in the C program, for example, variables, functions,
arrays, structures, unions, labels, etc. An identifier can be composed of letters such as uppercase,
lowercase letters, underscore, digits, but the starting letter should be either an alphabet or an
underscore.
7. What down the rules for constructing C identifiers.
o The first character of an identifier should be either an alphabet or an underscore, and then
it can be followed by any of the character, digit, or underscore.
o It should not begin with any numerical digit.
o In identifiers, both uppercase and lowercase letters are distinct. Therefore, we can say
that identifiers are case sensitive.
o Commas or blank spaces cannot be specified within an identifier.
o Keywords cannot be represented as an identifier.
o The length of the identifiers should not be more than 31 characters.
o Identifiers should be written in such a way that it is meaningful, short, and easy to read.
8. What is Variables in C?
A variable is a name of the memory location. It is used to store data. Its value can be
changed, and it can be reused many times. It is a way to represent memory location through
symbol so that it can be easily identified.
Rules for defining variables
o A variable can have alphabets, digits, and underscore.
o A variable name can start with the alphabet, and underscore only. It can't start with a
digit.
o No whitespace is allowed within the variable name.
o A variable name must not be any reserved word or keyword, e.g. int, float, etc.
9. Describe different Data Types in C?
The basic data types are integer-based and floating-point based. C language supports both
signed and unsigned literals.
int:
Integers are entire numbers without any fractional or decimal parts, and the int data type is used
to represent them.
R.S ComputeR ClaSS
Location: Shibnagar, Agartala
Mob:+91-9774148558
It is frequently applied to variables that include values, such as counts, indices, or other
numerical numbers. The int data type may represent both positive and negative numbers because
it is signed by default.
An int takes up 4 bytes of memory on most devices, allowing it to store values between around -
2 billion and +2 billion.
char:
Individual characters are represented by the char data type. Typically used to
hold ASCII or UTF-8 encoding scheme characters, such as letters, numbers, symbols,
or commas. There are 256 characters that can be represented by a single char, which takes up
one byte of memory. Characters such as 'A', 'b', '5', or '$' are enclosed in single quotes.
float:
To represent integers, use the floating data type. Floating numbers can be used to represent
fractional units or numbers with decimal places.
The float type is usually used for variables that require very good precision but may not be very
precise. It can store values with an accuracy of about 6 decimal places and a range of about 3.4 x
1038 in 4 bytes of memory.
double:
Use two data types to represent two floating integers. When additional precision is needed, such
as in scientific calculations or financial applications, it provides greater accuracy compared to
float.
Double type, which uses 8 bytes of memory and has an accuracy of about 15 decimal places,
yields larger values. C treats floating point numbers as doubles by default if no explicit type is
supplied.
10. What is Keywords?
A keyword is a reserved word. We cannot use it as a variable name, constant name, etc.
There are only 32 reserved words (keywords) in the C language.
auto break case char const continue default do
double else enum extern float for goto if
int long register return short signed sizeof static
struct switch typedef union unsigned void volatile while
R.S ComputeR ClaSS
Location: Shibnagar, Agartala
Mob:+91-9774148558
11. What is Comments? What are the types of Comments?
Comments in C language are used to provide information about lines of code. It is widely used
for documenting code. There are 2 types of comments in the C language.
1. Single Line Comments
2. Multi-Line Comments
Single Line Comments
Single line comments are represented by double slash \\.
Mult Line Comments
Multi-Line comments are represented by slash asterisk \* ... *\.
12.What is Escape Sequence?
An escape sequence in the C programming language consists of a backslash () and a character
that stands in for a special character or control sequence. During the compilation process, the C
compiler substitutes any escape sequences it comes across with the relevant character or control
sequence.
Escape Sequence Meaning
\a Alarm or Beep
\b Backspace
\f Form Feed
\n New Line
\r Carriage Return
\t Tab (Horizontal)
\v Vertical Tab
13. What is ASCII code?
The full form of ASCII is the American Standard Code for information interchange.
It is a character encoding scheme used for electronics communication. Each character or a
special character is represented by some ASCII code, and each ascii code occupies 7 bits in
memory. The ASCII value represents the character variable in numbers, and each character
variable is assigned with some number range from 0 to 127. For example, the ascii value of 'A' is
65.
R.S ComputeR ClaSS
Location: Shibnagar, Agartala
Mob:+91-9774148558
14. What is Tokens in C?
Tokens in C is the most important element to be used in creating a program in C. We can define
the token as the smallest individual element in C. Tokens in C is the building block or the basic
component for creating a program in C language.
Classification of tokens in C
Tokens in C language can be divided into the following categories:
o Keywords in C
o Identifiers in C
o Strings in C
o Operators in C
o Constant in C
o Special Characters in C
15. What is Constants ?
A constant is a value assigned to the variable which will remain the same throughout the
program, i.e., the constant value cannot be changed.
There are two ways of declaring constant:
o Using const keyword
o Using #define pre-processor
16. Programming Errors in C?
Errors are the problems or the faults that occur in the program, which makes the behavior
of the program abnormal, and experienced developers can also make these faults. Programming
errors are also known as the bugs or faults, and the process of removing these bugs is known
as debugging.
Types of errors exist in C programming:
o Syntax Error : Syntax errors are also known as the compilation errors as they occurred
at the compilation time, or we can say that the syntax errors are thrown by the compilers.
These errors are mainly occurred due to the mistakes while typing or do not follow the
syntax of the specified programming language.
Commonly occurred syntax errors are:
If we miss the parenthesis (}) while writing the code.
Displaying the value of a variable without its declaration.
If we miss the semicolon (;) at the end of the statement.
R.S ComputeR ClaSS
Location: Shibnagar, Agartala
Mob:+91-9774148558
o Run-time Error: Sometimes the errors exist during the execution-time even after the
successful compilation known as run-time errors. When the program is running, and it is
not able to perform the operation is the main cause of the run-time error. The division by
zero is the common example of the run-time error.
o Logical Error : The logical error is an error that leads to an undesired output. These
errors produce the incorrect output, but they are error-free, known as logical errors.
17. Write down the applications of C Programming ?
C was initially used for system development work, particularly the programs that make-up
the operating system. C was adopted as a system development language because it produces code
that runs nearly as fast as the code written in assembly language. Some examples of the use of C
are -
Operating Systems
Language Compilers
Assemblers
Text Editors
18. What is a Compiler?
A compiler is a software program that follows the syntax rule of programming language to
convert a source code to machine code. It cannot fix any error if present in a program; it
generates an error message, and you have to correct it yourself in the program's syntax. If your
written program is correct (contains no error), then the compiler will convert your entire source
code into machine code.
Advantages of Compiler
Compiled code runs faster in comparison to Interpreted code.
Compilers help in improving the security of Applications.
As Compilers give Debugging tools, which help in fixing errors easily.
Disadvantages of Compiler
The compiler can catch only syntax errors and some semantic errors.
Compilation can take more time in the case of bulky code.
19. What is a Interpreter?
An interpreter is also a software program that translates a source code into a machine
language. However, an interpreter converts high-level programming language into machine
language line-by-line while interpreting and running the program.
R.S ComputeR ClaSS
Location: Shibnagar, Agartala
Mob:+91-9774148558
Advantages of Interpreter
Programs written in an Interpreted language are easier to debug.
Interpreters allow the management of memory automatically, which reduces memory error
risks.
Interpreted Language is more flexible than a Compiled language.
Disadvantages of Interpreter
The interpreter can run only the corresponding Interpreted program.
Interpreted code runs slower in comparison to Compiled code.
20. Difference between Compiler and Interpreter ?
Parameter Compiler Interpreter
Steps of Creation of the program. Creation of the program.
Programming The Compiler analyses all the It doesn’t require the linking of
language statements and throws files or generation of machine
an error when it finds something code.
incorrect. It executes the source
If there’s zero error, the compiler statements line by line during
converts the source code to the execution.
machine one.
It links various code files into a
runnable program (exe).
It runs the program.
Advantage The code execution time is comparatively They are fairly easy to use and execute,
less because the program code already even for a beginner.
gets translated into machine code.
Disadvantage One can’t change a program without Only computers with the corresponding
getting back to the source code. Interpreter can run the interpreted
programs.
Execution One can separate the program execution Execution of the program is one of the
from the compilation. Thus, you can steps of the Interpretation process. So,
perform it only after completing the you can perform it line by line.
compilation of the entire output.
Use It works best for the Production It works the best for the programming
Environment. and development environment.
R.S ComputeR ClaSS
Location: Shibnagar, Agartala
Mob:+91-9774148558
Execution of A Compiler displays every error and An Interpreter reads every statement,
Error warning while compiling. So, you can’t then displays the errors, if any. A user
run this program unless you fix the must resolve these errors in order to
errors. interpret the next line.
Programming Java, Scala, C#, C, C++ use Compilers. Perl, Ruby, PHP use Interpreters
Languages
21. What is Operator in C ? Explain the different types of Operator used in C with example
each type?
22. What is Header File? Why Header file is important?
Header files contain a set of predefined standard library functions. The .h is the
extension of the header files in C and we request to use a header file in our program by
including it with the C preprocessing directive “#include”. C language has numerous libraries
that include predefined functions to make programming easier.
Here is the table that displays some of the header files in C language:
Sr.No. Header Files & Description
1 stdio.h
Input/Output functions
2 conio.h
Console Input/Output functions
3 stdlib.h
General utility functions
4 math.h
Mathematics functions
5 string.h
String functions
The <stdio.h> Header File In C
The name stands for standard input/output header, and it contains functions for standard input
and output operations such as printf() and scanf() and file operations like fopen(), fclose(),
fseek(), fread(), and write(). This header file is used for printing information to the console,
reading input from the keyboard, and reading and writing data to and from files.
R.S ComputeR ClaSS
Location: Shibnagar, Agartala
Mob:+91-9774148558
Some standard functions that form a part of this header file are:
1. printf(): Used to print formatted output to the console or a file.
2. scanf(): Used to read formatted input from the console or a file.
3. fgets(): Used to read a line of text from a file or the console.
4. fopen(): Used to open a file.
5. fclose(): Used to close a file.
6. fseek(): Used to set the file position indicator for a file.
7. fread(): Used to read data from a file.
8. fwrite(): Used to write data to a file.
The <math.h> Header File In C
This header file contains mathematical functions such as trigonometric functions, logarithmic
functions, and exponential functions. This includes functions like sin(), cos(), tan(), sqrt(),
pow(), and ceil() for performing mathematical operations. The primary purpose of this header is
to help in performing complex mathematical operations.
Some standard functions that form a part of this header file are:
1. sin(): Used to calculate the sine of an angle.
2. cos(): Used to calculate the cosine of an angle.
3. tan(): Used to calculate the tangent of an angle.
4. sqrt(): Used to calculate the square root of a number.
5. pow(): Used to raise a number to a power.
6. ceil(): Used to round a number up to the nearest integer.
7. floor(): Used to round a number down to the nearest integer.
23. What do you mean by Precedence and Associativity of an operator?
Precedence of operators
The precedence of operators determines which operator is executed first if there is more than
one operator in an expression.
Let us consider an example:
R.S ComputeR ClaSS
Location: Shibnagar, Agartala
Mob:+91-9774148558
int x = 5 - 17* 6;
In C, the precedence of * is higher than - and =. Hence, 17 * 6 is evaluated first. Then the
expression involving - is evaluated as the precedence of - is higher than that of =.
Associativity of Operators
The associativity of operators determines the direction in which an expression is evaluated. For
example,
b = a;
Here, the value of a is assigned to b, and not the other way around. It's because the associativity
of the = operator is from right to left.
24.Explain the purpose of printf() and scanf() function in C?
printf() Function
In C Programming language, the printf() function is used for output. printf() function can take
any number of arguments. First argument must be enclosed within the double quotes “hello” and
every other argument should be separated by comma ( , ) within the double quotes.
Important points about printf():
printf() function is defined in stdio.h header file. By using this function, we can print the
data or user-defined message on monitor (also called the console).
printf() can print a different kind of data format on the output string.
To print on a new line on the screen, we use “\n” in printf() statement.
C language is case sensitive programming language. For example, printf() and scanf() in
lowercase letters treated are different from Printf() and Scanf(). All characters in printf() and
scanf() builtin functions must be in lower case.
Syntax
printf("format specifier",argument_list);
The format string for output can be
%d (integer), %c (character), %s (string), %f (float) %lf (double)
and %x (hexadecimal) variable.
scanf() Function
The scanf() function is used to read input data from the console.The scanf() function is builtin
function available in the C library. scanf() function can read character, string, numeric & other
data from keyboard in C language.scanf() reads formatted data from user and assign them in the
R.S ComputeR ClaSS
Location: Shibnagar, Agartala
Mob:+91-9774148558
variables provided the additional arguments. Additional arguments must point to variables that
have the same datatype as of user input data format.
Syntax
scanf("format specifier",argument_list);
25. Write down the purpose of clrscr() and getch() function?
clrscr() in C
The clrscr() in C is a built-in function that is used for clearing the screen of the console output
during the execution of the C program. This function is defined in the conio.h header file. This
function is useful in the case of the console-based program as it allows the programmer to clear
the screen and starts displaying the output from the top of the screen.
The syntax of the clrscr in C is given below:
clrscr();
getch() in C
The getch in C is a non-standard function used to receive a character as input from the user. It is
defined in the header file conio.h The character entered by the user is not visible on the output
screen but is stored in the assigned variable which makes this the best method for receiving
passwords from a user.
We can also explain the getch() as a function call that pauses the execution of the program until
the user enters a character from the keyboard.
The sytax of the getch() function call in C is,
getch();
26. Write the differences between = and ==?
= ==
It is an assignment operator. It is a relational or comparison operator.
It is used for assigning the value to a It is used for comparing two values. It returns 1 if
variable. both the values are equal otherwise returns 0.
Constant term cannot be placed on left Constant term can be placed in the left hand side.
hand side. Example: 1=x; is invalid. Example: 1==1 is valid and returns 1.
R.S ComputeR ClaSS
Location: Shibnagar, Agartala
Mob:+91-9774148558
27. Difference between Prefix and Postfix Operator?
Prefix Operator
The increment operator ++ if used as prefix on a variable, the value of variable gets incremented
by 1. After that the value is returned unlike Postfix operator. It is called Prefix increment
operator. In the same way the prefix decrement operator works but it decrements by 1.
Postfix Operator
The increment operator ++ if used as postfix on a variable, the value of variable is first returned
and then gets incremented by 1. It is called Postfix increment operator. In the same way the
decrement operator works but it decrements by 1.
28. What is Type Casting? What are the different types of casting?
Typecasting in C is the process of converting one data type to another data type by the
programmer using the casting operator during program design. In typecasting, the destination
data type may be smaller than the source data type when converting the data type to another
data type, that’s why it is also called narrowing conversion.
Syntax:
int x;
float y;
y = (float) x;
Types of Type Casting in C
In C there are two major types to perform type casting.
1. Implicit Type Casting
Implicit type casting in C is used to convert the data type of any variable without using the
actual value that the variable holds. It performs the conversions without altering any of the
values which are stored in the data variable. Conversion of lower data type to higher data type
will occur automatically.
R.S ComputeR ClaSS
Location: Shibnagar, Agartala
Mob:+91-9774148558
2. Explicit Type Casting
There are some cases where if the datatype remains unchanged, it can give incorrect output. In
such cases, typecasting can help to get the correct output and reduce the time of compilation.
In explicit type casting, we have to force the conversion between data types. This type of
casting is explicitly defined within the program.
29. What are the C library functions?
Library functions are built-in functions that are grouped together and placed in a
common location called library. Each function here performs a specific operation. We can use
this library functions to get the pre-defined output. All C standard library functions are declared
by using many header files.
Sl.No Function & Description
1 printf()
This function is used to print the all char, int, float, string etc., values onto the output
screen.
2 scanf()
This function is used to read data from keyboard.
3 getc()
It reads character from file.
4 gets()
It reads line from keyboard.
30. Explain important functions in math.h library functions using C language.
A. floor() :It is used to round down a float B. ceil() :It is used to round up a float
number. number.
Syntax Syntax −
Double floor(double a); Double ceil(double a);
Example Example,
Double a=floor(93.58); Double a=ceil(93.58);
The output is as follows − The output is as follows −
A=93 A=94
R.S ComputeR ClaSS
Location: Shibnagar, Agartala
Mob:+91-9774148558
C. exp() :It is used to find the exponent
value of its argument.
Syntax
double exp (double a);
Example,
double a=exp(4.6);
D. abs() :It is used to find the absolute value
of an integer.
Syntax −
int abs(int a);
For example,
Int a= abs(-6);
The output is as follows −
a=6
E. pow() :It is used to find the power of a
value.
Syntax −
double pow(double a,double b);
For example,
double x=pow(2,3)
The output is as follows −
x=8
F. sqrt():It is used to find the square root
value of its argument.
Syntax −
double sqrt(double a);
For example,
double x=sqrt(36);
The output is as follows −
x=6
R.S ComputeR ClaSS
Location: Shibnagar, Agartala
Mob:+91-9774148558