A Structured Programming Approach Using C1
Objectives
❏ To understand the structure of a C-language program.
❏ To write your first C program.
❏ To introduce the include preprocessor command.
❏ To be able to create good identifiers for objects in a program.
❏ To be able to list, describe, and use the C basic data types.
❏ To be able to create and use variables and constants.
❏ To understand input and output concepts.
❏ To be able to use simple input and output statements.
Introduction to the C LanguageIntroduction to the C Language
A Structured Programming Approach Using C2
2-1 Background
C is a structured programming language. It isC is a structured programming language. It is
considered a high-level language because it allows theconsidered a high-level language because it allows the
programmer to concentrate on the problem at handprogrammer to concentrate on the problem at hand
and not worry about the machine that the programand not worry about the machine that the program
will be using. That is another reason why it is used bywill be using. That is another reason why it is used by
software developers whose applications have to run onsoftware developers whose applications have to run on
many different hardware platforms.many different hardware platforms.
A Structured Programming Approach Using C3
2-2 C Programs
It's time to write your first C program.It's time to write your first C program.
Structure of a C Program
Your First C Program
Comments
The Greeting Program
Topics discussed in this section:Topics discussed in this section:
A Structured Programming Approach Using C8
FIGURE 2-5 Examples of Line Comments
#include <stdio.h>
// program prints a number of type int
int main() {
int number = 4;
printf (“Number is %d”, number);
return 0;
}
Output: Number is 4
A Structured Programming Approach Using C9
Errors
Compilation
Compiler generally gives the line number at
which the error is present.
Run time
C programs are sequential making the
debugging easier.
A Structured Programming Approach Using C11
2-3 Identifiers
One feature present in all computer languages is theOne feature present in all computer languages is the
identifier. Identifiers allow us to name data and otheridentifier. Identifiers allow us to name data and other
objects in the program. Each identified object in theobjects in the program. Each identified object in the
computer is stored at a unique address.computer is stored at a unique address.
A Structured Programming Approach Using C13
An identifier must start with a letter or underscore:
it may not have a space or a hyphen.
NoteNote
C is a case-sensitive language.
A Structured Programming Approach Using C15
2-4 Types
A type defines a set of values and a set of operationsA type defines a set of values and a set of operations
that can be applied on those values.that can be applied on those values.
Void Type
Integral Type
Floating-Point Types
Topics discussed in this section:Topics discussed in this section:
A Structured Programming Approach Using C24
2-5 Variables
Variables are named memory locations that have a type,Variables are named memory locations that have a type,
such as integer or character, which is inherited fromsuch as integer or character, which is inherited from
their type. The type determines the values that a variabletheir type. The type determines the values that a variable
may contain and the operations that may be used withmay contain and the operations that may be used with
its values.its values.
Variable Declaration
Variable Initialization
Topics discussed in this section:Topics discussed in this section:
A Structured Programming Approach Using C28
When a variable is defined, it is not initialized.
We must initialize any variable requiring
prescribed data when the function starts.
NoteNote
A Structured Programming Approach Using C32
2-6 Constants
Constants are data values that cannot be changedConstants are data values that cannot be changed
during the execution of a program. Like variables,during the execution of a program. Like variables,
constants have a type. In this section, we discussconstants have a type. In this section, we discuss
Boolean, character, integer, real, complex, and stringBoolean, character, integer, real, complex, and string
constants.constants.
Constant Representation
Coding Constants
Topics discussed in this section:Topics discussed in this section:
A Structured Programming Approach Using C33
A character constant is enclosed in single quotes.
NoteNote
Some more Arithmetic Operators
C Course, Programming club, Fall 200843
Prefix Increment : ++a
example:
o int a=5;
o b=++a; // value of b=6; a=6;
Postfix Increment: a++
example
o int a=5;
o b=a++; //value of b=5; a=6;
Contd…
C Course, Programming club, Fall 200844
Modulus (remainder): %
example:
o 12%5 = 2;
Assignment by addition: +=
example:
o int a=4;
o a+=1; //(means a=a+1) value of a becomes 5
Can use -, /, *, % also
Contd…
C Course, Programming club, Fall 200845
Comparision Operators: <, > , <=, >= , !=, ==, !,
&&, || .
example:
o int a=4, b=5;
o a<b returns a true(non zero number) value.
Bitwise Operators: <<, >>, ~, &, | ,^ .
example
o int a=8;
o a= a>>1; // value of a becomes 4
Operator Precedence
C Course, Programming club, Fall 200846
Meaning of a + b * c ?
is it a+(b*c) or (a+b)*c ?
All operators have precedence over each other
*, / have more precedence over +, - .
If both *, / are used, associativity comes into picture. (more on
this later)
example :
o 5+4*3 = 5+12= 17.
Precedence Table
C Course, Programming club, Fall 200847
Highest on top
++ -- (Postfix)
++ -- (Prefix)
* / %
+ -
<< >>
< >
&
|
&&
||
Input / Output
C Course, Programming club, Fall 200848
printf (); //used to print to console(screen)
scanf (); //used to take an input from console(user).
example: printf(“%c”, ’a’); scanf(“%d”, &a);
More format specifiers
%c The character format specifier.
%d The integer format specifier.
%i The integer format specifier (same as %d).
%f The floating-point format specifier.
%o The unsigned octal format specifier.
%s The string format specifier.
%u The unsigned integer format specifier.
%x The unsigned hexadecimal format specifier.
%% Outputs a percent sign.
Some more geek stuff
C Course, Programming club, Fall 200849
& in scanf.
It is used to access the address of the variable used.
example:
o scanf(%d,&a);
o we are reading into the address of a.
Data Hierarchy.
example:
int value can be assigned to float not vice-versa.
Type casting.
Home Work
C Course, Programming club, Fall 200850
Meaning of
Syntax
Semantics of a programming language
Find the Output:
value=value++ + value++;
Value=++value + ++value;
value=value++ + ++value;
End of Today’s Lecture
C Course, Programming club, Fall 200851
Doubts && Queries?