Você está na página 1de 11

Programming in C

Lecture 1
By Fahmida Sultana
Quick Introduction to C
– The C language facilitates a structured and
disciplined approach to computer program design.
– Goal is to get you to a point where you can write
useful programs.
– In this course we will Concentrate on the basics:
• Variables
• Constants
• Arithmetic Calculation
• Control-Flow
• Functions
• Rudiments of Input and Output
Your first C Program
• The Hello World Program

1 /* Print the words hello, world */

2 # include <stdio.h>
3 void main()
4 {
5 printf(“Hello,World\n”);
6 }

– Compile this program.


– Run the executable.
– It should print “Hello World”.
Explanation of each line of the
program- Line 1 is a comment

• Line 1begins with /* and end with */


indicates a comment.
• Comment helps to document programs
and improve program readability
• Comments do not cause the compiler to
perform any action when the program is
running.
Line 2 indicates Libraries
• Line 2 is a directive to the C preprocessor
• Lines beginning with # are processed by the
preprocessor program before the program is
compiled.
• This line tells the preprocessor to include the
contents of the standard input/output
header(stdio.h) file in the program.
• This header file contains information that is used
by the compiler to call the standard library
functions like printf
Line 3 is a Function
• C programs are built with functions and variables
regardless of its size.
– Functions specify computing operations that needs to be done.
– Variables store values during the computation.
• What is main function?
– “main” is a special function where your program starts
executing.
– It is the beginning of your program.
– Every c program must have the “main” function
– Parentheses after main indicates that main is a function
– Within Left and right brace function body is placed
Line 5 - String of Characters
“hello world\n”
– Enclosed in double quotation marks.
– \n is a newline character.
– \ is a escape sequence. When encountering a
backslash in a string , the compiler looks
ahead at the next character and combines it
with the backslash to perform the task
– Some of the escape sequences are shown in
the next slide.
Example of some escape
sequence
• \t Horizontal tab.
• \b Backspace
• \\ backslash. (insert backslash in the string)
• \“ Double quote. (is used to print double quote
character in a string)

• \a Alert. (Sound the system bell)


Comments

Comments can be written in two


ways:

1. /* print Hello World


in screen */
2. // This is a single line comment
Few Definitions
• Source Code: The text of a program that a user can
read. The source code is the input to C compiler.
• Object Code: Translation of the source code of a
program into machine code.
• Library: The file contains standard functions that can be
used by your program. <stdio.h> Without it you cannot
print anything to the screen.
• Compile Time: The events that occur while your program
is being compiled (a common occurrence during compile
time is Syntax Error).
• Run Time: The events that occur while your program is
executing.
Task!!!
• Compile and Run hello world program .
Leave out parts and see what happens.
• Try to write hello world program using
three printf statements.

Você também pode gostar