Computer is a machine that can perform computation
Input:User gives a set of Input
Processing:Input data is processed by a well defined and
finite sequence of steps
Output:Some data available from processing step and output to
the user
Figure 2.0:
Programming and Software
Computer needs to be programmed to do such tasks:-
Programming: is the process of writing instructions in
a language that can be understood by the computer so that a desired
task can be performed by it Program: sequence of instructions to do a
task,computer processes the instructions sequentially one after the
other Software: programs for doing tasks on computers
Structure Of C Program
A collection of functions (we will see what they are later)
Exactly one special function named main must be present.Program
always starts from there
Each function has statements (instructions) for declaration,
assignment, condition check,looping etc.
Statements are executed one by one.
Variables
Very important concept for programming.
Variables stored in memory.
An entity that has a value and is known to the program by a name.
It Can store any temporary result while executing a program.
It Can have only one value assigned to it at any given time during
the execution of the program.
The value of a variable can be changed during the execution of the
program.
Remember that memory is a list of storage locations,each having a
unique address.
A variable is like a bin:-
The contents of the bin is the value of the variable.
The variable name is used to refer to the value of the variable.
A variable is mapped to a location of the memory,called its
address.
Variables in Memory
Variables in memory
How to give Names of Variable
Sequence of letters and digits
First character must be a letter or _
No special characters other than _
No blank in between
Names are case-sensitive (max and Max are two different names)
Examples of valid names:
rank1, MAX, max, Min, class_rank
Examples of invalid names:
's ,fact rec,2sqroot,class?rank
Constants
Integer constants
Consists of a sequence of digits, with possibly a plus or a minus
sign before it
Embedded spaces, commas and non-digit charactersare not permitted
between digits
Floating point constants
Two different notations:
Decimal notation: 25.0, 0.0034, .84, -2.234
Exponential (scientific) notation 3.45e23, 0.123e-12, 123e2 e
means 10 to the power of
Character constants
Contains a single character enclosed within a pair of single quote
marks.
Used by the C language, cannot be used as variable names
Examples:
int, float, char, double, main, if else, for, while,do, struct,
union, typedef, enum, void, return,signed, unsigned, case,
break, sizeof etc.
There are others, see textbook.
Input:Scanf function
Performs input from keyboard
It requires a format string and a list of variables into which the
value received from the keyboard will be stored
format string = individual groups of characters(usually
'%' sign, followed by a conversion character),with one
character group for each variable in the list
int a, b;
float c;
scanf(%d %d %f, &a, &b, &c);
Commonly used conversion characters
c :for char type variable
d :for int type variable
f :for float type variable
lf :for double type variable
Examples:
scanf("%d", &size) ;
scanf("%c", nextchar) ;
scanf("%f", &length) ;
scanf("%d%d", &a, &b);
Reading a single character
A single character can be read using scanf with %c
It can also be read using the getchar() function
Program waits at the getchar() line until a character is typed, and
then reads it and stores it in c
char c;
c = getchar();
Data types
Each variable has a type, indicates what type of values the variable
can hold
Four common data types in C:
int - can store integers (usually 4 bytes)
float - can store single-precision floating point numbers
(usually 4 bytes)
double - can store double-precision floating point numbers
(usually 8 bytes)
char- can store a character (1 byte)
Must declare a variable (specify its type and name) before using it
anywhere in your program
All variable declarations should be at the beginning of the main()
or other functions
A value can also be assigned to a variable at the time the variable
is declared.
int speed = 30;
char flag = ' y ' ;
Some typical sizes (some of these can vary depending on type of
machine)
Chapter 2 Quiz
1.How much memory Size takes char,int,float :& double datatypes in
memory
2. scanf() is a predefined function in which header file?
3.What is the Range of Char Data Types
4.What outputs does it generates:printf ( "%d" , printf ( "helloWorld" ) );
5.Which of the data types has the size that is variable?