Programming for Problemsolving Clab Week-2
Programming for Problemsolving Clab Week-2
Week-----2
• Presented By
1
Facts About C Language
2
Structure of A C Program
3
The most basic program in C
#include<stdio.h>
main()
{
printf(“Hello World”);
}
4
Output Screen
5
Various parts of Programming structure
/* Comments */
6
#include<stdio.h>
7
int/void main()
main()
8
Braces
• Two curly brackets "{...}" are used to group all statements.
or
• Curly braces which shows how much the main() function
has its scope.
printf()
• It is a function in C, which prints text on the screen. or
• This is another pre-defined function of C which is used to
be displayed text string in the screen.
return 0
• At the end of the main function returns value 0.
9
printf and scanf functions in C
10
C printf functions
11
12
Important Notes
13
scanf Functions
15
scanf("%d", &b);
char ch = 'a';
// some code
ch = 'l';
Rules for naming a variable
• A variable name can have only letters (both
uppercase and lowercase letters), digits and
underscore.
• The first letter of a variable should be either a letter
or an underscore.
• There is no rule on how long a variable name
(identifier) can be. However, you may run into
problems in some compilers if the variable name is
longer than 31 characters.
• Note: You should always try to give meaningful names
to variables. For example: firstName is a better
variable name than fn.
• C is a strongly typed language. This means that the
variable type cannot be changed once it is declared.
For example:
char 1
• Only really four basic types:
• char
int 4
• int (short, long, long long, unsigned)
• float short 2
• double
long 8
• Size of these types on
• CLEAR machines:
long long 8
float 4
• In the ASCII character set, each binary value between 0 and 127
represents a specific character.
• Most computers extend the ASCII character set to use the full range of
256 characters available in a byte. The upper 128 characters handle
special things like accented characters from common foreign languages.
ASCII
Special
control
characters From “man ascii”:
int main()
{
int testInteger = 5;
printf("Number = %d", testInteger);
return 0;
}
Float and double Output
#include <stdio.h>
int main()
{
float number1 = 13.5;
double number2 = 12.4;
#include <stdio.h>
int main()
{
char chr = 'a';
printf("character = %c.", chr);
return 0;
}
1. Integer Input/output
Notice, that we
#include <stdio.h> have used
int main() &testInteger
{ inside scanf().
int testInteger;
It is because
printf("Enter an integer: "); &testInteger gets
scanf("%d", &testInteger); the address of
printf("Number = %d",testInteger); testInteger, and
the value entered
return 0;
by the user is
} stored in that
address.s
2. Write a program to print the ASCII value.
#include <stdio.h>
int main()
{
char chr;
printf("Enter a character: ");
scanf("%c", &chr);
// When %c is used, a character is displayed
printf("You entered %c.\n",chr);
// When %d is used, ASCII value is displayed
printf("ASCII value is % d.", chr);
return 0;
}
Sample
Program
Thank you
32