0% found this document useful (0 votes)
9 views15 pages

Lecture 03

Uploaded by

imalshaamashan
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)
9 views15 pages

Lecture 03

Uploaded by

imalshaamashan
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/ 15

Fundamentals of Programming

COSC 11023 / COST 11023

Introduction to C

Sachintha Pitigala
© 2023
C Programming Language
● Designed by Dennis Ritchie (1972)
● Used for developing UNIX OS

2
C Features
● Structured programming, modular programming

● Highly portable

● Efficient (fast)

● C has the features of high level language and low


level language
3
Uses of C Language
● Operating Systems
● Compilers
● Device Drivers
● Database Systems
● Word Processors

4
First C Program

Output: Welcome to C Programming 5


Creating Programs on Linux
● Use emacs, vi, or some other text editor to type
in and save program. Good idea to:
● Name program something meaningful

● Establish conventions for naming

● Add a .c suffix to the name


6
Running Programs on Linux
● Compile program
gcc hello.c [-o whatever]

● This produces the executable named whatever, or


a.out by default.

● Note: linker will be required when our programs


become more sophisticated – not necessary now.
7
Statements in C
● Note: all statements end with a semicolon!

● Statements can (with a few exceptions) be broken


across lines or ganged on a single line

● Blank lines have no effect

● Extra spaces between tokens has no effect.

● Comments are ignored by the compiler


8
Comments in C
● You insert comments to document programs and improve
program readability.

● Comments do not cause the computer to perform any action


when the program is run.

● Comments are ignored by the C compiler and do not cause


any machine-language object code to be generated.

● Comments also help other people read and understand your


program.
9
Comments in C
● Comments in C can be divided into two types.
○ Multi-line Comments
○ Single line Comments

● Multi-line Comment
/* Multi-line comment
A first program in C */

● Single line Comment


● //single line comment
10
Preprocessor Directives
● #include <stdio.h>

● is a directive to the C preprocessor.

● Lines beginning with # are processed by the preprocessor before the program
is compiled.

● Above include line tells the preprocessor to include the contents of the standard
input/output header (<stdio.h>) in the program.

● This header contains information used by the compiler when compiling calls to
standard input/output library functions such as printf. 11
Main Function
● int main()

● is a part of every C program.

● C programs contain one or more functions, one of


which must be main.

● Every program in C begins executing at the


function main.
12
printf() Command
● printf() sends output to standard out, which for now we
can think of as the terminal screen.

● The characters normally print exactly as they appear


between the double quotes in the printf statement.

● General form;
printf(format descriptor, var1, var2, …);
13
printf() Command
● The backslash (\) is called an escape character.

● It indicates that printf is supposed to do something out of the ordinary.

● When encountering a backslash in a string, the compiler looks ahead at the


next character and combines it with the backslash to form an escape
sequence.

● The escape sequence \n means newline.

● When a newline appears in the string output by a printf, the newline causes the
cursor to position to the beginning of the next line on the screen. 14
Some Common Escape Characters
Sequence Description

\n New Line. Position the cursor at the beginning of the next line.

\t Horizontal tab. Move the cursor to the next tab stop.

\v Insert a vertical tab in the output.

\\ Insert a backslash character in a string.

\’ Insert a single quotation mark in a string

\” Insert a double quotation mark in a string

\? Insert a question mark 15

You might also like