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 C4
FIGURE 2-2 Structure of a C Program
A Structured Programming Approach Using C5
FIGURE 2-3 The Greeting Program
A Structured Programming Approach Using C6
PROGRAM 2-1 The Greeting Program
A Structured Programming Approach Using C7
FIGURE 2-4 Examples of Block Comments
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 C10
FIGURE 2-6 Nested Block Comments Are Invalid
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 C12
Table 2-1 Rules for Identifiers
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 C14
Table 2-2 Examples of Valid and Invalid Names
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 C16
FIGURE 2-7 Data Types
A Structured Programming Approach Using C17
FIGURE 2-8 Character Types
A Structured Programming Approach Using C18
FIGURE 2-9 Integer Types
A Structured Programming Approach Using C19
sizeof (short) ≤ sizeof (int) ≤ sizeof (long) ≤ sizeof (long long)
NoteNote
A Structured Programming Approach Using C20
Table 2-3 Typical Integer Sizes and Values for Signed Integers
A Structured Programming Approach Using C21
FIGURE 2-10 Floating-point Types
A Structured Programming Approach Using C22
sizeof (float) ≤ sizeof (double) ≤ sizeof (long double)
NoteNote
A Structured Programming Approach Using C23
Table 2-4 Type Summary
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 C25
FIGURE 2-11 Variables
A Structured Programming Approach Using C26
Table 2-5 Examples of Variable Declarations and Definitions
A Structured Programming Approach Using C27
FIGURE 2-12 Variable Initialization
‘B’
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 C29
PROGRAM 2-2 Print Sum of Three Numbers
A Structured Programming Approach Using C30
PROGRAM 2-2 Print Sum of Three Numbers (continued)
A Structured Programming Approach Using C31
PROGRAM 2-2 Print Sum of Three Numbers (continued)
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
A Structured Programming Approach Using C34
Table 2-6 Symbolic Names for Control Characters
A Structured Programming Approach Using C35
Table 2-7 Examples of Integer Constants
A Structured Programming Approach Using C36
Table 2-8 Examples of Real Constants
A Structured Programming Approach Using C37
FIGURE 2-13 Some Strings
A Structured Programming Approach Using C38
FIGURE 2-14 Null Characters and Null Strings
A Structured Programming Approach Using C39
Use single quotes for character constants.
Use double quotes for string constants.
NoteNote
A Structured Programming Approach Using C40
PROGRAM 2-3 Memory Constants
A Structured Programming Approach Using C41
PROGRAM 2-3 Memory Constants (continued)
A Structured Programming Approach Using C42
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?
THANK YOU
C Course, Programming club, Fall 200852

Intro

  • 1.
    A Structured ProgrammingApproach 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
  • 2.
    A Structured ProgrammingApproach 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.
  • 3.
    A Structured ProgrammingApproach 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:
  • 4.
    A Structured ProgrammingApproach Using C4 FIGURE 2-2 Structure of a C Program
  • 5.
    A Structured ProgrammingApproach Using C5 FIGURE 2-3 The Greeting Program
  • 6.
    A Structured ProgrammingApproach Using C6 PROGRAM 2-1 The Greeting Program
  • 7.
    A Structured ProgrammingApproach Using C7 FIGURE 2-4 Examples of Block Comments
  • 8.
    A Structured ProgrammingApproach 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
  • 9.
    A Structured ProgrammingApproach 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.
  • 10.
    A Structured ProgrammingApproach Using C10 FIGURE 2-6 Nested Block Comments Are Invalid
  • 11.
    A Structured ProgrammingApproach 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.
  • 12.
    A Structured ProgrammingApproach Using C12 Table 2-1 Rules for Identifiers
  • 13.
    A Structured ProgrammingApproach 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.
  • 14.
    A Structured ProgrammingApproach Using C14 Table 2-2 Examples of Valid and Invalid Names
  • 15.
    A Structured ProgrammingApproach 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:
  • 16.
    A Structured ProgrammingApproach Using C16 FIGURE 2-7 Data Types
  • 17.
    A Structured ProgrammingApproach Using C17 FIGURE 2-8 Character Types
  • 18.
    A Structured ProgrammingApproach Using C18 FIGURE 2-9 Integer Types
  • 19.
    A Structured ProgrammingApproach Using C19 sizeof (short) ≤ sizeof (int) ≤ sizeof (long) ≤ sizeof (long long) NoteNote
  • 20.
    A Structured ProgrammingApproach Using C20 Table 2-3 Typical Integer Sizes and Values for Signed Integers
  • 21.
    A Structured ProgrammingApproach Using C21 FIGURE 2-10 Floating-point Types
  • 22.
    A Structured ProgrammingApproach Using C22 sizeof (float) ≤ sizeof (double) ≤ sizeof (long double) NoteNote
  • 23.
    A Structured ProgrammingApproach Using C23 Table 2-4 Type Summary
  • 24.
    A Structured ProgrammingApproach 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:
  • 25.
    A Structured ProgrammingApproach Using C25 FIGURE 2-11 Variables
  • 26.
    A Structured ProgrammingApproach Using C26 Table 2-5 Examples of Variable Declarations and Definitions
  • 27.
    A Structured ProgrammingApproach Using C27 FIGURE 2-12 Variable Initialization ‘B’
  • 28.
    A Structured ProgrammingApproach Using C28 When a variable is defined, it is not initialized. We must initialize any variable requiring prescribed data when the function starts. NoteNote
  • 29.
    A Structured ProgrammingApproach Using C29 PROGRAM 2-2 Print Sum of Three Numbers
  • 30.
    A Structured ProgrammingApproach Using C30 PROGRAM 2-2 Print Sum of Three Numbers (continued)
  • 31.
    A Structured ProgrammingApproach Using C31 PROGRAM 2-2 Print Sum of Three Numbers (continued)
  • 32.
    A Structured ProgrammingApproach 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:
  • 33.
    A Structured ProgrammingApproach Using C33 A character constant is enclosed in single quotes. NoteNote
  • 34.
    A Structured ProgrammingApproach Using C34 Table 2-6 Symbolic Names for Control Characters
  • 35.
    A Structured ProgrammingApproach Using C35 Table 2-7 Examples of Integer Constants
  • 36.
    A Structured ProgrammingApproach Using C36 Table 2-8 Examples of Real Constants
  • 37.
    A Structured ProgrammingApproach Using C37 FIGURE 2-13 Some Strings
  • 38.
    A Structured ProgrammingApproach Using C38 FIGURE 2-14 Null Characters and Null Strings
  • 39.
    A Structured ProgrammingApproach Using C39 Use single quotes for character constants. Use double quotes for string constants. NoteNote
  • 40.
    A Structured ProgrammingApproach Using C40 PROGRAM 2-3 Memory Constants
  • 41.
    A Structured ProgrammingApproach Using C41 PROGRAM 2-3 Memory Constants (continued)
  • 42.
    A Structured ProgrammingApproach Using C42
  • 43.
    Some more ArithmeticOperators 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;
  • 44.
    Contd… C Course, Programmingclub, 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
  • 45.
    Contd… C Course, Programmingclub, 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
  • 46.
    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.
  • 47.
    Precedence Table C Course,Programming club, Fall 200847 Highest on top ++ -- (Postfix) ++ -- (Prefix) * / % + - << >> < > & | && ||
  • 48.
    Input / Output CCourse, 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.
  • 49.
    Some more geekstuff 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.
  • 50.
    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;
  • 51.
    End of Today’sLecture C Course, Programming club, Fall 200851 Doubts && Queries?
  • 52.
    THANK YOU C Course,Programming club, Fall 200852

Editor's Notes

  • #3 Developed early 1970’s
  • #18 wchar_t is a wide character:  The increased datatype size allows for the use of larger coded character sets. Width is compiler specific (not portable).