0% found this document useful (0 votes)
2 views

Programming for Problemsolving Clab Week-2

This document provides an overview of the C programming language, including its structure, basic syntax, and key functions such as printf and scanf. It also covers data types, variable naming conventions, and ASCII character representation. The content is aimed at students in a programming laboratory course at the University of Engineering and Management, Kolkata.

Uploaded by

lpastore1997
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

Programming for Problemsolving Clab Week-2

This document provides an overview of the C programming language, including its structure, basic syntax, and key functions such as printf and scanf. It also covers data types, variable naming conventions, and ASCII character representation. The content is aimed at students in a programming laboratory course at the University of Engineering and Management, Kolkata.

Uploaded by

lpastore1997
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 32

Programming for Problem Solving – I Laboratory

Week-----2

• Presented By

• Dr. Sudipta Basu Pal

• COMPUTER SCIENCE AND ENGINEERING DEPT


• UNIVERSITY OF ENGINEERING AND MANAGEMENT, KOLKATA

1
Facts About C Language

C is a structured programming language. It is considered a high-


level language because it allows the programmer to concentrate on
the problem at hand and not worry about the machine that the
program will be using. That is another reason why it is used by
software developers whose applications have to run on many
different hardware platforms.

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 */

• Comments are a way of explaining what makes a


program. The compiler ignores comments and used
by others to understand the code. or
• This is a comment block, which is ignored by the
compiler. Comment can be used anywhere in the
program to add info about the program or code block,
which will be helpful for developers to understand the
existing code in the future easily.

6
#include<stdio.h>

• stdio is standard for input/output, this allows us to


use some commands which includes a file called
stdio.h. or
• This is a preprocessor command. That notifies the
compiler to include the header file stdio.h in the
program before compiling the source-code.

7
int/void main()

• int/void is a return value, which will be explained in a


while.

main()

• The main() is the main function where program


execution begins. Every C program must contain only
one main function. or
• This is the main function, which is the default entry
point for every C program and the void in front of it
indicates that it does not return a value.

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);

The program will read in an integer


value that the user enters on the
keyboard (%d is for integers, as is
printf, so b must be declared as an
int) and place that value into b.
• The scanf function uses the same placeholders as
printf:
• int uses %d
• float uses %f
• char uses %c
• character strings use %s
Variables
• In programming, a variable is a container (storage area) to hold data.
• To indicate the storage area, each variable should be given a unique
name (identifier). Variable names are just the symbolic
representation of a memory location. For example:

int playerScore = 95;

• Here, playerScore is a variable of int type. Here, the variable is


assigned an integer value 95.
• The value of a variable can be changed, hence the name variable.

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:

int number = 5; // integer variable


number = 5.5; // error

• Here, the type of number variable is int. You cannot


assign a floating-point (decimal) value 5.5 to this
variable. By the way, to store the decimal values in C,
you need to declare its type to either double or float.
Data types in C Type Size (bytes)

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

• Sizes of these types double 8


• vary from one machine
• to another!

Simple Data Types


• It is an acronym for the American Standard
Code for Information Interchange.
• It is a standard seven-bit code that was first
proposed by the American National
ASCII Standards Institute or ANSI in 1963, and
finalized in 1968 as ANSI Standard X3.4.
• The purpose of ASCII was to provide a
standard to code various symbols ( visible
and invisible symbols)
ASCII

• 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”:

| 0 NUL| 1 SOH| 2 STX| 3 ETX| 4 EOT| 5 ENQ| 6 ACK| 7 BEL|


| 8 BS | 9 HT | 10 NL | 11 VT | 12 NP | 13 CR | 14 SO | 15 SI |
| 16 DLE| 17 DC1| 18 DC2| 19 DC3| 20 DC4| 21 NAK| 22 SYN| 23 ETB|
| 24 CAN| 25 EM | 26 SUB| 27 ESC| 28 FS | 29 GS | 30 RS | 31 US |
| 32 SP | 33 ! | 34 " | 35 # | 36 $ | 37 % | 38 & | 39 ' |
| 40 ( | 41 ) | 42 * | 43 + | 44 , | 45 - | 46 . | 47 / |
| 48 0 | 49 1 | 50 2 | 51 3 | 52 4 | 53 5 | 54 6 | 55 7 |
| 56 8 | 57 9 | 58 : | 59 ; | 60 < | 61 = | 62 > | 63 ? |
| 64 @ | 65 A | 66 B | 67 C | 68 D | 69 E | 70 F | 71 G |
| 72 H | 73 I | 74 J | 75 K | 76 L | 77 M | 78 N | 79 O |
| 80 P | 81 Q | 82 R | 83 S | 84 T | 85 U | 86 V | 87 W |
| 88 X | 89 Y | 90 Z | 91 [ | 92 \ | 93 ] | 94 ^ | 95 _ |
| 96 ` | 97 a | 98 b | 99 c |100 d |101 e |102 f |103 g |
|104 h |105 i |106 j |107 k |108 l |109 m |110 n |111 o |
|112 p |113 q |114 r |115 s |116 t |117 u |118 v |119 w |
|120 x |121 y |122 z |123 { |124 | |125 } |126 ~ |127 DEL|
Integer Output
#include <stdio.h>

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;

printf("number1 = %f\n", number1);


printf("number2 = %lf", number2);
return 0;
}
Print Characters

#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

Email Id: Sudipta.Basu@uem.edu.in

32

You might also like