ICS4U – Grade 12 Computer Science – C Programming Guide


PDF Password: this_note_is_from_onstudynotes.com

Thanks, Priti!


C Programming- Part 1 Structure of a C program


Every C program consists of one or more modules called functions. One of the functions that must be called is the main. The program will always begin by executing the main function, which may access other functions.

Any other function definitions must be defined separately, either ahead of or after main. Each function must contain:

1) A function heading, which consists of the function name, followed by an optional list of arguments, enclosed in parentheses.

2)A list of argument declarations, if arguments are included in the headings. 3)A compound statement , which comprises the remainder of the function.

The arguments are symbols that represent information being passed between the function and other parts of the program.(also called parameters)

Each compound statement is enclosed between a pair of braces i.e { }. The braces may contain one or more elementary statements (called expression statements) and other compound statements. Thus compound statements may be nested one within another. Each expression statement must end with a semicolon(;).

Writing first program of C —————————
# include<studio.h> main( )

{ printf(“Welcome to C programming\n”); }
Identifiers and Keywords —————————

Identifiers are names that are given to various program elements such as variables, functions, and arrays. Identifiers consist of letters and digits , in any order except that the first character must be a letter. Both upper and lowercase letters are permitted though common usage favors the usage of lowercase letters for most type of identifiers. C is case-sensitive.The underscore(_) can also be included and is considered to be a letter. An underscore is often included in the middle of an identifier. An identifier may also begin with an underscore.

The following are valid identifiers. The following are valid identifiers.
x y12 sum_1 _temperature
names area tax_rate TABLE

C supports different types of data, each of which may be represented differently within the computer’s memory. The basic data types are listed below. Typical memory requirements are also given.

Data type Description Typical memory req. ———– ————- ———————-

Operators and Expressions
There are five arithmetic operators in C.They are- Operator Purpose

——— ———-

integer character

2 bytes 1 byte

floating-point no. double-precision floating pt no.

1 word (4 bytes) 2 words(8 bytes)

+ – * / %

addition subtraction

multiplication division

remainder after division
The % operator is sometimes referred to as the modulus operator.

The remainder operator (%) requires that both operands be integers and the second operand be non-zero. Similarly, the division operator(/) requires that the second operand be non-zero.

Relational and Logical Operators ———————————
There are 4 relational operators in C. They are-

< less than
<= less than or equal to
> greater than
>= greater than or equal to

These operators fall within the same precedence group, which is lower than the arithmetic and unary operators. The ass. of these operators is left to right.

Closely associated are the following 2 equality operators. == equal to

!= not equal to.

They are used to form logical expressions. Integer value 1 is used to represent true and integer value 0 is used to represent false.

C contains 3 logical operators- and, or and not. Operator meaning

——— ———-
&& and
|| or
! not
Operator category Operators Ass.

1)unary operators
3)add and subtract.
4)rational and equality <<=>>= == != L->R 5)logical and. && L->R 6)logical or || L->R

Assignment Operators ————————

There are several different assignment operators in C. All of them are used to form assignment expressions, which assign the value of an expression to an identifier.

The most commonly used assignment operator is ‘=’.
Assignment expressions that make use of this operator are written in the form. identifier=expression

where identifier generally represents a variable and expression represents a constant, a variable or a more complex expression.

Some assignment expressions- a=3


————- ——————
– ++–sizeof(type) R->L

*/% L->R +- L->R

The Conditional Operator
Simple conditional operations can be carried out with the conditional operations(?:).An expression that makes use of the conditional operator is called a conditional expression. In the form,


When evaluating a conditional expression, expression 1 is evaluated first. If expression 1 is true, then expression 2 is evaluated and this becomes the value of the conditional expression. However, if expression 1 is false, then expression 3 is evaluated and this becomes the value of the conditional expression.

Operator Precedence groups ——————————
conditional operator ?: R->L
assignment operators = += -= *= /= %= R->L Associativity

It means how operators of the same precedence are grouped in the absence of parentheses.

Data Input and Output
1)Single character input-The getchar function
Single characters can be entered into the computer using the C library function getchar.

The getchar function is a part of the standard C I/O library. It returns a single character from a standard input device(typically a keyboard).

character variable= getchar();
where character variable refers to some previously declared character variable. 2)Single Character Output-The putchar function

Single characters can be displayed(i.e written out of the computer) using the C library function putchar.The putchar function like getchar is as part of the standard C I/O library. It transmits a single character to a standard output device (typically a TV monitor). The character being transmitted will normally be represented as a character-type variable. It must be expressed as an argument to the function, enclosed in parentheses, following the word putchar.

In general, a function reference would be written as putchar(character variable)
3)Entering input data-the scanf function

Input data can be input into the computer from a standard input device by means of the C library function scanf. The function can be used to enter any combination of numerical values, single characters and strings. The function enters the number of data items that have been entered successfully.

In general terms scanf function is written as:

scanf(control string, arg1, arg2…., argn)

where control string refers to a string containing some required formatting information and arg1, arg2, arg n are arguments that represent the individual input data items.

The control string consists of individual groups of characters, with one character group for each input data item. Each character group must begin with a percent (%) sign. In its simplest form, a single character group will consist of the percent sign, followed by a conversion character, which indicates the type of the corresponding data item.

The arguments are written as variables or arrays, whose types match the corresponding character groups in the control string. Each variable name must be preceded by an &(ampersand). Array names should not begin with an ampersand.

Writing Output Data- the printf function ———————

This function can be used to output any combination of numerical values, single characters and strings. That is, the printf function moves data from the computer’s memory to the standard output device, whereas the scanf function enters data from the standard input device and stores it in the computer’s memory.

In general terms, the printf function is written as printf(control string, arg1, arg2, ……, argn)

where control string refers to a string that contains formatting information and arg1, arg2…., argn are arguments that represent the individual input data items. The arguments can be written as constants, single variable or array names , or more complex expressions. Function references may also be included.