SlideShare a Scribd company logo
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

More Related Content

PPT
C and C++ Industrial Training Jalandhar
DOCX
C notes
DOCX
PPTX
C_Programming_Notes_ICE
PPTX
C language
PPT
Introduction to c programming
PDF
C programming
C and C++ Industrial Training Jalandhar
C notes
C_Programming_Notes_ICE
C language
Introduction to c programming
C programming

What's hot (20)

PPTX
C language
PPTX
Introduction to c
DOC
C notes by m v b reddy(gitam)imp notes all units notes 5 unit order
DOC
Notes of c programming 1st unit BCA I SEM
DOC
Programming in c notes
PDF
best notes in c language
PPTX
DOCX
C Programming
PDF
C programming
DOCX
C language industrial training report
PPT
Unit 4 Foc
PPSX
C basics 4 std11(GujBoard)
PPTX
Introduction to C programming
PPT
C Language
PPTX
PPTX
COM1407: Introduction to C Programming
PDF
Learning the C Language
DOCX
C language
PDF
C programming notes BATRACOMPUTER CENTRE IN Ambala CANTT
C language
Introduction to c
C notes by m v b reddy(gitam)imp notes all units notes 5 unit order
Notes of c programming 1st unit BCA I SEM
Programming in c notes
best notes in c language
C Programming
C programming
C language industrial training report
Unit 4 Foc
C basics 4 std11(GujBoard)
Introduction to C programming
C Language
COM1407: Introduction to C Programming
Learning the C Language
C language
C programming notes BATRACOMPUTER CENTRE IN Ambala CANTT
Ad

Similar to Intro (20)

PPT
Chap-02-1.ppt
PPT
Chap-02-1.ppt
PPT
Chap-02-1.ppt
PPT
Chap-02-1.ppt
PPT
Chap-02-1.ppt
PPTX
C programming-1.pptx
PPT
Chap-02-01.ppt
PPT
Chap 02-1
PPT
the programming Structure of c concept.ppt
PPT
Chap-02-1.ppt
PPT
1.Introduction_to_c_programming lan.ppt
PPT
C Programming Intro.ppt
PPT
CODING-DAY-2-INTRODUCTION TO C PROGRAMMING.ppt
PPT
PPTX
C Programming language basic for students
PPT
Introduction_to_c_programming.ppt for software developing students
PPT
Semester-1-22-23-Introduction_to_c_programming.ppt
PPTX
Team-7 SP.pptxdfghjksdfgduytredfghjkjhgffghj
PPTX
C programming language
PPT
M.Florence Dayana / Basics of C Language
Chap-02-1.ppt
Chap-02-1.ppt
Chap-02-1.ppt
Chap-02-1.ppt
Chap-02-1.ppt
C programming-1.pptx
Chap-02-01.ppt
Chap 02-1
the programming Structure of c concept.ppt
Chap-02-1.ppt
1.Introduction_to_c_programming lan.ppt
C Programming Intro.ppt
CODING-DAY-2-INTRODUCTION TO C PROGRAMMING.ppt
C Programming language basic for students
Introduction_to_c_programming.ppt for software developing students
Semester-1-22-23-Introduction_to_c_programming.ppt
Team-7 SP.pptxdfghjksdfgduytredfghjkjhgffghj
C programming language
M.Florence Dayana / Basics of C Language
Ad

More from CGC Technical campus,Mohali (20)

PPTX
Gender Issues CS.pptx
PPTX
Intellectual Property Rights.pptx
PPTX
Cyber Safety ppt.pptx
PPTX
Python Modules .pptx
PPT
Dynamic allocation
PPT
Control statments in c
PPTX
Operators in c by anupam
PPTX
PPT
Fundamentals of-computer
PPT
PPTX
Structure in C language
PPTX
Function in c program
PPTX
PPTX
Data processing and Report writing in Research(Section E)
PPTX
data analysis and report wring in research (Section d)
PPTX
Section C(Analytical and descriptive surveys... )
Gender Issues CS.pptx
Intellectual Property Rights.pptx
Cyber Safety ppt.pptx
Python Modules .pptx
Dynamic allocation
Control statments in c
Operators in c by anupam
Fundamentals of-computer
Structure in C language
Function in c program
Data processing and Report writing in Research(Section E)
data analysis and report wring in research (Section d)
Section C(Analytical and descriptive surveys... )

Recently uploaded (20)

PPTX
KTU 2019 -S7-MCN 401 MODULE 2-VINAY.pptx
PPTX
FINAL REVIEW FOR COPD DIANOSIS FOR PULMONARY DISEASE.pptx
PDF
BMEC211 - INTRODUCTION TO MECHATRONICS-1.pdf
PDF
Operating System & Kernel Study Guide-1 - converted.pdf
PDF
composite construction of structures.pdf
PPTX
M Tech Sem 1 Civil Engineering Environmental Sciences.pptx
PPTX
Lecture Notes Electrical Wiring System Components
PDF
Mohammad Mahdi Farshadian CV - Prospective PhD Student 2026
PDF
Mitigating Risks through Effective Management for Enhancing Organizational Pe...
PDF
Embodied AI: Ushering in the Next Era of Intelligent Systems
PPTX
CYBER-CRIMES AND SECURITY A guide to understanding
PDF
July 2025 - Top 10 Read Articles in International Journal of Software Enginee...
PPTX
Lesson 3_Tessellation.pptx finite Mathematics
PPTX
IOT PPTs Week 10 Lecture Material.pptx of NPTEL Smart Cities contd
PDF
Digital Logic Computer Design lecture notes
PPTX
Engineering Ethics, Safety and Environment [Autosaved] (1).pptx
PDF
Model Code of Practice - Construction Work - 21102022 .pdf
PDF
The CXO Playbook 2025 – Future-Ready Strategies for C-Suite Leaders Cerebrai...
PPTX
CH1 Production IntroductoryConcepts.pptx
PPTX
Infosys Presentation by1.Riyan Bagwan 2.Samadhan Naiknavare 3.Gaurav Shinde 4...
KTU 2019 -S7-MCN 401 MODULE 2-VINAY.pptx
FINAL REVIEW FOR COPD DIANOSIS FOR PULMONARY DISEASE.pptx
BMEC211 - INTRODUCTION TO MECHATRONICS-1.pdf
Operating System & Kernel Study Guide-1 - converted.pdf
composite construction of structures.pdf
M Tech Sem 1 Civil Engineering Environmental Sciences.pptx
Lecture Notes Electrical Wiring System Components
Mohammad Mahdi Farshadian CV - Prospective PhD Student 2026
Mitigating Risks through Effective Management for Enhancing Organizational Pe...
Embodied AI: Ushering in the Next Era of Intelligent Systems
CYBER-CRIMES AND SECURITY A guide to understanding
July 2025 - Top 10 Read Articles in International Journal of Software Enginee...
Lesson 3_Tessellation.pptx finite Mathematics
IOT PPTs Week 10 Lecture Material.pptx of NPTEL Smart Cities contd
Digital Logic Computer Design lecture notes
Engineering Ethics, Safety and Environment [Autosaved] (1).pptx
Model Code of Practice - Construction Work - 21102022 .pdf
The CXO Playbook 2025 – Future-Ready Strategies for C-Suite Leaders Cerebrai...
CH1 Production IntroductoryConcepts.pptx
Infosys Presentation by1.Riyan Bagwan 2.Samadhan Naiknavare 3.Gaurav Shinde 4...

Intro

  • 1. 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
  • 2. 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.
  • 3. 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:
  • 4. A Structured Programming Approach Using C4 FIGURE 2-2 Structure of a C Program
  • 5. A Structured Programming Approach Using C5 FIGURE 2-3 The Greeting Program
  • 6. A Structured Programming Approach Using C6 PROGRAM 2-1 The Greeting Program
  • 7. A Structured Programming Approach Using C7 FIGURE 2-4 Examples of Block Comments
  • 8. 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
  • 9. 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.
  • 10. A Structured Programming Approach Using C10 FIGURE 2-6 Nested Block Comments Are Invalid
  • 11. 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.
  • 12. A Structured Programming Approach Using C12 Table 2-1 Rules for Identifiers
  • 13. 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.
  • 14. A Structured Programming Approach Using C14 Table 2-2 Examples of Valid and Invalid Names
  • 15. 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:
  • 16. A Structured Programming Approach Using C16 FIGURE 2-7 Data Types
  • 17. A Structured Programming Approach Using C17 FIGURE 2-8 Character Types
  • 18. A Structured Programming Approach Using C18 FIGURE 2-9 Integer Types
  • 19. A Structured Programming Approach Using C19 sizeof (short) ≤ sizeof (int) ≤ sizeof (long) ≤ sizeof (long long) NoteNote
  • 20. A Structured Programming Approach Using C20 Table 2-3 Typical Integer Sizes and Values for Signed Integers
  • 21. A Structured Programming Approach Using C21 FIGURE 2-10 Floating-point Types
  • 22. A Structured Programming Approach Using C22 sizeof (float) ≤ sizeof (double) ≤ sizeof (long double) NoteNote
  • 23. A Structured Programming Approach Using C23 Table 2-4 Type Summary
  • 24. 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:
  • 25. A Structured Programming Approach Using C25 FIGURE 2-11 Variables
  • 26. A Structured Programming Approach Using C26 Table 2-5 Examples of Variable Declarations and Definitions
  • 27. A Structured Programming Approach Using C27 FIGURE 2-12 Variable Initialization ‘B’
  • 28. 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
  • 29. A Structured Programming Approach Using C29 PROGRAM 2-2 Print Sum of Three Numbers
  • 30. A Structured Programming Approach Using C30 PROGRAM 2-2 Print Sum of Three Numbers (continued)
  • 31. A Structured Programming Approach Using C31 PROGRAM 2-2 Print Sum of Three Numbers (continued)
  • 32. 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:
  • 33. A Structured Programming Approach Using C33 A character constant is enclosed in single quotes. NoteNote
  • 34. A Structured Programming Approach Using C34 Table 2-6 Symbolic Names for Control Characters
  • 35. A Structured Programming Approach Using C35 Table 2-7 Examples of Integer Constants
  • 36. A Structured Programming Approach Using C36 Table 2-8 Examples of Real Constants
  • 37. A Structured Programming Approach Using C37 FIGURE 2-13 Some Strings
  • 38. A Structured Programming Approach Using C38 FIGURE 2-14 Null Characters and Null Strings
  • 39. A Structured Programming Approach Using C39 Use single quotes for character constants. Use double quotes for string constants. NoteNote
  • 40. A Structured Programming Approach Using C40 PROGRAM 2-3 Memory Constants
  • 41. A Structured Programming Approach Using C41 PROGRAM 2-3 Memory Constants (continued)
  • 42. A Structured Programming Approach Using C42
  • 43. 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;
  • 44. 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
  • 45. 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
  • 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 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.
  • 49. 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.
  • 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’s Lecture 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).