0% found this document useful (0 votes)
24 views5 pages

01 Session Printf

Uploaded by

deshikadewmini03
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
24 views5 pages

01 Session Printf

Uploaded by

deshikadewmini03
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

Session 1

First Program in C

1.1 Hello World - the First Program in C


In this session, a simple program in C language is developed.

Listing 1.1: Displaying a text on the Console by using printf()

columns
columns
# include < stdio .h > co
columns
columns co
int main ( void )
columns
columns co
columns
columns
{ co
printf ( " Hello , World ! " ) ;
columns
columns co
columns
columns
return 0; co
columns
columns
} co

1.2 Understanding the Program


ˆ Program in the Listing 1.1 is written in C language.
ˆ Program is a set of statements, written within the boundaries of main().
int main(void)
{
...
YOUR PROGRAM STATEMENTS
...
}

ˆ Boundaries are indicated by { and }


ˆ C Statements ends with a semicolon ;
ˆ Statements are executed sequentially.
ˆ in the Program 1.1
– printf("Hello, World!"); is a statement.
– return 0; is a statement.
– During the program execution, these two statements are executed sequentially.
ˆ #include <stdio.h> enables the use of functions defined in the library stdio.h, such as printf().

E1.1 Observe the output of the program in Listing 1.1.


E1.2 Modify the program in Listing 1.1 to display
(a) your name.
(b) your name for 5 times.
(c) names of two friends.
1
(d) your name first and then the town you live.
(e) name of the town you live first and then your name,
E1.3 Delete a randomly chosen character from the source code in Listing 1.1, and compile to see if error
messages are generated. If so, try to cross check error messages with respect to the deleted character.
Repeat this for different characters.

1.3 Displaying Characters and Strings


ˆ Characters are the symbols that can be entered by using the Keyboard.

– Eg. a, B, t, &, ?, ..

ˆ String (also called Text, or Text-String) is made out of characters.

– Eg. Hello, World!


What is your name?, ..
ˆ printf() is function that can be used to display a string on the Console. String should be given between
” and ”.

– E.g. printf("Hello, World!") will display Hello, World! on the Console window.
ˆ printf()displays the string (i.e. all characters of the string) starting from the current cursor position.

ˆ Usually a Console window is designed to display 25 x 80 characters, in a matrix of 25 raws and 80


columns.

ˆ Once a character is displayed, the cursor position is automatically advanced to the next character position
of the terminal window.

1.4 Escape Sequences


ˆ Escape sequences are special characters, having special meanings, when used as a character (eg. in
printf()).
ˆ \n is an escape sequence called newline character.

ˆ \n moves the cursor to the beginning of the next line.

ˆ Execute the program in the Listing 1.2, to see the effect of \n.

ˆ \t is an escape sequence called tabulator.

ˆ \t can be used to align characters/text along with tab positions of the terminal across raws.

ˆ By default, the size of a tab is 8 characters.

ˆ Program in the Listing 1.3 shows the effect of using \t.

– Why the last line is not aligned with other two lines? Correct the program in the Listing 1.3 to
make all three lines aligned along a single column.

ˆ Escape sequences are listed in the Table 1.1.

Exercise 1.4 Execute the program in Listing 1.2 and notice the effect of (\n).
Exercise 1.5 Execute the program in Listing 1.3 and observe the effect of \t).

Exercise 1.6 Modify the program in Listing 1.3 to observe the effect of other escape sequences given in Table
1.1.

2
Table 1.1: Escape sequences

Escape Sequence Name


\n New Line
\r Carriage Return
\v Vertical Tab
\t Horizontal Tab
\b Back Space
\’ Single Quote
\" Double Quote
\? Question Mark
\a Audible Bell
\f Form Feed
\nnn Arbitrary octal value nn
\Xnn Arbitrary hexadecimal value nn
\unnnn Universal character name (Unicode) nnnn

Listing 1.2: Formatting output string with newline characcter \n

# include < stdio .h >


columns
columns co
columns
columns co
columns
columns
int main ( void ) co
{
columns
columns co
columns
columns
printf ( " Hello , World !\ n " ) ; co
printf ( " This is Sri Lanka !\ n " ) ;
columns
columns co
columns
columns
return 0; co
columns
columns
} co

1.5 Comments
ˆ Comments are part of the source code but ignored during the compilation and making the executable.

ˆ Comments can be inserted to any part of the the source code.

ˆ Comments are written between \* and *\

ˆ What are the comments in the Program 1.4

Listing 1.3: Formatting output string with tabulator \t (tabulator

# include < stdio .h >


columns
columns co
columns
columns co
columns
columns
int main ( void ) co
columns
columns
{ co
columns
columns
printf ( " Name \ t Telephone \ n " ) ; co
columns
columns
printf ( " Nimal \ t 0718978654\ n " ) ; co
printf ( " Sama \ t 0778434565\ n " ) ;
columns
columns co
columns
columns
printf ( " Gorge Washington \ t 0789123456\ n " ) ; co
columns
columns co
columns
columns
return 0; co
columns
columns
} co

3
Listing 1.4: Comments are written between /* and */

columns
columns
/* This Program displays information on the Faculty of Engineering co
columns
columnsUniversity of Ruhuna co
*/
columns
columns co
columns
columns
# include < stdio .h > co
columns
columns co
columns
columns
int main ( void ) co
{ /* Begining of the Program */
columns
columns co
columns
columnsprintf ( " Faculty of Engineering , " ) ; /* Display Infomation */ co
columns
columnsprintf ( " University of Ruhuna is situated in Hapugala , Galle .\ n " ) ; co
columns
columns co
columns
columnsreturn 0; /* End of the program */ co
}
columns
columns co
columns
columns co

1.6 Assignments
1.1 Write a program to display the following output.
*******
*****
****
***
**
*

1.2 Write a program to display the following output.


*
* *
* *
* *
*
*****
1.3 Write a program to display the following output.
M
MM
MMM
MMMM
MMMMM
MMMMMM
1.4 Display names of five animals and five birds in two left aligned columns.
1.5 Write a program to display the following output.

University of Ruhuna
Faculty of Engineering
======================
CONTINOUS ASSESSMENT MARKS
Module:EE5217 Measurements and Instrumentation

Student Reg.No MidSem (20%) Tutes (30%) Total(50%)


--------------------- ------------ ------------ ------------ -----------
Mr. Deegange A.B.C. RU/E/G/2345 12.0 23.5 34.5
Ms. Wijerathna D.E. RU/E/G/2346 11.0 24.5 34.5
Mr. Jinadasa F.G.H. RU/E/G/2347 17.0 25.5 42.5
Mr. Fernando I.J. RU/E/G/2348 13.0 26.5 34.5
FILL WITH YOUR DATA RU/E/G/???? 20 30 50
1.6 Display the following
4
(a) David says "Programming is fun!"
(b) My data is found in C:\My Computer\My Folder
(c) [email protected]:~\$

—-

You might also like