This document provides an introduction to computer programming in C, including comments, variables, constants, format specifiers, and escape sequences. It discusses using comments to document code, declaring and initializing variables, naming conventions for variables and constants, and different format specifiers for printing values. It also provides examples of programs that work with variables to perform basic math operations and calculate areas of shapes.
2. INTRODUCTION TO COMPUTER PROGRAMMING
1) Comments in C, Program Style, Round one, format specifiers.
2) Variable, identifiers or Names in C and CONSTANTS IN C
3) Working with variable, identifiers or Names in C etc
3. Why use Comments
• Documentation of variables, functions and their usage.
• Explaining difficult section of code.
• Describes author, program, data, modification changes and revisions.
• Best programmers comment as they write the code not after the fact.
Comments
• The addition of comments inside programs is desirable.
• These may be added to C programs by enclosing them as follows.
Multiple line comment
/* This code will not be compiled and executed*/
• Note that /*opens the comment field and */ closes the comment field
• Comments may span multiple lines
• Comments may not be nested one inside the another
• For example /* and /* and */ */ error generated.
Single line comment
// This will not be executed.
4. Program Style, Round one, format specifiers
void main(void){printf(“This is the number two:%d”,2);}
• The white space characters(space, tab, newline) are invisible
by compiler.
• C distinguishes between UPPERCASE and LOWERCASE.
Printing Numbers
void main(void)
{
printf(“This is the number two:%d”,2);
}
5. Format Specifier
• It tells printf function to put a value in String and what format
to use in printing value.
Printing Strings
void main(void)
{
printf(“%s is %d million miles from sun”, “venus”,67);
}
o/p
venus is 67 million miles from sun
7. Variable
•Variable may be the most fundamental aspect of any computer
language.
•Variable is space in computer memory. Set aside for a certain
kind data and given a name for easy reference.
•Variables are used so that, the same space in memory can hold
different times.
•You need to store at least hourly rate and hours worked.
Identifiers or Names in C
• Identifiers in C must begin with a character or underscore and
may be followed by any combination of characters,
underscores or digits 0-9.
11. Escape Sequences
Escape Sequence Character
a Bell (speaker beeps)
b Backspace (non-erase)
f Form feed/clear screen
n New line
r Carriage Return(return it to begin of line)
t Tab
v Vertical tab
Backslash
? Question mark
' Single quote
" Double quote
xnn Hexadecimal character code nn
onn Octal character code nn
13. Working with variable
a. Write a program in C for three variables and display its addition.
b. Write a program in C for two variable and display its multiplication.
c. Write a program in C for two variables and display its division.
d. Write a program in C for two variables and display its subtraction.
e. Write a program in C for subject marks and find its percentage.
f. Write a program in C for radius and pi for area of circle.
g. Write a program in C for side of Square and find its Area.
h. Write a program in C for length and width of rectangle and find its Area