Basic Computer Theroy Notes

You might also like

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

2013

Q.Explain the basic structure of C with examples.


Following is the basic structure of a C program.

Documentation Consists of comments, some description of the program, programmer name and any other
useful points that can be referenced later.

Link Provides instruction to the compiler to link function from the library function.

Definition Consists of symbolic constants.

Global Consists of function declaration and global variables.


declaration

main( ) Every C program must have a main() function which is the starting point of the program
{ execution.

Subprograms User defined functions.

Lets explore the sections with an example.

Write a program to print area of a circle.


In the following example we will find the area of a circle for a given radius 10cm.

Formula
The formula to compute the area of a circle is πr2 where π is PI = 3.1416 (approx.) and r is the
radius of the circle.
Lets write the C code to compute the area of the circle.

/**
* file: circle.c

* author: yusuf shakeel

* date: 2010-11-25

* description: program to find the area of a circle

* using the radius r

*/

#include <stdio.h>

#define PI 3.1416

float area(float r);

int main(void)

float r = 10;

printf("Area: %.2f", area(r));

return 0;

float area(float r) {

return PI * r * r;
}

The above code will give the following output.

Area: 314.16

Different sections of the above code

Documentation
This section contains a multi line comment describing the code.

/**

* file: circle.c

* author: yusuf shakeel

* date: 2010-11-25

* description: program to find the area of a circle

* using the radius r

*/

In C, we can create single line comment using two forward slash // and we can create multi line
comment using /* */.
Comments are ignored by the compiler and is used to write notes and document code.

Link
This section includes header file.

#include <stdio.h>

We are including the stdio.h input/output header file from the C library.

Definition
This section contains constant.
#define PI 3.1416

In the above code we have created a constant PI and assigned 3.1416 to it.
The #define is a preprocessor compiler directive which is used to create constants. We
generally use uppercase letters to create constants.
The #define is not a statement and must not end with a ; semicolon.

Global declaration
This section contains function declaration.

float area(float r);

We have declared an area function which takes a floating number (i.e., number with decimal
parts) as argument and returns floating number.

main( ) function
This section contains the main() function.

int main(void)

float r = 10;

printf("Area: %.2f", area(r));

return 0;

This is the main() function of the code. Inside this function we have created a floating
variabler and assigned 10 to it.
Then we have called the printf() function. The first argument contains "Area:
%.2f" which means we will print floating number having only 2 decimal place. In the second
argument we are calling the area() function and passing the value of r to it.

Subprograms
This section contains a subprogram, an area() function that is called from the main()function.
float area(float r) {

return PI * r * r;

This is the definition of the area() function. It receives the value of radius in variable r and


then returns the area of the circle using the following formula PI * r * r.

Q.Explain increment and decrement operator of C with examples.

Increment and Decrement Operator in C

Increment and decrement operators are unary operators that add or subtract one


from their operand, respectively. They are commonly implemented in imperative
programming languages. ... The increment operator increases the value of its
operand by 1.

+6
+---./’/

Q.Variable Definition in C
A variable definition tells the compiler where and how much storage to
create for the variable. A variable definition specifies a data type and
contains a list of one or more variables of that type as follows −

r.No. Type & Description


1 Char

Typically a single octet(one byte). This is an integer type.

2 Int

The most natural size of integer for the machine.

3 Float

A single-precision floating point value.

4 Double

A double-precision floating point value.

5 Void

Represents the absence of type.

2014

Q.What are the rational operators in C

The following table shows all the relational operators supported by C


language. Assume variable A holds 10 and variable B holds 20 then −

Operator Description Example

== Checks if the values of two operands are equal (A == B)


or not. If yes, then the condition becomes true. is not
true.

!= Checks if the values of two operands are equal (A != B)


or not. If the values are not equal, then the is true.
condition becomes true.
> Checks if the value of left operand is greater (A > B)
than the value of right operand. If yes, then the is not
condition becomes true. true.

< Checks if the value of left operand is less than (A < B)


the value of right operand. If yes, then the is true.
condition becomes true.

>= Checks if the value of left operand is greater (A >= B)


than or equal to the value of right operand. If is not
yes, then the condition becomes true. true.

<= Checks if the value of left operand is less than or (A <= B)


equal to the value of right operand. If yes, then is true.
the condition becomes true.

Example
Try the following example to understand all the relational operators
available in C −
 Live Demo

#include <stdio.h>

main() {

int a = 21;

int b = 10;

int c ;

if( a == b ) {

printf("Line 1 - a is equal to b\n" );


} else {

printf("Line 1 - a is not equal to b\n" );

if ( a < b ) {

printf("Line 2 - a is less than b\n" );

} else {

printf("Line 2 - a is not less than b\n" );

if ( a > b ) {

printf("Line 3 - a is greater than b\n" );

} else {

printf("Line 3 - a is not greater than b\n" );

/* Lets change value of a and b */

a = 5;

b = 20;

if ( a <= b ) {

printf("Line 4 - a is either less than or equal to b\n" );

if ( b >= a ) {
printf("Line 5 - b is either greater than or equal to b\n" );

When you compile and execute the above program, it produces the
following result −

Line 1 - a is not equal to b


Line 2 - a is not less than b
Line 3 - a is greater than b
Line 4 - a is either less than or equal to b
Line 5 - b is either greater than or equal to b

Relational operators: Relational operators are


used for comparison of to variables of same data type. These operators are used in the
conditions where two values are compared and true or false is returned according to test
result.
Example: 3 > 5 returns you false and 3 < 5 returns you true, which can be used in if
statement.

Q.Explain the different arithmetic operators used in C

Arithmetic Operators in C

The Arithmetic operators are some of the C Programming Operator, which are used to
perform arithmetic operations includes operators like Addition, Subtraction,
Multiplication, Division and Modulus. All these operators are binary operators which
means they operate on two operands. Below table shows all the Arithmetic Operators
in C Programming with examples.

Q.Explain different constants and variables


In this C programming language tutorial we take a look at variables and
constants.

Variables
If you declare a variable in C (later on we talk about how to do this), you
ask the operating system for a piece of memory. This piece of memory
you give a name and you can store something in that piece of memory
(for later use). There are two basic kinds of variables in C which are
numeric and character.
Numeric variables
Numeric variables can either be of the type integer (int) or of the type
real (float). Integer (int) values are whole numbers (like 10 or -10). Real
(float) values can have a decimal point in them. (Like 1.23 or -20.123).

Character variables
Character variables are letters of the alphabet, ASCII characters or
numbers 0-9. If you declare a character variable you must always put the
character between single quotes (like so ‘A’ ). So remember a number
without single quotes is not the same as a character with single quotes.

Constants
The difference between variables and constants is that variables can
change their value at any time but constants can never change their
value. (The constants value is lockedfor the duration of the program).
Constants can be very useful, Pi for instance is a good example to declare
as a constant.

Data Types
So you now know that there are three types of variables: numeric –
integer, numeric-real and character. A variable has a type-name, a type
and a range (minimum / maximum). In the following table you can see
the type-name, type and range:

Type-
Type Range
name

Int Numeric – Integer -32 768 to 32 767

Short Numeric – Integer -32 768 to 32 767


-2 147 483 648 to
Long Numeric – Integer
2 147 483 647
1.2 X 10-38 to 3.4 X
Float Numeric – Real
1038
2.2 X 10-308 to 1.8
Double Numeric – Real
X 10308
All ASCII
Char Character
characters

Q.What are the input and output statements in C

Input : In any programming language input means to feed some data into program. This can be
given in the form of file or from command line. C programming language provides a set of built-
in functions to read given input and feed it to the program as per requirement.

Output : In any programming language output means to display some data on screen, printer or
in any file. C programming language provides a set of built-in functions to output required data.

Q.What are the different data types in C? What is the local and global variable?
How do you declare the global and local variable 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 datatypes have different storage capacities.

C language supports 2 different type of data types:

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 datatypes but a little twisted or grouped
together like array, structure, union and pointer. 
A local variable is a variable that is declared inside a function.
A global variable is a variable that is declared outside all functions.
A local variable can only be used in the function where it is declared.
A global variable can be used in all functions.

A local variable is a variable which is either a variable declared within the


function or is an argument passed to a function. As you may have encountered in your
programming, if we declare variables in a function then we can only use them within
that function.

A global variable is a variable that is declared outside all functions. A


local variable can only be used in the function where it is declared. A global variable
can be used in all functions. As you can see two global variables are declared, A and
B. These variables can be used in main() and Add()

Q.What are the different conditional operators and write their purposes.

There are three conditional operators

1. && the logical AND operator


2. !!the logical OR operator
3. ?: the ternary operator
Purposes
1. The conditional operator expression can be used as shorthandfor some if-else
statement.
2. Conditional operator is also called as ternary operator.
3. This operator consists of two symbols: the question mark(?) and colon(:)
4. If the first operand evaluates to true (1) The second operand is evaluated
5. If the third operand evaluates to false (0)the third operand is evaluated.

Q.Write down the rules for constructing the variable names.

Rules for constructing variable names


1) A Variable name consists of any combination of alphabets, digits and underscores.
Some compiler allows variable names whole length could be up to 247 characters. Still
it would be safer to stick to the rule of 31 characters. Please avoid creating long
variable name as it adds to your typing effort.
2) The first character of the variable name must either be alphabet or underscore. It
should not start with the digit.
3) No commas and blanks are allowed in the variable name.
4) No special symbols other than underscore are allowed in the variable name.

Q.What are the types of C constants

C supports several types of constants.      

Integer Constants 
 An integer constant is a sequence of digits from 0 to 9 without decimal points
or fractional part or any other symbols. There are 3 types of integers namely decimal
integer, octal integers and hexadecimal integer.
Decimal Integers consists of a set of digits 0 to 9 preceded by an optional + or - sign.
Spaces, commas and non digit characters are not permitted between digits. Example
for valid decimal integer constants are
     int y=123; //here 123 is a decimal integer constant
Octal Integers constant consists of any combination of digits from 0 through 7 with a O
at the beginning. Some examples of octal integers are
     int X=O123; // here 0123 is a octal integer constant .
Hexadecimal integer constant is preceded by OX or Ox, they may contain alphabets
from A to F or a to f. The alphabets A to F refers to 10 to 15 in decimal digits. Example
of valid hexadecimal integers are
     int x=Ox12 // here Ox12 is a Hexa-Decimal integer constant
Real Constants 
Real Constants consists of a fractional part in their representation. Integer constants are
inadequate to represent quantities that vary continuously. These quantities are
represented by numbers containing fractional parts like 26.082. Example of real
constants are
  float x = 6.3; //here 6.3 is a double constant.
  float y = 6.3f; //here 6.3f is a float constant.
  float z = 6.3 e + 2; //here 6.3 e + 2 is a exponential constant.
  float s = 6.3L ; //here 6.3L is a long double constant
Real Numbers can also be represented by exponential notation. The general form for
exponential notation is mantissa exponent. The mantissa is either a real number
expressed in decimal notation or an integer. The exponent is an integer number with an
optional plus or minus sign.

Single Character Constants 


A Single Character constant represent a single character which is enclosed in a pair of
quotation symbols.
Example for character constants are
char p ='ok' ;  // p will hold the value 'O' and k will be omitted
char y ='u';    // y will hold the value 'u'
char k ='34' ; // k will hold the value '3, and '4' will be omitted
char e =' ';     // e will hold the value ' ' , a blank space
chars ='\45'; // swill hold the value ' ' , a blank space
All character constants have an equivalent integer value which are called ASCII Values.

String Constants
A string constant is a set of characters enclosed in double quotation marks. The
characters in a string constant sequence may be a alphabet, number, special character
and blank space. Example of string constants are
  "VISHAL"   "1234"  "God Bless" "!.....?"

Backslash Character Constants [Escape Sequences]


Backslash character constants are special characters used in output functions. Although
they contain two characters they represent only one character. Given below is the table
of escape sequence and their meanings.
 

You might also like