0% found this document useful (0 votes)
19 views16 pages

Unit 1 - PIC

The document provides an overview of the C programming language, including its features, structure, and fundamental data types. It explains key concepts such as tokens, keywords, identifiers, constants, and the basic syntax for input/output functions. Additionally, it outlines the steps for executing a C program and differentiates between various types of constants and variables.

Uploaded by

vinayvinay55685
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
19 views16 pages

Unit 1 - PIC

The document provides an overview of the C programming language, including its features, structure, and fundamental data types. It explains key concepts such as tokens, keywords, identifiers, constants, and the basic syntax for input/output functions. Additionally, it outlines the steps for executing a C program and differentiates between various types of constants and variables.

Uploaded by

vinayvinay55685
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 16

PROGRAMMING IN C – UNIT 1

2 MARKS

1) List any two important features of C


a) C is a structured programming language.
b) C is machine independent and portable.

2) What are tokens? Give any two examples.


The smallest individual units in a C program are known as tokens.
Ex : Keywords – float, while
Constants – 12, -15.8

3) Give the structure of C program.


Documentation section
Link section(#include section)
Definition section
Global declaration section
main() function section
{
Declaration part
Executable part
}
Subprogram section
Function 1
Function 2
. (User defined functions)
.
Function n

4) List steps involved in executing a C program .


There are 4 steps involved in executing a C program.
1. Creating the program;
2. Compiling the program;
3. Linking the program with functions;
4. Executing the program;

5) What are keywords? List any four keywords.


C words having fixed meanings whose meanings can not be changed are called as
keywords.
Example for keywords are :
break, float, double, for, if, char
6) What are identifiers? Give example for a valid and invalid identifier name.
Identifiers are the user defined names consisting of letters, digits and underscore. They
refer to the names of variables, functions and arrays.
Example for a valid identifier name : sum, name, Total, add2
Example for an invalid identifier name : 2sum, 2name, add sub

7) Differentiate keywords and identifiers.


Keywords
• C words having fixed meanings whose meanings can not be changed are called
as keywords.
• Example for keywords are : break, float, double etc
• All keywords must be written in lowercase.
Identifiers
• Identifiers are the user defined names consisting of letters, digits and
underscore.
• Example for identifiers are : sum, name, Total, add2
• Both uppercase and lowercase letters are permitted.

8) List the rules to be followed for naming an identifier/variable.


• First character must be an alphabet.
• Must consists of only letters, digits or underscore.
• Only first 31 characters are significant.
• Keywords can not be used.
• Must not contain a white space.

9) What are constants? Give examples for integer constants.


Constants are the fixed values that do not change during the execution of the program.
Examples for integer constants are : 123, 21, -12 etc

10) Differentiate constants and variables.


Constants
Constants are the fixed values that do not change during the execution of the program.
Ex : 12, 0.4, ‘X’, “Hello” etc
Variables
Variables are used to store the data value. Unlike constants variables can take different
values at different times during the execution.
Ex : sum, name, addition, Total etc

11) Differentiate integer and real constants. Give example

Integer constants
Integer constants refer to the sequence of digits.
There are 3 types of integers : Decimal integers, octal integers, hexadecimal integers.
Integer constants are inadequate to represent quantities such as distances, heights,
prices etc.
Examples for integer constants are : 12, -32, 100 etc
Real constants
Real constants are the numbers with fractional parts.
Unlike Integer constants real constants can represent quantities such as distance,
heights, prices etc.
Examples for real constants are: 0.008, -0.75, 3.14 etc

12) Differentiate character and string constants.


Character constants
A single character constant contains a single character enclosed within single quotes.
A single character will have an equivalent integer value. For example integer value of
“A” is 65.
Examples for single character constant are : ‘X’, ‘5’, ‘+’, ‘ ‘ etc
String constants
A string constant is a sequence of characters enclosed in double quotes.
The characters may be letters, numbers, special characters and blank spaces.
Unlike single character constant string constant will not have equivalent integer value.
Example for string constants are : “Hello!”, “1998”, “Well done”, “5+3” etc

13) List and specify the meaning of any four backslash character constants.
Backslash characters are used in output functions.
‘\n’ stands for newline.
‘\b’ indicates back space.
‘\0’ indicates null character
‘\t’ indicates horizontal tab
‘\v’ indicates vertical tab

14) Provide the syntax for declaring a variable. Give an example.


Syntax for declaring a variable :
datatype variable_name;

Example for declaring a variable :


int number;
int total,number;

15) What is initialization? Give an example.


Initialization is the process of assigning an initial value to the variable.
Example : int rollno = 101;
Here variable rollno of integer type has been assigned the value 101.

16) What do you mean by symbolic constants? How to define them?


A symbolic constant is a name given to some numeric constant, or a character constant
or string constant, or any other constants. Symbolic constant names are also known as
constant identifiers.
For example constant 3.142 represents the mathematical constant ‘pi’.
A symbolic constant is defined as follows:
#define symbolic_name value of constant
Example :
#define PI 3.14159

17) Provide the syntax of printf(). Give an example.


Syntax :
printf(“control string”, arg1, arg2, …., argn);

Example: int number =10;


printf(“%d”,number);

Here variable number will give an integer value 10 as the output.

18) Provide the syntax of scanf(). Give an example.


Synatx :
scanf(“control string”, arg1, arg2, …..argn);
Example:
scanf(“%d”, &number);

The value read at the terminal will be assigned to the variable a.

19) List any four unformatted I/O functions available in C


Unformatted Input functions are :
getchar();
gets();

Unformatted output functions are :


putchar();
puts();

20) List with use any four format codes used with scanf()/printf().
%d – Format specification for integer type data
%f - Format specification for floating type data
%c – Format specification for character type data
%s – Format specification for the string data
21) Specify the value of x after evaluating following expression
x=3+4-7*8/5%10;
x =3+4-56/5%10
=3+4-11.2%10
=3+4-1.2
=7-1.2
= 5.8

22) Specify the value of x after evaluating following expression float x =1.5;
y=3
x=y/2+y*8/y-y+x/3;

x=3/2+3*8/3-3+1.5/3
x=1.5+3*8/3-3+1.5/3
x=1.5+24/3-3+1.5/3
x=1.5+8-3+1.5/3
x=1.5+8-3+0.5
x=9.5-3+0.5
x=6.5+0.5
x=7

23) Specify the value of x after evaluating following expression int y=3, z=4, x; float
t=4.2; x=z*z/y+y/2*t+2+t;
x =4*4/3+3/2*4.2+2+4.2
=16/3+3/2*4.2+2+4.2
=5.3+3/2*4.2+2+4.2
=5.3+1.5*4.2+2+4.2
=5.3+6.3+2+4.2
=11.6+2+4.2
=13.6+4.2
=17.8
6 MARKS
1) List and Explain the features of C ?
❖ Procedural Language.
❖ Fast and Efficient.
❖ Modularity.
❖ Statically Type.
❖ General-Purpose Language.
❖ Libraries with rich Functions.
❖ Portability
❖ Easy to extend

➢ Procedural Language:
In a procedural language like C step by step predefined instructions
are carried out. C program may contain more than one function to
perform a particular task.
➢ Fast and Efficient:
Newer languages like java, python offer more features than c
programming language but due to additional processing in these
languages, their performance rate gets down effectively. It’s fast
because statically typed languages are faster than dynamically
typed languages.
➢ Modularity:
The concept of storing C programming language code in the form of
libraries for further future uses is known as modularity.
➢ Statically Type:
C programming language is a statically typed language. Meaningthe
type of variable is checked at the time of compilation but not at run
time.

➢ General Purpose Language:


From system programming to photo editing software, the C
programming language is used in various applications .
➢ Libraries with rich Functions:
Robust libraries and functions in C help even a beginner coder to code
with ease.
➢ Portability:
C language is lavishly portable as programs that are written in C
language can run and compile on any system with either none or small
changes.
➢ Easy to Extend:
Programs written in C language can be extended means when a
program is already written in it then some more features and operations
can be added to it.

2)Explain the basic structure of C program.


• Documentation Section
• Link Section
• Definition Section
• Global Declaration Section
main() function
{
DeclarationPart
Executable Part
}
Sub Program Section

function 1
function 2
. User Defined Functions
function n

➢ Documentation section:
The documentation section consists of a set of comment lines giving the
name of the program, the author and otherdetails, which the programmer
would like to use later.
➢ Link section:
The link section provides instructions to the compiler to link functions
from the system Library.

➢ Definition section:
The definition section defines all symbolic constants.

➢ Global Declaration Section:


There are some variables that are used in more than one function. Such
variables are called global variables and are declared in the global
declaration section that is outside of all the functions.

➢ main () function section:


Every C program must have one main function section. This section
contains two parts; declaration part and executable part

• Declaration Part:
The declaration part declares all the variables used in the executable
part.
• Executable Part:
There is at least one statement in the executable part.

➢ Subprogram Section:
The subprogram section contains all the user-definedfunctions that
are called in the main () function.

3) List and explain primary(fundamental) data types in C.


A data type specifies the type of the data that a variable can store.
The fundamental data types are
• Integer(int)
• Character(char)
• Floating point(float)
• Double
• Void
Integer : Integers are the whole numbers. They can be positive, negative or
zero. It allows the variables to store numeric values. Int datatype occupies 4
bytes of memory space.
Ex : 123, -56, 1752 etc.
Char : Char data type allows the variables to store the character data. Character
data can be string or numbers. Size of the character variable is 1 byte.
Ex : ‘J’,2,’A’ etc
Float : Float data type is used to represent floating type values. It allows the
variables to store only real values. They are the numbers with decimal point.
Float datatype occupies 4 bytes of memory.
Ex : 32.45,199.8 etc
Double : Double data type is the double precision floating point. It is called
double because it can hold double the size of the data compared to float type.
While float is 32 bits in size, double is 64 bits in size. Double datatype occupies
8 bytes of memory.
Ex : 6.3456768, -43.65893412, 0.432665898 etc
Void : Void is an empty data type that has no value. Void data type is used in
functions when we don't want to return any value to the calling function. Its a
data type that represents the lack of a data type.

4) Explain in brief Keywords and Identifiers. Give example for


each.
Every C word is classified as either a keyword or an identifier.
All keywords have fixed meanings and it can not be changed. Keywords serve as
basic building blocks for program statements. All the keywords must be written
in lowercase.
Examples : auto, break, case, double, else, int, long, switch, default, continue,
char, const etc

Identifiers refer to the names of variables, functions and arrays. These are user
defined names and consist of a sequence of letters and digits, with a letter as a
first character. Both upper case and lowercase letters are permitted, although
lower case letters are commonly used. The underscore character is also permitted
that is usually used as a link between two words in long identifiers.
Rules for identifiers:
1. First character must be an alphabet.
2. Must consist of only letters, digits and underscore.
3. Only first 31 characters are significant.
4. Can not use a keyword.
5. Must not contain white space.
Examples : sum, amount, name, number etc.
5) Write a note on tokens on C?
The smallest individual units in a C program are known as tokens.
• Keywords
• constants
• Strings
• Operators
• Identifiers
• Special symbols
Keywords : All keywords have fixed meanings and these meanings cannot be
the keywords must be written in lowercase.
Example: break,else,if,switch,for etc

Identifiers :Identifiers refers to the name of the variable, functions and arrays.
These are user defined names and consist of sequence of letters and digits, with
the letter as a first character.
Example : name,sum,number etc
Constants: Constant in C refers to the fixed values that do not change during
the execution of program.
a) Integer constants: An integer constant refers to a sequences of digits. There
are three types of integers namely decimal integer, octal integer & hexadecimal
Integer. Example : 121, 100, -17 etc
b) Real constants: Real constants are also known as floating point constants, are
number that have a whole number followed by a decimal point, followed by the
fraction number. Example : 3.14, 0.008 etc
c) Single character Constants: A single character constant contains single
character enclosed within a pair of a single quote marks. Example : ‘X’, ‘5’ etc.
d) String constants: A string constant is a sequence of character enclosed in
double quotes. the characters maybe letters, numbers, special characters and
blank space. Example : “abc”, “I BCA” etc.
Strings: String is a sequence of characters. String represents text rather than
numbers. It is comprised of a set of character that can also contain space and
numbers.
Ex: “hamburger” , “I ate 3 hamburgers” both are strings.
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 operators such as arithmetic operators, relational operators, logical
operators. Example : + , /, >, && etc.
Special symbols: In c programming language the special symbols have some
special meanings and they cannot be used for other purposes.
some of the special symbols are [], (), {}, ; , *, =, #.
6) Write a note on Constants in ‘C’.
Constant is a fixed value that do not change during the execution of a program.
They are classified as:
a) Numeric constants – Integer constants and real constants
b) Character constants – Single character constants and String constants
Integer constants
Integer constants refer to the sequence of digits.
There are 3 types of integers : Decimal integers, octal integers, hexadecimal
integers.
Integer constants are inadequate to represent quantities such as distances,
heights, prices etc.
Examples for integer constants are : 12, -32, 100 etc
Real constants
Real constants are the numbers with fractional parts.
Unlike Integer constants real constants can represent quantities such as distance,
heights, prices etc.
Examples for real constants are: 0.008, -0.75, 3.14 etc
Single Character constants
A single character constant contains a single character enclosed within single
quotes.
A single character will have an equivalent integer value. For example integer
value of “A” is 65.
Examples for single character constant are : ‘X’, ‘5’, ‘+’, ‘ ‘ etc
String constants
A string constant is a sequence of characters enclosed in double quotes.
The characters may be letters, numbers, special characters and blank spaces.
Unlike single character constant string constant will not have equivalent integer
value.
Example for string constants are : “Hello!”, “1998”, “Well done”, “5+3” etc

7) Explain with examples declaring, initializing and assigning value


to variable.
Variable is the user defined name used to store the variable. Variables must be
declared before they are used in the program. Declaration does two things:
1. It tells the compiler what the variable name is.
2. It specifies what type of data the variable will hold.

Syntax(general form) for declaring the variable name is:

datatype variable_name;
Here variable_name is the name of the varaible. Variables are separated by
commas. A declaration statement must end with a semicolon.

Example : int number,total;


float avg;
Here number and total are the variables holding integer type values and avg is
the variable which stores the float type value.

Initialization and assigning value to the variable


Initialization is the process of assigning value to the variable. Values are
assigned to the variable using the assignment operator = .
Syntax :
variable_name = value;
Example :
total = 100;
avg = 98.87;
Here integer value 100 has been assigned to variable total. Floating value 98.87
has been assigned to the variable avg.
C also permits assignments in one line : total=100; avg=98.87;

It is also possible to assign a value to the variable in the declaration part.


Syntax : datatype variable_name=value;
Example : int total=100;
double balance = 45.78;
Integer value 100 and double value 45.78 have been assigned to the variables
total and balance respectively.

8) Explain scanf() function with its syntax and example.


scanf() function is the formatted input statement which is used to read the
input data. scanf()
function allows us to input data into the variable. There are a lot of options
available for reading the data using scanf function.
General form(syntax) :
scanf(“control string”,arg1,arg2,…..argn);
The control string specifies the format in which the data is to be entered. For
example, if the data is of integer type the field specification is %d , for
character type its %c. The arguments arg1,arg2,…arg1 specify the variable
names where the data is stored. Control strings and arguments are separated
by commas. The field specification directs to input the data in the particular
type.

Example : scanf(“%d %d”, &num1,&num2);


This statement will allow the user to input the data at the terminal(output
screen). Suppose the data inputted is as follows:
50 12
Then, the value 50 will be assigned to the first variable num1 and 12 will be
stored in num2.
%d is the field specification for the integer type which allows to input only
integer type of data.
The input data must be separated by spaces, tabs and newlines.
scanf(“%f %c”,&a,&b);
Here %f allows to input only floating type values and %c allows to input
character type data. scanf() statement is useful if we want to input mixed
types of data in a single statement.

9) Explain printf() function with its syntax and example.


printf() function is the formatted output statement which is used to write the
data to the screen. printf() statement displays the characters written inside the
double quote on the output screen as they appear.
General form(syntax) :
printf(“control string”,arg1,arg2,…..argn);
Control string consists of three types of items:
1. Characters that will be printed on the screen as they appear.
2. Format specification specifies the type of the output that has to be
displayed for each variable.
3. Escape sequence characters such as \n and \t.
The control string indicates how many arguments are there what their data
types are. The arguments arg1,arg2,…argn are the variables whose values
need to be printed according to the specification of the control string. The
arguments should match in the number, order and type with the format
specifications.
Examples : printf(“Programming in C”);
This will print the statement as it is how inside the double quotes.
main()
{
int x=2;
printf(“%d”,x);
}
This will print the integer value that has been stored inside the variable x.
So the value 2 will be printed.
Let us take one more example :
main()
{
int x=2;
printf(“Value of x = %d”,x);
}

Here the output looks like this : Value of x = 2


The statement written inside the double quotes will be printed as it is, in
the place of format specification %d the value that stored inside the
variable x will be printed.

10)Explain with example reading, writing a character and strings


in C.
Reading and writing the character
The simplest of all input/output operations is reading a character and writing a
character.
• Reading a single character can be done using the function
getchar.(unformatted input)
General form : variable_name=getchar();
Here variable name is the user defined name that has been declared as the
character type. When this statement is encountered the computer waits until a
key is pressed and the entered character will be assigned to the variable.
Example : char name;
name = getchar();
Here name is the variable name that has been declared as the character type.
getchar() function is the unformatted input statement that allows us to input the
character through the keyboard. If character ‘J’ is pressed then the character J
will be assigned to the variable name.

• Writing the character to the screen is done using putchar()


function.(unformatted output)
General form : putchar(variable_name);
Where variable name is the name that is declared as the character type. putchar()
function displays the character stored in the variable name in the terminal.
Example :
char answer = ‘Y’;
putchar(answer);
This will display the character Y which has been stored inside the character type
variable answer.
Reading and writing strings
• scanf() function is used to input the string containing more than one
character. %s is the specification for reading the string.
Example : char name[10];
scanf(“%s”, name);
Here character array is equivalent to string. scanf is used to input the a string
value. Using scanf function we can input only a single word as soon as the it
encounters a white space it will be terminated.
For example if we input “information technology” scanf will read only the first
word that information, rest of the thing will be ignored.

Multiple strings or a sentence can be read using gets() function which is an


unformatted input statement.
Example :
char name[20];
gets(name);
For example if we input “C programming lab” , gets() will read the complete
sentence.
• printf() function is used to print the string containing more than one
characters.
Example : char name[10] = “programming”;
printf(“%s”,name);
This will print the string “programming” on the output string. Same as
the scanf() function printf() function is used only to print a single string.
Multiple strings can not printed using printf() function.

Multiple strings or a sentence can be printed using puts() function which


is an unformatted ouput statement.
Example : char name[20]=”C programming lab”;
puts(name);
Which will print “ C programming lab” in the output string.

You might also like