Docsity

Understanding the Basics of C Programming: Language Features and Syntax, Slides of Computer Science

Partial preview of the text

Download Understanding the Basics of C Programming: Language Features and Syntax and more Slides Computer Science in PDF only on Docsity!

What is Computer Program or

Programming?

A computer program is set of instructions,written in any of the computer languages,as a step by step solution of problems.

Programming is the process of planning,designing,

writing,

testing

and

maintaining

these

procedures

or

programs according to user need.

2. C is a Case Sensitive Language •

The commands in ‘C’ are written inlowercase.

The variables written in C in upper caseletters have different meaning than thevariables used in lower case letters.

C

is

a

Structured

Programming

Language

It

divides

the

programs

in

small

module to perform a specific task.

Through

this

approach

one

can

have

accurate, error free code.

Each module can have – sequence, selectionor iterative statements.

  1. C is Portable Language: •

C

programs

are

run

on

different

environment.

E.g.

if

u

have

written

a

C

program

in

windows 95, then it can be executed in UNIXwithout any or little modification.

docsity.com

A Simple C Program Structure

/* Filename:

hello.c

Author: N.S.Dhindsa

Date written: 03/08/2012Description:

This program prints the greeting

“Hello, World!”

*/

#include <stdio.h>int main ( void ){

printf ( “Hello, World!\n” ) ;return 0 ;

}

Comment: •

A comment is descriptive text used to help areader of the program understand its content.

All comments must begin with the characters/* and end with the characters */

These are called comment delimiters.

The program header comment always comesfirst.

Text surrounded by /* and */ is ignored bycomputer.

Used to describe program.

The

#include

directives

“paste”

the

contents of the files stdio.h, stdlib.h andstring.h into your source code, at the veryplace where the directives appear.

These

files

contain

information

about

some

library

functions

used

in

the

program:

stdio stands for “standard I/O”,

stdlib stands for “standard library”,

string.h includes useful string

manipulation functions.

The Function Body

A left brace (curly bracket)

-- { --begins the body of every function.

A corresponding right brace

-- } --ends the function body.

The reserved word “int” indicates thatmain( ) returns an integer value.

The parentheses following the reservedword

“main”

indicate

that

it

is

a

function.

Braces { and } indicate a block.

The bodies of all functions must becontained in braces.

docsity.com

The printf( ) function

printf( “Prog. In C is easy. %s \n", input );

printf( ) is a library function declared in<stdio.h>

Syntax:

printf(

FormatString

Expr

Expr

FormatString

: String of text to print.

FormatString

has

placeholders

to

show

where to put the values.

printf (“Hello, World!\n”) ;

All

statements

in

C

end

with

a

semicolon(;).

Entire line is called a statement.

printf() prints the text/string of characterswithin double quotes (“ ”).

Escape character () indicates that printf( )should do something out of the ordinary.

docsity.com

return 0 ;

Because

function

main()

returns

an

integer

value,

there

must

be

a

statement that indicates what this valueis.

The statement return 0 indicates thatmain() returns a value of zero to theoperating system.

A value of 0 indicates that the programsuccessfully terminated execution.

docsity.com

Anatomy of a C Program

program header comment preprocessor directives (if any) int main ( void ){

statement (s); return 0 ;

}

C Character Set

The C language consists of:

  1. Alphabets: A to Z (Upper case) , a to z

(lower case)

  1. Digits: 0 to 93. Special Symbols:
[
] {
^

docsity.com