0% found this document useful (0 votes)
28 views34 pages

Unit 1

Uploaded by

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

Unit 1

Uploaded by

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

21CB101 PROBLEM

SOLVING AND C
PROGRAMMING
UNIT-1
PROBLEM SOLVING AND C
FUNDAMENTALS
#include <stdio.h>

int main ()
{
printf(“Welcome to 21CB101!\n”);
}
History of C
1960: ALGOL (ALGOrithmic Language)
1967: BCPL (Basic Combined Programming
Language)
1970: B programming language (typeless)
1972: C: BCPL plus B with types
1978: Kernighan + Ritchie standard for C
1989: ANSI standard for C
C Program Structure
Preprocessor Directives
• Program defined by:
– global declarations
Global Declarations
– function definitions
Function Definitions • May contain preprocessor
int main () {
directives
Local Declarations
• Always has one function
Statements named main, may contain
}
others
Parts of a Program

#include <stdio.h> Preprocessor Directive

int x; Global Declaration

int main () {
int y; Local Declaration
Function printf("Enter x and y: ");
scanf(&x,&y); Statements
printf("Sum is %d\n",x+y);
}
Preprocessor Directives
• Begin with #
• Instruct compiler to perform some
transformation to file before compiling
• Example: #include <stdio.h>
– add the header file stdio.h to this file
– .h for header file
– stdio.h defines useful input/output functions
Declarations
• Global
– visible throughout program
– describes data used throughout program
• Local
– visible within function
– describes data used only in function
Functions
• Consists of header and body
– header: int main ()
– body: contained between { and }
• starts with location declarations
• followed by series of statements
• More than one function may be defined
• Functions are called (invoked) - more later
Main Function
• Every program has one function main
• Header for main: int main ()
• Program is the sequence of statements
between the { } following main
• Statements are executed one at a time from
the one immediately following to main to
the one before the }
Comments
• Text between /* and */
• Used to “document” the code for the human
reader
• Ignored by compiler (not part of program)
• Have to be careful
– comments may cover multiple lines
– ends as soon as */ encountered (so no internal
comments - /* An /* internal */ comment */)
Comment Example
#include <stdio.h>

/* This comment covers


* multiple lines
* in the program.
*/

int main () /* The main header */ {


/* No local declarations */

printf(“Too many comments\n”);


} /* end of main */
Syntax of C
• Rules that define C language
– Specify which tokens are valid
– Also indicate the expected order of tokens
• Some types of tokens:
– reserved words: include printf int ...
– identifiers: x y ...
– literal constants: 5 ‘a’ 5.0 ...
– punctuation: { } ; < > # /* */
Identifier
• Names used for objects in C
• Rules for identifiers in C:
– first char alphabetic [a-z,A-Z] or underscore (_)
– has only alphabetic, digit, underscore chars
– first 31 characters are significant
– cannot duplicate a reserved word
– case (upper/lower) matters
Valid/Invalid Identifiers
Valid Invalid
sum 7of9
c4_5 x-name
A_NUMBER name with spaces
longnamewithmanychars 1234a
TRUE int
_split_name AXYZ&
Program Execution
• Global declarations set up
• Function main executed
– local declarations set up
– each statement in statement section executed
• executed in order (first to last)
• changes made by one statement affect later
statements
Variables
• Named memory location
• Variables declared in global or local
declaration sections
• Syntax: Type Name;
• Examples:
int sum;
float avg;
char dummy;
Variable Type
• Indicates how much memory to set aside for
the variable
• Also determines how that space will be
interpreted
• Basic types: char, int, float
– specify amount of space (bytes) to set aside
– what can be stored in that space
– what operations can be performed on those vars
Literal Constants
• Sequences of characters (tokens) that
correspond to values from that type
-35 is the integer -35
3.14159 is the floating pointer number 3.14159
‘A’ is the character A
• Can be used to initialize variables
Integer Type
• Type name:
– int
– short int
– long int
• Possible values: whole numbers (within
given ranges) as in 5, -35, 401
• Operations: arithmetic (addition,
subtraction, multiplication, …), and others
Character Type
• Type name: char
• Possible values: keys that can be typed at the
keyboard
• Representation: each character assigned a
value (ASCII values), 8 bits
– A - binary number 65
– a - binary number 97
– b - binary number 98
– 2 - binary number 50
Constants
• Literal constants - tokens representing
values from type
• Defined constants
– syntax: #define Name Value
– preprocessor command, Name replaced by
Value in program
– example: #define MAX_NUMBER 100
Constants (cont)
• Memory constants
– declared similar to variables, type and name
– const added before declaration
– Example: const float PI = 3.14159;
– Can be used as a variable, but one that cannot
be changed
– Since the value cannot be changed, it must be
initialized
Formatted Input/Output
• Input comes from files
• Output sent to files
• Other objects treated like files:
– keyboard - standard input file (stdin)
– monitor - standard output file (stdout)
• Generally send/retrieve characters to/from
files
Formatted Output
• Command: printf - print formatted
• Syntax: printf(Format String, Data List);
– Format string any legal string
– Characters sent (in order) to screen
• Ex.: printf(“Welcome to\nCS 1621!\n”);
causes
Welcome to
CS 1621!
to appear on monitor
Formatted Output (cont)
• Successive printf commands cause output to
be added to previous output
• Ex.
printf(“Hi, how “);
printf(“is it going\nin 1621?”);
prints
Hi, how is it going
in 1621?
To the monitor
Formatted Input
• Command: scanf - scan formatted
• Syntax: scanf(Format String, Address List);
– Format string a string with one or more field
specifications
– Characters read from keyboard, stored in
variables
• scanf(“%c %d %f”,&cVar,&dVar,&fVar);
attempts to read first a single character, then a
whole number, then a floating point number
from the keyboard
Formatted Input (cont)
• Generally only have field specifications and
spaces in string
– any other character must be matched exactly (user
must type that char or chars)
– space characters indicate white-space is ignored
– “white-space” - spaces, tabs, newlines
– %d and %f generally ignore leading white space
anyway (looking for numbers)
– %d and %f read until next non-number char
reached
Formatted Input (cont)
• More notes
– can use width in field specifications to indicate
max number of characters to read for number
– computer will not read input until return typed
– if not enough input on this line, next line read,
(and line after, etc.)
– inappropriate chars result in run-time errors (x
when number expected)
– if end-of-file occurs while variable being read, an
error occurs
Operators in C
C programming has wide range of operators to perform
various operations. For better understanding of operators,
these operators can be classified as:
• Arithmetic Operators
• Increment and Decrement Operators
• Assignment Operators
• Relational Operators
• Logical Operators
• Conditional Operators
• Bitwise Operators
• Special Operators
Arithmetic Operator
• Operator Meaning of Operator
• + addition or unary plus
• - subtraction or unary minus
• * multiplication
• / division
• % remainder after
division( modulo division)
Increment and Decrement
Operators
1. C programming has two operators increment ++ and
decrement -- to change the value of an operand (constant
or variable) by 1.
2. Increment ++ increases the value by 1 whereas
decrement -- decreases the value by 1.
3. These two operators are unary operators, meaning they
only operate on a single operand.
eg. int a=10, b=100
++a = 11
--b = 99
C Assignment Operators
• An assignment operator is used for assigning a value to a
variable. The most common assignment operator is =
• Operator Example Same as
• = a=b a=b
• += a += b a = a+b
• -= a -= b a = a-b
• *= a *= b a = a*b
• /= a /= b a = a/b
• %= a %= b a = a%b
C Relational Operators
• A relational operator checks the relationship between two
operands. If the relation is true, it returns 1; if the relation is false,
it returns value 0.
• Relational operators are used in decision making and loops.
Operator Meaning of Operator Example
• == Equal to 5 == 3 returns 0
• > Greater than 5 > 3 returns 1
• < Less than 5 < 3 returns 0
• != Not equal to 5 != 3 returns 1
• >= Greater than or equal to 5 >= 3 returns 1
• <= Less than or equal to 5 <= 3 return 0

You might also like