0% found this document useful (0 votes)
12 views

chapter1a-introductiontocprogramming

The document provides an introduction to the C programming language, covering its structure, variable declarations, types, and control statements. It explains the significance of libraries, the rules for identifiers, and the use of comments in C programs. Additionally, it outlines the syntax for conditional statements and loops, emphasizing the importance of variable initialization and constants.

Uploaded by

derilas147
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views

chapter1a-introductiontocprogramming

The document provides an introduction to the C programming language, covering its structure, variable declarations, types, and control statements. It explains the significance of libraries, the rules for identifiers, and the use of comments in C programs. Additionally, it outlines the syntax for conditional statements and loops, emphasizing the importance of variable initialization and constants.

Uploaded by

derilas147
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 56

Introduction to the C

Language
❖ Structure of C Program
❖ First C Program
❖ Write comments
❖ The Greeting Program
C Programs

• Case matters, white space does not


• Comments go between /* and */
• Each statement is followed by a semicolon
• Execution begins in the main function:

int main(int argc, char* argv[])

{ /* ignore this */
/* start here */
return 0; /*end here */
}

2
C Libraries

• C is a lightweight language. Most of its intelligence is


compartmentalized in libraries.
• Almost all c programs use the “stdio” or standard
input/output library. Many also use the “math” library.
• To use a library, include the header file (i.e., “stdio.h”) at
the top of the file.

Computer Science: A
Structured Programming
Approach Using C
Structure of a C Program
The Greeting Program
PROGRAM 1 The Greeting Program
Examples of Block Comments
Examples of Line Comments
Variable

• Must be a valid identifier.


• Must not be a keyword
• Names are case sensitive.
• Variables are identified by only first 32 characters.
• Library commonly uses names beginning with _.
Variable

• Naming Styles: Uppercase style and Underscore style


• lowerLimit lower_limit
• incomeTax income_tax

• The most common types are: char, int, float, and double.
Declaring a Variable

• Each variable used must be declared.


• A form of a declaration statement is
• data-type var1, var2,…;
• Declaration announces the data type of a variable and
allocates appropriate memory location. No initial
value (like 0 for integers) should be assumed.
• It is possible to assign an initial value to a variable in
the declaration itself.
Declaring a Variable

• data-type var = expression;


• Examples
int sum = 0;
char newLine = ‘\n’;
float epsilon = 1.0e-6

Computer Science: A
Structured Programming
Approach Using C
Global and Local Variable

• Global Variables
• These variables are declared
outside all functions.
• Life time of a global variable is
the entire execution period of the
program.
• Can be accessed by any function
defined below the declaration, in
a file.
Global and Local Variable

• Local Variables
• These variables are declared
inside some functions.
• Life time of a local variable is the
entire execution period of the
function in which it is defined.
• Cannot be accessed by any other
function.
• In general variables declared
inside a block are accessible only
in that block.
Variable Types

• Declare a variable before you use it:


• int x; /* declares an integer called x.
Its value is not assigned. */
• float y, z = 3.14159; /* declares two
floating point numbers. z is set equal to
pi */
• z = 4; /* now z is equal to 4 */
• myVal = 2; /* This would be an error,
because myVal was not yet declared */
Types

A type defines a set of values and a set of operations


that can be applied on those values.

Topics discussed in this section:


Void Type
Integral Type
Floating-Point Types
Data Types
Logical Operators

• C defines these logical operators:


• <, >, <=, >= and == (the equivalence operator)

• You can compare any variable. Characters are compared


based on their ASCII values.

• All answers will be true (not zero) or false (0)


Logical Operators

• &&, || and ! are the three logical operators.


• expr1 && expr2 has a value 1 if expr1 and expr2 both
are nonzero.
• expr1 || expr2 has a value 1 if expr1 and expr2 both
are nonzero.
• !expr1 has a value 1 if expr1 is zero else 0.
• Example
• if ( marks >= 40 && attendance >= 75 ) grade = ‘P’
• If ( marks < 40 || attendance < 75 ) grade = ‘N’
Logical Operator
Logical Operator
Identifiers

One feature present in all computer languages is the


identifier. Identifiers allow us to name data and other
objects in the program. Each identified object in the
computer is stored at a unique address.
Rules for Identifiers
Note
An identifier must start with a letter or underscore:
it may not have a space or a hyphen.
Note
C is a case-sensitive language.

Computer Science: A
Structured Programming 25
Approach Using C
Examples of Valid and Invalid Names
Character Types
Integer Types
Note
sizeof (short) ≤ sizeof (int) ≤ sizeof (long) ≤ sizeof (long long)
Typical Integer Sizes and Values for Signed Integers
Floating-point Types
Note
sizeof (float) ≤ sizeof (double) ≤ sizeof (long double)
Type Summary
Variables

Variables are named memory locations that have a type,


such as integer or character, which is inherited from their
type. The type determines the values that a variable may
contain and the operations that may be used with its
values.

Topics discussed in this section:


Variable Declaration
Variable Initialization
Variables
Examples of Variable Declarations and Definitions
‘B’

Variable Initialization
Note
When a variable is defined, it is not initialized.
We must initialize any variable requiring
prescribed data when the function starts.
PROGRAM 2 Print Sum of Three Numbers
PROGRAM 2 Print Sum of Three Numbers (continued)
PROGRAM 2 Print Sum of Three Numbers (continued)
Constants

Constants are data values that cannot be changed during


the execution of a program. Like variables, constants
have a type. In this section, we discuss Boolean,
character, integer, real, complex, and string constants.

Topics discussed in this section:


Constant Representation
Coding Constants
Note
A character constant is enclosed in single quotes.
Symbolic Names for Control Characters
Examples of Integer Constants
Examples of Real Constants
Some Strings
Null Characters and Null Strings
Note
Use single quotes for character constants.
Use double quotes for string constants.
PROGRAM 3 Memory Constants
PROGRAM 3 Memory Constants (continued)
The if statement

• Syntax: if (expression) statement;


• If the expression is true (not zero), the statement is
executed. If the expression is false, it is not executed.
• You can group multiple expressions together with braces:
• if (expression)
{
statement 1;
statement 2;
statement 3;
}
The if / else statement

• Syntax: if (expression) statement_1; else statement_2;


• If the expression is true, statement_1 will be executed,
otherwise, statement_2 will be.

if (myVal < 3)
printf(“myVal is less than 3.\n”);
else
printf(“myVal is greater than or equal to 3.\n”);
The for Loop

• Syntax: for (initialization; test; increment)


{statements;}
• The for loop will first perform the initialization.
Then, as long is test is TRUE, it will execute
statements. After each execution, it will
increment.
The for Loop

for (cntr = 0; cntr < 3; cntr = cntr + 1)


{
printf(“ Counter = %d\n”, cntr);
}

Counter = 0;
Counter = 1;
Counter = 2
Thank You

You might also like