Programming in C For Exam

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 11

C Notes By Kundan Bharti

Programming In C For Exam


Language Processor

Mostly, high-level languages like Java, C++, Python, and more are used to write the programs,
called source code, as it is very uninteresting work to write a computer program directly in
machine code. These source codes need to translate into machine language to be executed
because they cannot be executed directly by the computer. Hence, a special translator system, a
language processor, is used to convert source code into machine language.

A language processor is a special type of software program that has the potential to translate the
program codes into machine codes.

Types of language processors

There are mainly three kinds of language processors

1. Compiler: The language processor that reads the complete source program written in high-
level language as a whole in one go and translates it into an equivalent program in machine
language is called a Compiler. Example: C, C++, C#, Java.

2. Assembler: An assembler converts programs written in assembly language into machine code.
It is also referred to assembler as assembler language by some users. The source program has
assembly language instructions, which is an input of the assembler. The assemble translates this
source code into a code that is understandable by the computer, called object code or machine
code.

3. Interpreter: An interpreter is a computer program that allows a computer to interpret or


understand what tasks to perform. The programs written with the help of using one of the many
high-level programming languages are directly executed by an interpreter without previously
converting them to an object code or machine code, which is done line by line or statement by
statement. When the interpreter is translating the source code, it displays an error message if there
is an error in the statement and terminates this statement from translating process. When the
interpreter removed errors on the first line, then it moves on to the next line.

S.V. Polytechnic College Bhopal Page 1


C Notes By Kundan Bharti

Difference between Compiler and Interpreter


Compiler Interpreter

A compiler is a program that takes the complete program The interpreter is different from the Compiler;
as a single unit and converts the entire source code into it takes a source program and translates it one
executable machine code for a CPU. by one, and executes.

The compiler is comparatively faster in order to the overall As compared to the compiler, an interpreter is
execution of the source code; however, it takes a large slower in order to the overall execution of the
amount of time to analyze to compile the complete programming code, but it takes less time to
programming code. evaluate the source code.

When the compiler completes scanning the whole With an interpreter, until the error is met, it
program, then it generates the error message. As the error continues converting the program; therefore,
can be found in any statement in the program; therefore, its debugging is easier.
debugging is comparatively hard with a compiler.

When you want to run the program, a translator program When you want to run the program, a
is needed each time to convert the source code. translator program is not needed each time to
convert the source code.

It stores object code and is more beneficial for commercial It does not hold object code, and it is more
purposes. beneficial for learning purposes.

Examples: C, C++, Java, FORTRAN compiler, PASCAL Examples: Python, Perl, LISP, APL, Prolog
compiler. interpreter, etc.

Linker –
A linker or link editor is a program that takes a collection of objects (created by assemblers and compilers)
and combines them into an executable program.

Loader –
The loader keeps the linked program in the main memory.

S.V. Polytechnic College Bhopal Page 2


C Notes By Kundan Bharti

Procedural Language Non-Procedural Language


It is command-driven language. It is a function-driven language
It works through the state of machine. It works through the mathematical
functions.
Its semantics are quite tough. Its semantics are very simple.
It returns only restricted data types and allowed It can return any datatype or value
values.
Overall efficiency is very high. Overall efficiency is low as compared to
Procedural Language.
Size of the program written in Procedural Size of the Non-Procedural language
language is large. programs are small.
It is not suitable for time critical applications. It is suitable for time critical applications.
Iterative loops and Recursive calls both are used Recursive calls are used in Non-
in the Procedural languages. Procedural languages.
Structured programming

In structured programming design, programs are broken into different functions these functions
are also known as modules, subprogram, subroutines and procedures.

Each function is design to do a specific task with its own data and logic. Information can be passed
from one function to another function through parameters. A function can have local data that
cannot be accessed outside the function’s scope. The result of this process is that all the other
different functions are synthesized in an another function. This function is known as main function.
Many of the high level languages supported structure programming.

Advantages of structured programming

The following are the different advantages of structured programming

1. It is user friendly and easy to understand.


2. Similar to English vocabulary of words and symbols.
3. It is easier to learn.
4. They require less time to write.
5. They are easier to maintain.
6. These are mainly problem oriented rather than machine based.

Disadvantages of structured programming

The following are the disadvantages of structured programming:

1. A high level language has to be translated into the machine language by translator and thus a price
in computer time is paid.
2. The object code generated by a translator might be inefficient compared to an equivalent assembly
language program.

S.V. Polytechnic College Bhopal Page 3


C Notes By Kundan Bharti

C Character Set

As every language contains a set of characters used to construct words, statements, etc., C language also has a
set of characters which include alphabets, digits, and special symbols. C language supports a total of 256
characters.

Every C program contains statements. These statements are constructed using words and these words are
constructed using characters from C character set. C language character set contains the following set of
characters...

1. Alphabets
2. Digits
3. Special Symbols

Alphabets
C language supports all the alphabets from the English language. Lower and upper case letters together support
52 alphabets.
lower case letters - a to z
UPPER CASE LETTERS - A to Z

Digits
C language supports 10 digits which are used to construct numerical values in C language.
Digits - 0, 1, 2, 3, 4, 5, 6, 7, 8, 9

Special Symbols
C language supports a rich set of special symbols that include symbols to perform mathematical operations, to
check conditions, white spaces, backspaces, and other special symbols.
Special Symbols - ~ @ # $ % ^ & * ( ) _ - + = { } [ ] ; : ' " / ? . > , < \ 

Keywords 
A keyword is a reserved word. You cannot use it as a variable name, constant name, etc. There are
only 32 reserved words (keywords) in the C language.

A list of 32 keywords in the c language is given below:

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

S.V. Polytechnic College Bhopal Page 4


C Notes By Kundan Bharti

Constants in C

A constant is a name given to the variable whose values can’t be altered or changed. A constant is very similar
to variables in the C programming language, but it can hold only a single variable during the execution of a
program. It means that once we assign value to the constant, then we can’t change it throughout the execution
of a program- it stays fixed.

List of Constants in C
Constant Example

Decimal Constant 10, 20, 450 etc.

Real or Floating-point Constant 10.3, 20.2, 450.6 etc.

Octal Constant 021, 033, 046 etc.

Hexadecimal Constant 0x2a, 0x7b, 0xaa etc.

Character Constant 'a', 'b', 'x' etc.

String Constant "c", "c program", etc.

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.

In C, there are different types of variables (defined with different keywords), for


example:

 int - stores integers (whole numbers), without decimals, such as 123 or -123
 float - stores floating point numbers, with decimals, such as 19.99 or -19.99
 char - stores single characters, such as 'a' or 'B'. Char values are surrounded
by single quote

Syntax
type variableName = value;

Example
int myNum = 15;

Rules for defining variables


o A variable can have alphabets, digits, and underscore.

S.V. Polytechnic College Bhopal Page 5


C Notes By Kundan Bharti

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.

Data Types in C
A data type specifies the type of data that a variable can store such as integer, floating, character, etc.

Structure of a C program

The structure of a C program means the specific structure to start the programming in the C
language. Without a proper structure, it becomes difficult to analyze the problem and the solution.
It also gives us a reference to write more complex programs.

Basic Structure of C Program


Section Description

Documentation Consists of the description of the program, programmer's name, and creation date. These are
generally written in the form of comments.

Link All header files are included in this section which contains different functions from the
libraries. A copy of these header files is inserted into your code before compilation.

Definition Includes preprocessor directive, which contains symbolic constants. E.g.: #define allows us to


use constants in our code. It replaces all the constants with its value in the code.

Global Includes declaration of global variables, function declarations, static global variables, and
Declaration functions.

S.V. Polytechnic College Bhopal Page 6


C Notes By Kundan Bharti

Main() Function For every C program, the execution starts from the main() function. It is mandatory to include
a main() function in every C program.

Subprograms Includes all user-defined functions (functions the user provides). They can contain the inbuilt
functions, and the function definitions declared in the Global Declaration section. These are
called in the main() function.

Documentation
In a C program, single-line comments can be written using two forward slashes i.e., //, and we can create
multi-line comments using /* */. Here, we've used multi-line comments.

/**
* file: age.c
* author: you
* description: program to find our age.
*/

Link
All header files are included in this section.

A header file is a file that consists of C declarations that can be used between different files. It helps us in
using others' code in our files. A copy of these header files is inserted into your code before compilation.

#include <stdio.h>

Definition
A preprocessor directive in C is any statement that begins with the "#" symbol. The #define is a
preprocessor compiler directive used to create constants. In simple terms, #define basically allows the
macro definition, which allows the use of constants in our code.

#define BORN 2000

Global Declaration
This section includes all global variables, function declarations, and static variables. The variables declared
in this section can be used anywhere in the program. They're accessible to all the functions of the program.
Hence, they are called global variables.

int age(int current);

S.V. Polytechnic College Bhopal Page 7


C Notes By Kundan Bharti

Main() Function
In the structure of a C program, this section contains the main function of the code. The compiler starts
execution from the main() function. It can use global variables, static variables, inbuilt functions, and user-
defined functions. The return type of the main() function can be void also not necessarily int.
int main(void)
{
int current = 2021;
printf("Age: %d", age(current));
return 0;
}

Subprograms
This includes the user-defined functions called in the main() function. User-defined functions are generally
written after the main() function irrespective of their order.
int age(int current) {
return current - BORN;
}

S.V. Polytechnic College Bhopal Page 8


C Notes By Kundan Bharti

C - Operators
An operator is a symbol that tells the compiler to perform specific mathematical or logical
functions. C language is rich in built-in operators and provides the following types of operators −

 Arithmetic Operators
 Relational Operators
 Logical Operators
 Bitwise Operators
 Assignment Operators
 Misc Operators
Arithmetic Operators
The following table shows all the arithmetic operators supported by the C language. Assume
variable A holds 10 and variable B holds 20 then −
Operator Description Example
+ Adds two operands. A + B = 30
− Subtracts second operand from the first. A − B = -10
* Multiplies both operands. A * B = 200
/ Divides numerator by de-numerator. B/A=2
% Modulus Operator and remainder of after an integer division. B%A=0
++ Increment operator increases the integer value by one. A++ = 11
-- Decrement operator decreases the integer value by one. A-- = 9

Relational Operators
The following table shows all the relational operators supported by C. Assume variable A holds 10
and variable B holds 20 then −
Operator Description Example
== Checks if the values of two operands are equal or not. If yes, then the (A == B)
condition becomes true. is not
true.
!= Checks if the values of two operands are equal or not. If the values are not (A != B)
equal, then the condition becomes true. is true.
> Checks if the value of left operand is greater than the value of right operand. (A > B)
If yes, then the condition becomes true. is not
true.
< Checks if the value of left operand is less than the value of right operand. If (A < B)
yes, then the condition becomes true. is true.
>= Checks if the value of left operand is greater than or equal to the value of (A >= B)
right operand. If yes, then the condition becomes true. is not
true.
<= Checks if the value of left operand is less than or equal to the value of right (A <= B)
operand. If yes, then the condition becomes true. is true.

S.V. Polytechnic College Bhopal Page 9


C Notes By Kundan Bharti

Logical Operators
Following table shows all the logical operators supported by C language. Assume variable  A holds
1 and variable B holds 0, then −
Operator Description Example
&& Called Logical AND operator. If both the operands are non-zero, then the (A && B)
condition becomes true. is false.
|| Called Logical OR Operator. If any of the two operands is non-zero, then the (A || B) is
condition becomes true. true.
! Called Logical NOT Operator. It is used to reverse the logical state of its !(A &&
operand. If a condition is true, then Logical NOT operator will make it false. B) is
true.

Bitwise Operators
Bitwise operator works on bits and perform bit-by-bit operation. The truth tables for &, |, and ^ is as
follows −
p q p&q p|q p^q
0 0 0 0 0
0 1 0 1 1
1 1 1 1 0
1 0 0 1 1

Assignment Operators
Operator Description Example
= Simple assignment operator. Assigns values from right side operands to left C=A+B
side operand will assign
the value
of A + B
to C
+= Add AND assignment operator. It adds the right operand to the left operand C += A is
and assign the result to the left operand. equivalent
to C = C +
A
-= Subtract AND assignment operator. It subtracts the right operand from the C -= A is
left operand and assigns the result to the left operand. equivalent
to C = C -
A

sizeof & ternary


Besides the operators discussed above, there are a few other important operators including sizeof
Operator Description Example
sizeof() Returns the size of a variable. sizeof(a), where a is integer, will return 4.

& Returns the address of a variable. &a; returns the actual address of the
variable.
* Pointer to a variable. *a;
?: Conditional Expression. If Condition is true ? then value X :
otherwise value Y

S.V. Polytechnic College Bhopal Page 10


C Notes By Kundan Bharti

S.V. Polytechnic College Bhopal Page 11

You might also like