SlideShare a Scribd company logo
1 of 27
Download to read offline
Computing Fundamentals
Dr. Muhammad Yousaf Hamza
C is highly case sensitive
#include <stdio.h>
int main( )
{
int x, y, z, Z;
x = 5;
y = 7;
z = x + y;
Z = x - y;
printf(“%dn", z); // 12
printf(“%d", Z); // -2
getchar();
return 0;
}
//Note: Z is different from z.
Dr. Muhammad Yousaf Hamza
Escape Sequences
Sequence Meaning
a Bell (alert)
b Backspace
n Newline
t Horizontal tab
 Backslash
' Single quote
" Double quotation
Dr. Muhammad Yousaf Hamza
• The name reflects the fact that the backslash
causes an “escape” from the normal way
characters are interpreted.
• In this case, the n is interpreted not as the
character ‘n’ but as the new line character.
Escape Sequences
Dr. Muhammad Yousaf Hamza
#include <stdio.h>
int main( )
{
int x, y, z, m;
x = 5;
y = 7;
z = x + y;
m = x - y;
printf("Sum is %dnDifference is %d", z, m);
getchar();
return 0;
}
Sum is 12
Difference is -2
Dr. Muhammad Yousaf Hamza
Exercise
Try out various escape
sequences in this program.
#include <stdio.h>
int main( )
{
int x = 5, y = 7, z; // initialization with declaration
z = x + y;
printf(“%d", z);
getchar();
return 0;
}
Dr. Muhammad Yousaf Hamza
“Hello World”
//Most of the people write this program first
#include <stdio.h>
int main ( )
{
printf ("Hello World");
getchar();
return 0;
}
Dr. Muhammad Yousaf Hamza
More About printf()
Dr. Muhammad Yousaf Hamza
More about printf()
• The printf function is used to output information
(both data from variables and text) to standard
output.
• It takes a format string and parameters for output.
a = 10;
b = 20;
• printf("The number is %d", a);
The number is 10
• printf("The numbers are %d and %d",a,b);
The numbers are 10 and 20
• printf("The numbers are %d and %d",a,b*3);
The numbers are 10 and 60
• printf("The numbers are %d and %d",a,b);
The numbers are 10 and 20
Dr. Muhammad Yousaf Hamza
More about printf()
The format string contains:
– Literal text: is printed as is without variation
– Escaped sequences: special characters preceeded
by 
– Conversion specifiers: % followed by a single
character
• Indicates (usually) that a variable is to be
printed at this location in the output stream.
• The variables to be printed must appear in the
parameters to printf following the format
string, in the order that they appear in the
format string.
Dr. Muhammad Yousaf Hamza
C doesn’t care about spaces
#include <stdio.h>
int main ( )
{
printf ("Hello World”);
return 0;
}
#include <stdio.h>
int
main
(
)
{
printf
(
"Hello
World"
)
;
return
0
;
}
Both of these
programs are
the same as far as
your compiler is
concerned.
We SHOULD lay
out our C program
to make them look
nice.
Dr. Muhammad Yousaf Hamza
C doesn’t care about spaces
In the most general sense, a statement is a part of
your program that can be executed.
a = 10;
An expression is a statement.
a = a+1;
A function call is also a statement.
printf("%d”, a);
Other statements ……
C is a free form language, so you may type the
statements in any style you feel comfortable:
a=
a+
1;
a = a + 1; a = 6;
Dr. Muhammad Yousaf Hamza
#include <stdio.h>
int main( )
{
int x;
x = 5;
printf(“%d", x); // 5
x = x + 3;
printf(“%d", x); // 8
printf(“%d", x*6); // 48
printf(“%d", x); // 6
getchar();
return 0;
}
Dr. Muhammad Yousaf Hamza
Punctuation
Punctuations as semicolons, colons, commas,
apostrophes, quotation marks, braces,
brackets, and parentheses will also be used in
C code.
; : , ‘ “ [ ] { } ( )
Dr. Muhammad Yousaf Hamza
C Statements
Some Suggestions
• DO: Use block braces on their own line. It is easy
to
find the beginning and end of a block.
– This makes the code easier to read.
{
printf ("Hello, ");
printf ("world");
}
• AVOID: spreading a single statement across
multiple lines if there is no need.
– Try to keep it on one line.
Dr. Muhammad Yousaf Hamza
Names of C Variables
Dr. Muhammad Yousaf Hamza
Names of C Variables
• A good name for your variables is important
• Variables in C can be given any name made from numbers,
letters and underscores which is not a keyword and does
not begin with a number.
• Names may contain letters, digits and underscores
• The first character must be a letter or an underscore.
• First 31 characters are significant
(too long name is as bad as too short).
• Are case sensitive:
– abc is different from ABC
• Must begin with a letter or underscore and the rest can be
letters, digits, and underscores.
Dr. Muhammad Yousaf Hamza
present, hello, y2x3, r2d3, ... /* OK */
_1993_tar_return /* OK but not good */
Hello#there /* illegal */
int /* shouldn’t work */
2fartogo /* illegal */
Names of C Variables
Dr. Muhammad Yousaf Hamza
int a;
int d;
/* It is like
cryptic */
int start_time;
int no_students;
/* It is better */
Names of C Variables
Dr. Muhammad Yousaf Hamza
Names of C Variables
Suggestions regarding variable names
• DO: use variable names that are descriptive
• DO: adopt and stick to a standard naming convention
– sometimes it is useful to do this consistently for the entire
software development site
• AVOID: variable names starting with an underscore
– often used by the operating system and easy to miss
• AVOID: using uppercase only variable names
– generally these are pre-processor macros (later)
Dr. Muhammad Yousaf Hamza
• C keywords cannot be used as variable names.
• Sometimes called reserved words.
• Are defined as a part of the C language.
• Can not be used for anything else!
• Examples:
– int
– while
– for
Names of C Variables
Dr. Muhammad Yousaf Hamza
Keywords of C
• Flow control (6) – if, else, return, switch,
case, default
• Loops (5) – for, do, while, break, continue
• Common types (5) – int, float, double, char,
void
• Structures (3) – struct, typedef, union
Dr. Muhammad Yousaf Hamza
Comments
Dr. Muhammad Yousaf Hamza
Comments
• Can be used to write title of the program, author
details etc.
• To guide a programmer. To write a note for function,
operation, logic etc. in between a program.
• Non-executable statement.
Dr. Muhammad Yousaf Hamza
Comments: /* This is a comment */
Use them!
Comments should explain:
• special cases
• the use of functions (parameters, return values, purpose)
• explain WHY your code does things the what it does.
• Can’t be nested.
e.g:- /* Hello /* abc */ Hi */ ERROR.
Comments
Comments
• Ideally, a comment with each variable name
helps people know what they do.
• In Lab work, I like to see well chosen variable
names and comments on variables.
Dr. Muhammad Yousaf Hamza
More on Comments
For a single line comments, you may use //
For a single line or multiple lines comments, /* comments */ is
used.
A few examples of comments
/* This program calculates area of a rectangle
This program is developed by Mr. XYZ */
length = 5; // in km
width = 3; // in km
Dr. Muhammad Yousaf Hamza

More Related Content

Similar to C Language Lecture 2

Review of C.pptx
Review of C.pptxReview of C.pptx
Review of C.pptx
Satyanandaram Nandigam
 

Similar to C Language Lecture 2 (20)

C Language Lecture 3
C Language Lecture  3C Language Lecture  3
C Language Lecture 3
 
C Language Lecture 12
C Language Lecture 12C Language Lecture 12
C Language Lecture 12
 
C Language Lecture 1
C Language Lecture  1C Language Lecture  1
C Language Lecture 1
 
Cpu
CpuCpu
Cpu
 
A brief introduction to C Language
A brief introduction to C LanguageA brief introduction to C Language
A brief introduction to C Language
 
C programming day#3.
C programming day#3.C programming day#3.
C programming day#3.
 
Lập trình C
Lập trình CLập trình C
Lập trình C
 
C language
C languageC language
C language
 
Python
PythonPython
Python
 
COMPUTER PROGRAMMING
COMPUTER PROGRAMMINGCOMPUTER PROGRAMMING
COMPUTER PROGRAMMING
 
Review of C.pptx
Review of C.pptxReview of C.pptx
Review of C.pptx
 
270_1_CIntro_Up_To_Functions.ppt
270_1_CIntro_Up_To_Functions.ppt270_1_CIntro_Up_To_Functions.ppt
270_1_CIntro_Up_To_Functions.ppt
 
A Quick Taste of C
A Quick Taste of CA Quick Taste of C
A Quick Taste of C
 
270_1_CIntro_Up_To_Functions.ppt
270_1_CIntro_Up_To_Functions.ppt270_1_CIntro_Up_To_Functions.ppt
270_1_CIntro_Up_To_Functions.ppt
 
Survey of programming language getting started in C
Survey of programming language getting started in CSurvey of programming language getting started in C
Survey of programming language getting started in C
 
270 1 c_intro_up_to_functions
270 1 c_intro_up_to_functions270 1 c_intro_up_to_functions
270 1 c_intro_up_to_functions
 
270_1_CIntro_Up_To_Functions.ppt
270_1_CIntro_Up_To_Functions.ppt270_1_CIntro_Up_To_Functions.ppt
270_1_CIntro_Up_To_Functions.ppt
 
C prog ppt
C prog pptC prog ppt
C prog ppt
 
C programming language
C programming languageC programming language
C programming language
 
Token and operators
Token and operatorsToken and operators
Token and operators
 

More from Shahzaib Ajmal

More from Shahzaib Ajmal (18)

C Language Lecture 22
C Language Lecture 22C Language Lecture 22
C Language Lecture 22
 
C Language Lecture 21
C Language Lecture 21C Language Lecture 21
C Language Lecture 21
 
C Language Lecture 20
C Language Lecture 20C Language Lecture 20
C Language Lecture 20
 
C Language Lecture 19
C Language Lecture 19C Language Lecture 19
C Language Lecture 19
 
C Language Lecture 18
C Language Lecture 18C Language Lecture 18
C Language Lecture 18
 
C Language Lecture 17
C Language Lecture 17C Language Lecture 17
C Language Lecture 17
 
C Language Lecture 16
C Language Lecture 16C Language Lecture 16
C Language Lecture 16
 
C Language Lecture 15
C Language Lecture 15C Language Lecture 15
C Language Lecture 15
 
C Language Lecture 14
C Language Lecture 14C Language Lecture 14
C Language Lecture 14
 
C Language Lecture 13
C Language Lecture 13C Language Lecture 13
C Language Lecture 13
 
C Language Lecture 11
C Language Lecture  11C Language Lecture  11
C Language Lecture 11
 
C Language Lecture 10
C Language Lecture 10C Language Lecture 10
C Language Lecture 10
 
C Language Lecture 9
C Language Lecture 9C Language Lecture 9
C Language Lecture 9
 
C Language Lecture 8
C Language Lecture 8C Language Lecture 8
C Language Lecture 8
 
C Language Lecture 7
C Language Lecture 7C Language Lecture 7
C Language Lecture 7
 
C Language Lecture 6
C Language Lecture 6C Language Lecture 6
C Language Lecture 6
 
C Language Lecture 5
C Language Lecture  5C Language Lecture  5
C Language Lecture 5
 
C Language Lecture 4
C Language Lecture  4C Language Lecture  4
C Language Lecture 4
 

Recently uploaded

Recently uploaded (20)

80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...
80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...
80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...
 
FSB Advising Checklist - Orientation 2024
FSB Advising Checklist - Orientation 2024FSB Advising Checklist - Orientation 2024
FSB Advising Checklist - Orientation 2024
 
AIM of Education-Teachers Training-2024.ppt
AIM of Education-Teachers Training-2024.pptAIM of Education-Teachers Training-2024.ppt
AIM of Education-Teachers Training-2024.ppt
 
Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...
Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...
Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...
 
How to Add New Custom Addons Path in Odoo 17
How to Add New Custom Addons Path in Odoo 17How to Add New Custom Addons Path in Odoo 17
How to Add New Custom Addons Path in Odoo 17
 
Mehran University Newsletter Vol-X, Issue-I, 2024
Mehran University Newsletter Vol-X, Issue-I, 2024Mehran University Newsletter Vol-X, Issue-I, 2024
Mehran University Newsletter Vol-X, Issue-I, 2024
 
Basic Intentional Injuries Health Education
Basic Intentional Injuries Health EducationBasic Intentional Injuries Health Education
Basic Intentional Injuries Health Education
 
SOC 101 Demonstration of Learning Presentation
SOC 101 Demonstration of Learning PresentationSOC 101 Demonstration of Learning Presentation
SOC 101 Demonstration of Learning Presentation
 
OSCM Unit 2_Operations Processes & Systems
OSCM Unit 2_Operations Processes & SystemsOSCM Unit 2_Operations Processes & Systems
OSCM Unit 2_Operations Processes & Systems
 
Simple, Complex, and Compound Sentences Exercises.pdf
Simple, Complex, and Compound Sentences Exercises.pdfSimple, Complex, and Compound Sentences Exercises.pdf
Simple, Complex, and Compound Sentences Exercises.pdf
 
How to Manage Call for Tendor in Odoo 17
How to Manage Call for Tendor in Odoo 17How to Manage Call for Tendor in Odoo 17
How to Manage Call for Tendor in Odoo 17
 
Tatlong Kwento ni Lola basyang-1.pdf arts
Tatlong Kwento ni Lola basyang-1.pdf artsTatlong Kwento ni Lola basyang-1.pdf arts
Tatlong Kwento ni Lola basyang-1.pdf arts
 
How to Add a Tool Tip to a Field in Odoo 17
How to Add a Tool Tip to a Field in Odoo 17How to Add a Tool Tip to a Field in Odoo 17
How to Add a Tool Tip to a Field in Odoo 17
 
Towards a code of practice for AI in AT.pptx
Towards a code of practice for AI in AT.pptxTowards a code of practice for AI in AT.pptx
Towards a code of practice for AI in AT.pptx
 
REMIFENTANIL: An Ultra short acting opioid.pptx
REMIFENTANIL: An Ultra short acting opioid.pptxREMIFENTANIL: An Ultra short acting opioid.pptx
REMIFENTANIL: An Ultra short acting opioid.pptx
 
Exploring_the_Narrative_Style_of_Amitav_Ghoshs_Gun_Island.pptx
Exploring_the_Narrative_Style_of_Amitav_Ghoshs_Gun_Island.pptxExploring_the_Narrative_Style_of_Amitav_Ghoshs_Gun_Island.pptx
Exploring_the_Narrative_Style_of_Amitav_Ghoshs_Gun_Island.pptx
 
NO1 Top Black Magic Specialist In Lahore Black magic In Pakistan Kala Ilam Ex...
NO1 Top Black Magic Specialist In Lahore Black magic In Pakistan Kala Ilam Ex...NO1 Top Black Magic Specialist In Lahore Black magic In Pakistan Kala Ilam Ex...
NO1 Top Black Magic Specialist In Lahore Black magic In Pakistan Kala Ilam Ex...
 
Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptxBasic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
 
How to Manage Global Discount in Odoo 17 POS
How to Manage Global Discount in Odoo 17 POSHow to Manage Global Discount in Odoo 17 POS
How to Manage Global Discount in Odoo 17 POS
 
On_Translating_a_Tamil_Poem_by_A_K_Ramanujan.pptx
On_Translating_a_Tamil_Poem_by_A_K_Ramanujan.pptxOn_Translating_a_Tamil_Poem_by_A_K_Ramanujan.pptx
On_Translating_a_Tamil_Poem_by_A_K_Ramanujan.pptx
 

C Language Lecture 2

  • 2. C is highly case sensitive #include <stdio.h> int main( ) { int x, y, z, Z; x = 5; y = 7; z = x + y; Z = x - y; printf(“%dn", z); // 12 printf(“%d", Z); // -2 getchar(); return 0; } //Note: Z is different from z. Dr. Muhammad Yousaf Hamza
  • 3. Escape Sequences Sequence Meaning a Bell (alert) b Backspace n Newline t Horizontal tab Backslash ' Single quote " Double quotation Dr. Muhammad Yousaf Hamza
  • 4. • The name reflects the fact that the backslash causes an “escape” from the normal way characters are interpreted. • In this case, the n is interpreted not as the character ‘n’ but as the new line character. Escape Sequences Dr. Muhammad Yousaf Hamza
  • 5. #include <stdio.h> int main( ) { int x, y, z, m; x = 5; y = 7; z = x + y; m = x - y; printf("Sum is %dnDifference is %d", z, m); getchar(); return 0; } Sum is 12 Difference is -2 Dr. Muhammad Yousaf Hamza Exercise Try out various escape sequences in this program.
  • 6. #include <stdio.h> int main( ) { int x = 5, y = 7, z; // initialization with declaration z = x + y; printf(“%d", z); getchar(); return 0; } Dr. Muhammad Yousaf Hamza
  • 7. “Hello World” //Most of the people write this program first #include <stdio.h> int main ( ) { printf ("Hello World"); getchar(); return 0; } Dr. Muhammad Yousaf Hamza
  • 8. More About printf() Dr. Muhammad Yousaf Hamza
  • 9. More about printf() • The printf function is used to output information (both data from variables and text) to standard output. • It takes a format string and parameters for output. a = 10; b = 20; • printf("The number is %d", a); The number is 10 • printf("The numbers are %d and %d",a,b); The numbers are 10 and 20 • printf("The numbers are %d and %d",a,b*3); The numbers are 10 and 60 • printf("The numbers are %d and %d",a,b); The numbers are 10 and 20 Dr. Muhammad Yousaf Hamza
  • 10. More about printf() The format string contains: – Literal text: is printed as is without variation – Escaped sequences: special characters preceeded by – Conversion specifiers: % followed by a single character • Indicates (usually) that a variable is to be printed at this location in the output stream. • The variables to be printed must appear in the parameters to printf following the format string, in the order that they appear in the format string. Dr. Muhammad Yousaf Hamza
  • 11. C doesn’t care about spaces #include <stdio.h> int main ( ) { printf ("Hello World”); return 0; } #include <stdio.h> int main ( ) { printf ( "Hello World" ) ; return 0 ; } Both of these programs are the same as far as your compiler is concerned. We SHOULD lay out our C program to make them look nice. Dr. Muhammad Yousaf Hamza
  • 12. C doesn’t care about spaces In the most general sense, a statement is a part of your program that can be executed. a = 10; An expression is a statement. a = a+1; A function call is also a statement. printf("%d”, a); Other statements …… C is a free form language, so you may type the statements in any style you feel comfortable: a= a+ 1; a = a + 1; a = 6; Dr. Muhammad Yousaf Hamza
  • 13. #include <stdio.h> int main( ) { int x; x = 5; printf(“%d", x); // 5 x = x + 3; printf(“%d", x); // 8 printf(“%d", x*6); // 48 printf(“%d", x); // 6 getchar(); return 0; } Dr. Muhammad Yousaf Hamza
  • 14. Punctuation Punctuations as semicolons, colons, commas, apostrophes, quotation marks, braces, brackets, and parentheses will also be used in C code. ; : , ‘ “ [ ] { } ( ) Dr. Muhammad Yousaf Hamza
  • 15. C Statements Some Suggestions • DO: Use block braces on their own line. It is easy to find the beginning and end of a block. – This makes the code easier to read. { printf ("Hello, "); printf ("world"); } • AVOID: spreading a single statement across multiple lines if there is no need. – Try to keep it on one line. Dr. Muhammad Yousaf Hamza
  • 16. Names of C Variables Dr. Muhammad Yousaf Hamza
  • 17. Names of C Variables • A good name for your variables is important • Variables in C can be given any name made from numbers, letters and underscores which is not a keyword and does not begin with a number. • Names may contain letters, digits and underscores • The first character must be a letter or an underscore. • First 31 characters are significant (too long name is as bad as too short). • Are case sensitive: – abc is different from ABC • Must begin with a letter or underscore and the rest can be letters, digits, and underscores. Dr. Muhammad Yousaf Hamza
  • 18. present, hello, y2x3, r2d3, ... /* OK */ _1993_tar_return /* OK but not good */ Hello#there /* illegal */ int /* shouldn’t work */ 2fartogo /* illegal */ Names of C Variables Dr. Muhammad Yousaf Hamza
  • 19. int a; int d; /* It is like cryptic */ int start_time; int no_students; /* It is better */ Names of C Variables Dr. Muhammad Yousaf Hamza
  • 20. Names of C Variables Suggestions regarding variable names • DO: use variable names that are descriptive • DO: adopt and stick to a standard naming convention – sometimes it is useful to do this consistently for the entire software development site • AVOID: variable names starting with an underscore – often used by the operating system and easy to miss • AVOID: using uppercase only variable names – generally these are pre-processor macros (later) Dr. Muhammad Yousaf Hamza
  • 21. • C keywords cannot be used as variable names. • Sometimes called reserved words. • Are defined as a part of the C language. • Can not be used for anything else! • Examples: – int – while – for Names of C Variables Dr. Muhammad Yousaf Hamza
  • 22. Keywords of C • Flow control (6) – if, else, return, switch, case, default • Loops (5) – for, do, while, break, continue • Common types (5) – int, float, double, char, void • Structures (3) – struct, typedef, union Dr. Muhammad Yousaf Hamza
  • 24. Comments • Can be used to write title of the program, author details etc. • To guide a programmer. To write a note for function, operation, logic etc. in between a program. • Non-executable statement. Dr. Muhammad Yousaf Hamza
  • 25. Comments: /* This is a comment */ Use them! Comments should explain: • special cases • the use of functions (parameters, return values, purpose) • explain WHY your code does things the what it does. • Can’t be nested. e.g:- /* Hello /* abc */ Hi */ ERROR. Comments
  • 26. Comments • Ideally, a comment with each variable name helps people know what they do. • In Lab work, I like to see well chosen variable names and comments on variables. Dr. Muhammad Yousaf Hamza
  • 27. More on Comments For a single line comments, you may use // For a single line or multiple lines comments, /* comments */ is used. A few examples of comments /* This program calculates area of a rectangle This program is developed by Mr. XYZ */ length = 5; // in km width = 3; // in km Dr. Muhammad Yousaf Hamza