[go: up one dir, main page]

0% found this document useful (0 votes)
12 views3 pages

Chapter 1 LQ

Uploaded by

faiq3239
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)
12 views3 pages

Chapter 1 LQ

Uploaded by

faiq3239
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/ 3

CHAPTER: 01

INTRODUCTION TO PROGRAMMING
1. Discuss the main parts of the structure of a C Program.
Definition:
“How a C program is built, arranged and organized is called the structure of a C program.”
The sequence of statements in the program should be according to the sequence in which we want
our program to be executed.
Sections/ Parts of the C program
A program is divided into different sections. Each code has a similar outline. There are three main
sections to a basic C program which are as follows:
(i) Link Section:
This part of the code is used to declare all the header files that will be used in the program. While
writing C programs, we extensively use built-in functions and header files are included in which these
functions are declared. The “include” keyword is used to include a header file in our program. The
include statement is always written at the top of the program.
Syntax:
#include<header filer name>
Example:
#include<stdio.h>
(ii) Main section:
It consists of the main function. Every C program must contain the main function because the execution
of the C program starts from it.
(iii) Body of a main function:
It contains the actual code to be executed. It starts from left curly brace “{“ and ends with right curly
brace “}”.
Example:
#include<stdio.h>
Int main( )
{
printf(“Hello Pakistan”);
return 0;
}

Section Explanation
Link section #include<stdio>
Main section int main
Body of the main {
function printf(“Hello Pakistan”);
return 0;
}
2. Describe Purpose and Syntax of Comments in C Programs. Also explain their types.
Comments:
“The statements in a program that are ignored by the compiler and do not get executed are called
comments.”
Usually, comments are written in natural language like in English language to provide description of
our code. We do not want these statements to be executed, because it may cause syntax error as the
statements are written in natural language.

Purpose of writing comments:

Comments can be thought of as documentation of the program.


 They facilitate other programmers to understand our code.
 They help us to understand our own code even after years of writing it.
Syntax of writing comments:
In C programming language, there are two types of comments:
(i) Single-line comments:
They start with //. Anything after // on the same line is considered a comment.
Example:
// This is a comment.
(ii) Multi-line comments:
They start with /* and end at */. Anything between /* and */ is considered a comment even on
multiple lines.
Example:
/*this is
a multi-line
comment*/
3. What is a constant? Explain different types of constants in C.
Definition:
“The quantity that cannot be changed during program execution is called a constant.”
Types of constants:
There are three types of constants available in C which are as follows:
(i) Integer constants:
“The constants that do not contain any decimal value are called integer constants.”
Examples:
 2  10  -3
(ii) Real constants:
“The constants that contain decimal points are called real constants.”
Examples:
 2.3  .001  -7.86
(iii) Character constants:
“Any single small case letter, upper case letter, digit, punctuation mark, or special symbol
enclosed within ‘ ‘ (single inverted commas) is called a character constant.”
Examples:
 ‘3’  ‘A’  ‘x’
Note:
A digit used as a character constant i.e. ‘9’, is different from a digit used as an integer constant i.e. 9.
We can add two integer constants to get the obvious mathematical result e.g. 9+8=17, but we cannot
add a character constant to another character constant to get the obvious mathematical result e.g.
‘9’+‘8’≠17.
4. What is a variable? Write down the properties and rules for naming variables in C with
examples.
Definition:
“The quantity that can be changed during program execution Is called a variable.”
These are named memory locations that are used to store input or output. The variables are created in
RAM that’s why they store data temporarily.
Properties of a variable
There are three types of variables available in C e.g., int, float, and char etc. Each variable has the
following four properties:
(i) Name Each variable is assigned a unique name which is called its identifier.
(ii) Address The location of the variable in the main memory.
(iii) Data type The type of value stored in the variable.
(iv) Value The value in the variable.
Rules for naming variables
The following are rules for naming variables in C:
(i) A variable name can only contain digits, underscore, or alphabets (uppercase or lowercase).
Examples:
 xy  ab3  ob_marks
(ii) The first character of a variable name must be an alphabet or an underscore.
Examples:
 age  _marks
(iii) A keyword cannot be used as a variable name.
Examples:
 for  if
They are invalid variable names.
(iv) Space is not allowed in a variable name.
Example:
 my salary is an invalid variable name.
(v) No special symbol is allowed in a variable name except underscore.
Example:
 marks% is an invalid variable name
(vi) The maximum length of a variable name accepted by a C compiler is 32. If only a variable name
consists of 40 characters, then only the first 32 characters are considered.
5. What is the data type of a variable? List different categories of data type in C.
Each variable in C language has a data type. The data type not only describes the type of data to be
stored inside the variable but also the number of bytes that the compiler needs to reserve for data
storage.
The following are different data types provided by the C language:
(i) int data type:
“The data type which is used to store whole numbers is called int data type.”
The numbers can be positive or negative. The negative values are identified by a minus (-) sign before
the value. A variable of int data type takes 04 bytes in memory but in few compilers, it takes 02 bytes of
memory. The keyword “int” is used to declare a variable of int data type.
Types of int data type:
This data type is further categorized into the following types:
signed int unsigned int
 The signed int can store both positive and  The unsigned int can store only positive
negative values. values.
 The range and values for signed int is  The range of values for unsigned int is 0 to
2147483648 to 2147483647. 4294967295.
 To declare a signed int variable the  To declare unsigned int the keyword
keyword “int” is used. “unsigned int” is used.
(ii) float data type:
“The data type which is used to store real numbers is called float data type.”
It has a precision of up to 6 decimal points. A variable of this data type takes 04 bytes in memory. The
range of values for the float data type is 3.4×10-38 to 3.4×1038. The keyword “float” is used to declare a
variable of float data type.
(iii) char data type:
“The data type which is used to store a single character is called char data type.”
A variable of this data type takes 01 bytes in memory that’s why it can store one character only. The
keyword “char” is used to declare a variable of char data type.

You might also like