0% found this document useful (0 votes)
36 views30 pages

CSE115 Lec04 OverviewOfC

This document provides an overview of the basic structure of a C program. It discusses the main components of a C program including preprocessor commands, functions, variables, statements, expressions, and comments. It also explains key C concepts such as data types, input/output functions like printf() and scanf(), and how variables are used in assignment statements. The document uses a simple "Hello World" program as an example to demonstrate how these various components come together in a basic C program.

Uploaded by

Md. Yeasin
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)
36 views30 pages

CSE115 Lec04 OverviewOfC

This document provides an overview of the basic structure of a C program. It discusses the main components of a C program including preprocessor commands, functions, variables, statements, expressions, and comments. It also explains key C concepts such as data types, input/output functions like printf() and scanf(), and how variables are used in assignment statements. The document uses a simple "Hello World" program as an example to demonstrate how these various components come together in a basic C program.

Uploaded by

Md. Yeasin
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/ 30

Computing Concepts

Lecture 4: Overview of C

North South University CSE115


Basic Structure of a C program

A C program basically consists of the following parts:


• Preprocessor Commands
• Functions
• Variables
• Statements & Expressions
• Comments

North South University CSE115 2


Basic Structure of a C program

Example:

#include <stdio.h>

int main()
{
/*my first program in C*/
printf(“Hello, World! \n”);

return 0;
}

North South University CSE115 3


Basic Structure of a C program

Example:

#include <stdio.h> Preprocessor Directives

int main() Function Main


{
/*my first program in C*/ Comment
printf(“Hello, World! \n”);
Statements
return 0;
}

North South University CSE115 4


Preprocessor Directives

• Preprocessor:
– A system program that modifies a C program prior to its
compilation.
• Preprocessor Directives:
– A C program line beginning with # that provides an instruction to
the preprocessor.
– The two most common directives are #include and #define
– The directive gives a program access to a library. This directive
causes the preprocessor to insert definitions from a standard
header file into a program before compilation.
– Example: In our “Hello World” program the header file stdio.h
contains information about standard input and output functions
such as printf.

North South University CSE115 5


Function: main()
• Marks where program execution begins.
• SYNTAX:
int main(void)
{
function body
}
• A function body has two parts: declarations and executable
statements.
• The declarations tell the compiler what memory cells are
needed in the function
• The executable statements are translated into machine
language and later executed.
• Braces ({,}) mark the beginning and end of the body of the
function main.

North South University CSE115 6


An Example

North South University CSE115 7


An Example

North South University CSE115 8


An Example (contd.)

North South University CSE115 9


North South University CSE115 10
An Example (contd.)

• What happens in the computer memory?


memory memory memory

MileToKm.exe MileToKm.exe MileToKm.exe

miles miles miles


? 10.5 10.5

kms kms kms


? ? 16.89

At the beginning
After user enters: 10.5 to After this line is executed:
Do not assume that
uninitialised variables scanf("%f", &miles); kms = KMS_PER_MILE * miles;
contain zero! (Very
common mistake.)

North South University CSE115 11


Reserved Words

• A word that has special meaning in C

North South University CSE115 12


Standard Idenfiers

• A word having special meaning but one that a


programmer may redefine. However, redefine is not
recommended.
• printf and scanf are names of operations which are
standard identifiers.
• These standard identifiers can be redefined and used by
the programmer for other purposes.
• If a standard identifier is redefined, then C will no longer
be able to use it for its original purpose.

North South University CSE115 13


User-Defined Identifiers

• User-defined identifiers are chosen by the users


themselves
– to name memory cells that will hold data and program results
– To name operations that the user defines.
• Syntax rules:
1. An identifier must consist only of letters, digits, and
underscores.
2. An identifier cannot begin with a digit.
3. A C reserved word cannot be used as an identifier.
4. An identifier defined in a C standard library should not be
redefined.

North South University CSE115 14


User-Defined Identifiers

• Valid Identifiers
letter_1, letter_2, inches, cent,
CENT_PER_INCH, Hello, variable
• Invalid Identifiers

North South University CSE115 15


Reserved Words and Identifiers

• Example:

North South University CSE115 16


Variables

• Variable:
– A name associated with a memory cell whose value can change
• Variable declarations:
– Statements that communicate to the compiler the names of
variables in the program and the kind of information stored in
each variable.
– SYNTAX: <variable_type> variable_list;
– Examples:
int count;
double x, y, z;
char first_initial;
– C requires that every variable used in a program would be
declared.

North South University CSE115 17


Data Types

• A set of values and operations that can be performed on


those values.
• Objects of data type can be variables or constants
• Some basic data types:
– int
– float
– double
– char
• int
– Abstraction for integers
– Keyword: int
int num;
num = 5;

North South University CSE115 18


Data Types (contd.)

• float
– Abstraction of numbers that has fractional parts.
– Keyword: float
float length;
length = 5.64;
• Double
– Abstraction of real numbers consisting both integral part and a
fractional part
– Scientific notation can also be used to represent real numbers as
‚double‘ type.
– Keyword: double
double value;
value = 7E5; //value = 700000

North South University CSE115 19


Data Types (contd.)

• char
– Represents an individual character value – a letter, a digit, or a
special symbol.
– Each type char value is enclosed in apostrophes (single quotes)
like this: ‘A’ ‘z’ ‘2’ ‘9’ ‘*’ ‘:’
‘”’ ‘ ‘

• ASCII code
– A particular code that specifies the integer representing each
char value.
– ASCII: American Standard Code for Information Interchange

North South University CSE115 20


Data Types (contd.)

North South University CSE115 21


Assignment Statements

• An instruction that stores a value or a computational


result in a variable
• FORM: variable = expression;
• Example: kms = KMS_PER_MILE * miles;

North South University CSE115 22


Input/Output Operations and Functions

• Input operation:
– An instruction that copies data from an input device into memory.
• Output operation:
– An instruction that displays information stored in memory.
• Input/output function:
– A C function that performs an input or output operation.
– Most common input/output functions are supplied as part of the
C standard input/output library to which access can be gained
through the preprocessor directive
#include <stdio.h>
– Some common input/output functions in stdio.h:
• printf()
• scanf()
• getchar()
• putchar()

North South University CSE115 23


The printf function

• Used to send data to the standard output (usually the


monitor) to be printed according to specific format.
• General format:
– printf(format string);
• A sequence of any number of characters
surrounded by double quotation marks.
– printf(format string, print list);
• Format string is a combination of text, conversion
specifier and escape sequence.

North South University CSE115 24


The printf function (contd.)

• Example:
– printf(“Thank you”);
– printf (“Total sum is: %d\n”, sum);
• %d is a placeholder (conversion specifier)
–marks the display position for a type
integer variable
• \n is an escape sequence
–moves the cursor to the new line

North South University CSE115 25


The printf function (contd.)

• Format string:
– In a call to printf, a string of characters enclosed in double
quotes (“”), which specifies the form of the output line.
• Placeholder:
– A symbol beginning with % in a format string that indicateswhere
to display the output value.

North South University CSE115 26


The printf function (contd.)

• Escape Sequence

North South University CSE115 27


The scanf function

• Read data from the standard input device (usually


keyboard) and store it in a variable.
• General format:
– scanf(“Format string”, &variable);
• Notice ampersand (&) operator :
– C address of operator
– it passes the address of the variable instead of the
variable itself
– tells the scanf() where to find the variable to store the
new value
• Example:
int age;
printf(“Enter your age: ”);
scanf(“%d”, &age);

North South University CSE115 28


The scanf function (contd.)

• If you want the user to enter more than one value, you
serialize the inputs.
• Example:
float height, weight;
printf(“Enter height and weight: ”);
scanf(“%f %f”, &height, &weight);

North South University CSE115 29


The return statement

• Last line in the main function


• Transfers control from the program to the operating
system.
• SYNTAX: return expression;
• Example: return (0);

North South University CSE115 30

You might also like