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

C Language Lecture 2

  • 1.
  • 2.
    C is highlycase 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 aBell (alert) b Backspace n Newline t Horizontal tab Backslash ' Single quote " Double quotation Dr. Muhammad Yousaf Hamza
  • 4.
    • The namereflects 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 ofthe 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() Theformat 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 careabout 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 careabout 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 CVariables Dr. Muhammad Yousaf Hamza
  • 17.
    Names of CVariables • 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 CVariables 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 keywordscannot 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
  • 23.
  • 24.
    Comments • Can beused 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: /* Thisis 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, acomment 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 Fora 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