You are on page 1of 23

CSE 101

Computer Programming

Basic Syntax
Comments, Input and Output
Comments
• Comments are often helpful for understanding the specific
functions of any part of a code.
• All comments are ignored by the compiler. As if they don’t
exist.
• A single line comment must start with “ // “ and ends with
the line.
• A multi line comment starts with “ /* “ and ends with “ */ “
A Code With Comments
#include <stdio.h>
int main( )
{
// This is a comment. Only use it when single line
/* This format is used for both single line
and multi line */
printf("Only this line will be executed"); // This is also a comment
/* Remember that no comment will be executed, they are ignored */
return 0;
}
Comments
Notice that the color of comments is changed from normal text, usually
the color is light.
Data Types

There are mainly 4 data types are in C


• int ( Integer, It takes 2 bytes )
• char ( Character, It takes 1 byte )
• float ( Any number, Takes 4 bytes )
• double ( Any number, Takes 8 bytes )
Example
Example
Case Sensitivity

• Remember well that C is case sensitive.


• That means ‘A’ and ‘a’ are not same.
• Always use small letters in C. Try to avoid capital letters.
• int x=10 is correct but Int x=10 is not correct
ASCII Code for char
• ASCII is abbreviated from American Standard Code for Information
Interchange, it is a character encoding standard for electronic
communication.

• Each character has a unique integer value in ASCII. Computer stores


that value and when it requires to show the character then it is
converted into character again.

• To know the ASCII number of a character just use printf and %d


instead of %c
ASCII Table
ASCII Table
• Try to remember the following information
• ‘A’ has number 65 and ‘Z’ has 90. All block letters are serially
within this limit.
• ‘a’ has number 97 and ‘z’ has 122. All small letters are serially
within this limit.

• Suppose x is a char defined in the code:


• To print the character: printf(“%c”,x);
• To print the ASCII number: printf(“%d”,x);
• Though x is not an integer we are showing the ASCII integer,
so %d is used.
Printf Function
• This function is associated with stdio.h header file.
• Whenever we use printf, the text inside the double quotation is
printed in the screen.

printf(“ This line is printed “)

• So any line we can print using this function. But for printing a data
type we need some modification.
Printing Data Types
• For printing the data types we need to write specific text for each
type.

• %d is for int
• %c for char
• %f for float
• %lf for double (Double is nothing but long float)

• For creating a new line for output we need to put “ \n “ within printf.
Example
Example
Scanf function

• The same way printf is used to give a output, scanf is another


function that takes an input from the user.

• scanf is also within the header file stdio.h

• When the command is executed, the user have to give an


input otherwise next line will not be executed.
Example
Some Suggestions for Scanf
• Using a printf before scanf is a good idea, otherwise the user
will have no idea what he should do.

• Don’t forget to use the ‘&’ sign before the data type name. It
is called “assign to”.

• Scanf(“%d %lf”,&a,&b); this line implies that the first input


will be an integer and will be assigned to a and second one is
a double and will be assigned to b.
Class Work
Write a complete C code that does the following:

1. Suppose the user wants to know the ASCII code of a character.


First tell him to give the character as input.
2. Take the input from the user using scanf, remember that the
input is a character.
3. Show him ASCII number for that character as:
The ASCII code for d is 100 or
The ASCII code of p is 112
Solve
Problem
• Take input from the user a lower case letter (suppose ‘r’ ) and you
have to give output the upper case of the corresponding ( for ‘r’ it is
‘R’.

• Solve in next class. Think about this and try to solve

You might also like