Professional Documents
Culture Documents
Basic Computer Theroy Notes
Basic Computer Theroy Notes
Basic Computer Theroy Notes
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.
main( ) Every C program must have a main() function which is the starting point of the program
{ execution.
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
* date: 2010-11-25
*/
#include <stdio.h>
#define PI 3.1416
int main(void)
float r = 10;
return 0;
float area(float r) {
return PI * r * r;
}
Area: 314.16
Documentation
This section contains a multi line comment describing the code.
/**
* file: circle.c
* date: 2010-11-25
*/
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>
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.
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;
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;
+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 −
2 Int
3 Float
4 Double
5 Void
2014
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 ) {
if ( a < b ) {
} else {
if ( a > b ) {
} else {
a = 5;
b = 20;
if ( a <= b ) {
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 −
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.
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
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.
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.
Q.What are the different conditional operators and write their purposes.
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.
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" "!.....?"