2 Structureofcprogramming
2 Structureofcprogramming
Global declaration
Body
function
statement
Software and basic execution
• File>new>source file
• Save the file (any
name)
• Write the code
• Execute>complie
and run (F11)
Write Comments in your code
• Character that are not executed as part of the program.
• Use for remark or debug the code
• Syntax:
/* …. Comments…..
Comment……………..*/ block comments
//…. Comment line comments (for 1 line only)
Example: write your own notes
/*…*/
Exercise:
Examples:
Y = m*x+c , here we have 4 variables
Fx = a + b, here we have 3 variables
Total = 12 + ab, here we have 3 variables
Rules for Identifiers
1. First character must be alphabetic character or underscore
6. Identifiers are also case sensitive in C (e.g name and Name are two different
identifiers in C).
2) int 20thCentury
3) int char
5) int _BMW2003
6) int Reservedword
7) float BDA24202
9) char jam*kredit
20
How to insert variable data into printf(“ “);
Variable type Define variable (example) Data symbol (general)
Integer int x; %d
int sum;
Decimal float number; %f
float no;
character char room; %c
char sample;
Example: e.g. to display massage with the data type
23
PROGRAM 2-2 Print Sum of Three Numbers
24
PROGRAM 2-2 Print Sum of Three Numbers (continued)
25
PROGRAM 2-2 Print Sum of Three Numbers (continued)
26
EXERCISE:
using C Programming software, develop a complete code to display below output.
Constants
Constants are data values that cannot be changed during
the execution of a program. Like variables, constants have
a type. In this section, we discuss Boolean, character,
integer, real, complex, and string constants.
29
PROGRAM 2-3 Memory Constants (continued)
30
Topics discussed in next section:
Streams
Formatting Input/Output (integer %d, float %f,
character %c)
31
FIGURE 2-15 Stream Physical Devices
32
General Type and Size of Data Type
Data type size Range Format
specifier
Character 1 byte -128 to 127 %c
Integer 2 byte -32768 to %d
32767
Float 4 byte 3.4E(-38) to %f
3.4E(+38)
Double 8 byte 1.7E(-308) to %f
1.7E(+308)
A character variable
holds ASCII value (an integer
number between 0 and 127)
rather than that character itself
in C programming. That value is
known as ASCII value. For
example, ASCII value of 'A' is 65.
PROGRAM 2-6 Print Value of Selected Characters
41
PROGRAM 2-6 Print Value of Selected Characters (continued)
42
PROGRAM 2-6 Print Value of Selected Characters (continued)
int main()
{
char first=‘T’;
char second=63;
printf(“the first example as letter is %c\n”, first);
printf(“the first example as number is %d\n”, first);
printf(“the second example as letter is %c\n”, second);
printf(“the second example as number is %d\n”, second);
return 0;
}
Output
the first example as letter is T
the first example as number is 84
the second example as letter is ?
the second example as number is 63
Character input and character output
Write a program to read a single character from the keyboard and store it in
a variable of type char using the scanf( ) function with the format specifier
%c.
#include <stdio.h>
int main()
{
char ch=0;
scanf("%c",&ch);
printf("The character is %c and the code value is %d",ch, ch);
return 0;
}
Display Integer In Different Styles (- is space)
Note
Use single quotes for character constants. (%c)
Use double quotes for string constants. (%s)
Example:
printf(“I love %c %s\n",‘C', "programming");
I love C programming
float j=12.345;
PROGRAM 2-7 Calculate a Circle’s Area and Circumference
51
PROGRAM 2-7 Calculate a Circle’s Area and Circumference (continued)
56
THANK YOU