C LANGUAGE
by Shivajirao Jadhav Mumbai
Marks: 10 (15)
Introduction:
C is a general-purpose programming language used for wide range of applications from Operating systems like Windows and UNIX to software that is used for creating 3D movies.
C programming is highly efficient. That’s the main reason why it’s very popular despite being more than 40 years old.
Uses of C language:
C programming language is used for developing system applications. Below are some examples of C being used.
Operating system development
Data base system
Word processors
Spread sheets
Compilers and assemblers
C shows both features of low level language and high level language, so C language is known as middle level language. C is very powerful programming language as most the part of UNIX is written in C. C is closely associated with Unix Operating system.
Tokens in C language:
When we write a paragraph, the individual words and punctuation marks are called tokens. In same way C program has got tokens. These tokens are small units. C tokens, Identifiers and Keywords are the basics in a C program.
C tokens are the basic buildings blocks in When C language which are constructed together to write a C program.
Each and every smallest individual units in a C program are known as C tokens.
C tokens are of six types. They are,
Keywords (eg: int, while),
Identifiers (eg: main, total),
Constants (eg: 10, 20),
Strings (eg: “total”, “hello”),
Special symbols (eg: (), {}),
Operators (eg: +, /,-,*)
Keywords in C language:
Keywords are those words whose meaning is already defined by Compiler
Each keyword is meant to perform a specific function in a C program.
Cannot be used as Variable Name
There are 32 Keywords in C
C Keywords are also called as Reserved words .
C Constants:
Constants have the fixed values that do not change during the execution of a program. A "constant" is a number, character, or character string that can be used as a value in a program.
Types of C constants:
Integer and real constants are numeric constants.
Integer Constants: It has sequence of digits. It must have at least one digit e. g. 5, 87, 145 etc.
Real Constants: Number having decimal point e.g. 0.35, 5.6, -78.976 etc.
Single character constants: A single character enclosed in single quote e.g. ‘M’, ‘S’ etc
String constants: It has sequence of character enclosed in double quote e.g. “Ajay”, “Ram” etc.
C variables:
Variables are used to store the value during the execution of a program. The name itself means, the value of variable can be changed hence the name “Variable“. The variables are stored in Main Memory i.e. RAM.
Types of C variables:
Integer variable
Float variable
Character variable
The type of a variable cannot be changed. Suppose we declared an integer type variable so we cannot store character or a decimal number in that variable.
The rules for constructing C variable:
The 1st letter should be alphabet.
Variables can be combination of alphabets and digits.
are Case Sensitive.
No Spaces allowed between Characters.
Variable name should not make use to the C Reserved Keywords.
Underscore (_) is the only special character allowed.
Variables can be written in both Uppercase and Lowercase or combination of both.
Variables Variable name should not start with a number.
Data types in C:
Data types specify how we enter data into our programs and what type of data we enter. C language has some predefined set of data types to handle various kinds of data that we can use in our program. These data types have different storage capacities.
1. Primary data types:
These are fundamental data types in C namely integer(int), floating point(float), character(char) and void.
2. Derived data types:
Derived data types are nothing but primary data types grouped together like array, structure, union and pointer.
Primary data types:
Integer type
Integers are used to store whole numbers.
Size and range of Integer type data:
Floating point type:
Floating types are used to store real numbers.
Size and range of floating type data:
Character type
Character types are used to store characters value.
Size and range of character type data:
void type
void type means no value. This is usually used to specify the type of functions which returns nothing.
C operators:
C language supports a rich set of built-in operators. An operator is a symbol that tells the compiler to perform a certain mathematical or logical operation. Operators are used in programs to manipulate data and variables.
C operators can be classified into following types:
Arithmetic operators
Relational operators
Logical operators
Bitwise operators
Assignment operators
Conditional operators
Special operators
Arithmetic operators:
Relational operators:
Decision making in C:
Decision making is about deciding the order of execution of statements based on certain conditions or repeat a group of statements until certain specified conditions are met.
Decision making with if statement:
The if statement may be implemented in different forms depending on the complexity of conditions to be tested. The different forms are,
Simple if statement
if....else statement
Nested if....else statement
Using else if statement
Simple if statement:
Format:
If the expression returns true, then the statement-inside will be executed, otherwise statement-inside is skipped and only the statement-outside is executed.
Example: Program to find greater number
if...else statement:
Format:
If the expression is true, the statement-block1 is executed, else statement-block1 is skipped andstatement-block2 is executed.
Example: Program to find greater number
else …if statement:
Format:
The expression is tested from the top(of the ladder) downwards. As soon as a true condition is found, the statement associated with it is executed.
Example: program to find number divisible by 5 and 8
Loops in C:
In any programming language including C, loops are used to execute a set of statements repeatedly until a particular condition is satisfied. When looping condition becomes false then control moves to next statement immediately after the end of loop block.
while loop:
While is entry controlled loop statement. It tells the compiler to repeat the series of statements as long as certain condition is true. As condition becomes false computer stops repeating and goes to next statement.
Format:
Example: Program to print first 10 natural numbers
do while loop:
These loops are exactly like while loops except that the test is performed at the end of the loops rather than beginning. In this loop computer centers into the body of do while, execute it and then checks the condition. If condition is true it goes back and continues till condition remains true. When condition becomes false it goes to next statement.
Format:
Example: Program to print first 10 multiples of 5.
Difference between while and do while loop:
for loop:
for loop is used to execute a set of statements repeatedly until a particular condition is satisfied. We can say it is an open ended loop.. General format is,
Format:
In for loop we have exactly two semicolons, one after initialization and second after the condition. In this loop we can have more than one initialization or increment/decrement, separated using comma operator. But it can have only one condition.
Example: Program to print first 10 natural numbers
C functions:
C functions are basic building blocks in a program. All C programs are written using functions to improve re-usability, understandability and to keep track on them.
What is c function?
A large C program is divided into basic building blocks called C function. C function contains set of instructions enclosed by “{ }” which performs specific operation in a C program. Actually, Collection of these functions creates a C program.
Uses of c functions:
C functions are used to avoid rewriting same logic/code again and again in a program.
There is no limit in calling C functions to make use of same functionality wherever required.
We can call functions any number of times in a program and from any place in a program.
A large C program can easily be tracked when it is divided into functions.
The core concept of C functions are, re-usability, dividing a big task into small pieces to achieve the functionality and to improve understandability of very large C programs.
How to call c functions in a program?
There are two ways that a C function can be called from a program. They are,
Call by value
Call by reference
1. Call by value:
In call by value method, the value of the variable is passed to the function as parameter.
The value of the actual parameter can not be modified by formal parameter.
Different Memory is allocated for both actual and formal parameters. Because, value of actual parameter is copied to formal parameter.
2. Call by reference:
In call by reference method, the address of the variable is passed to the function as parameter.
The value of the actual parameter can be modified by formal parameter.
Same memory is used for both actual and formal parameters since only address is used by both parameters.
Program to add two integers:
Output:
Questions
Marks wise Questions: 1M(2), 3M(3), 4M(1)
1. Fill in the blanks with given alternatives.
-----is known as middle level language.
The words and punctuation marks in paragraph are called ------------.
------ are specifications provides with variable or function name.
-------are the symbols used to indicate the operations on the values.
-------allows us to execute a statement multiple time.
-------can store a fixed size sequential collection of elements of the same size.
------ is a group of statements that together perform task.
------language was evolved from ALGOL, BPCL and B.B.
--------is not a C token.
--------operator is used for modulo division.
-------operator is used for left shift.
In ---------C loop condition is given at top.
In --------C loop condition is given at bottom.
C language programs are converted into machine language with the help of ------.
Size of --------integer type data is smallest in C.
C language has --------types of token.
Answers-: 1. C 2. Tokens 3. Data types 4. Operators 5. Loop statement 6. Arrays
7. A function 8. C 9. Integer 10. % 11. << 12. while 13. do while
14. Compiler 15. Short/signed short 16. 6
2. Answer the following.
State uses of C language.
What is token? List types of tokens.
List the types of constants and variables in C.
What are variables? State the rules for constructing/naming variables.
What is difference between constant and variable?
List basic data types in C.
List down keywords in C.
Give the list of arithmetical and relational operators in C.
What is use of a loop statement? State types of loops.
Write the format of -(i) if statement (ii) if----else statement (iii) else---if statement (iv) while loop (v) do while loop (vi) for loop
Distinguish between while and do while loop.
Write how to call C functions in program.
Write a program in C to add two integers.