Sie sind auf Seite 1von 21

ELEMENTS OF C

Programming Languages

Preprocessor Directives
Lines that are always preceded by a hash sign (#). The preprocessor is executed before the actual compilation of code begins, therefore the preprocessor digests all these directives before any code is generated by the statements.

Example:
#include <stdio.h> #define

The main() Function


main() is the entry point for execution for all C programs

In ANSI C, main() is an int, meaning it returns an integer to the operating system (usually 0)

Example:
int main () { code goes here code goes here return 0; }

Identifiers
composed of a sequence of letters, digits and the special character _ (underscore). names supplied for variables, types, functions, and labels in the program

Example:
interest_rate computer_02

Keywords
reserved words in C that have a special meaning.

Example:
auto break case char const continue default do double else enum extern float for goto if int long register return short signed static sizedof struct switch typedef union unsigned void volatile while

Variables
identifiers that can store a changeable value. These can be different data types. refers to a location in memory where a value can be stored for use by a program.

can be of three types: Local, Global, and Parameter


All variables and constants should be declared before they can be used in the main program.

Variables
Types:

LOCAL VARIABLE declared inside a function GLOBAL VARIABLE known throughout the entire program and may be used by any piece of code PARAMETER VARIABLE used for passing values between functions

#include<stdio.h> main() { int a,b,c; _________; _________; _________; }


Global Variables

Local Variables

#include<stdio.h> int a,b,c; main() {_________; _________; _________; }

Rules for Naming Variables


1) It must consist of letters, digits and underscore. _duh, num_1 2) It should not begin with a digit. 1name, 3ot3, 4 3) Keywords are not allowed to be variables printf, scanf 4) It is case sensitive 5) It do not include embedded blanks 6) Do not call yout variable/ by other name as other functions. main

Variable Declaration
Syntax

Data Type Variable terminator (;)

Example: int_firstname, sn; float num1, ex_2,file; char first name; char lastname [20];

C Data Types
Data type is a data storage format that can contain a specific type or range of values.
Data Type Format Specifier Meaning Examples

Int
Float Char Char

%d
%f %c %s

Whole number
Number w/ decimal point Single letter, symbol/num. Series of characters

3,7,+8,-9,10000
5.8, 9.0, 5.6789, +0.0032 B, r, *, 3 Hi, 143, Ms.

C Data Types
Character - Implemented by char keyword. They are used to store ASCII character and are always 1 byte long. Integer - Implemented by int keyword. They are used to store integer value and are always 4 byte long. Floating Point - Implemented by float keyword. They are used to store decimal values and are always 1 byte long. Double Floating Point - Implemented by double keyword. They are also used to store decimal values but with higher precision and are always 8 byte long. Valueless - Implemented by void keyword. They are used to explicitly define function returning no value or generic pointer. No variable of this type can be created. Boolean - Implemented by _Bool keyword. They are used to store Boolean values ie. True or False and are always 1 byte long. Complex - Implemented by _Complex keyword. They are used to store complex numbers and are operating system implemented. Imaginary - Implemented by _Imaginary keyword. They are used to store imaginary number and are operating system implemented .

Operators
Symbols used to relate two operands

Symbols that tells the computer to perform specific mathematical or logical manipulations.
Classified as Arithmetic, Logical, Bitwise

~ Arithmetic Operators
Operator + * / % ++ -Description Adds two operands Subtracts second operand from the first Multiply both operands Divide numerator by denumerator Modulus Operator and remainder of after an integer division Increment operator, increases integer value by one Decrement operator, decreases integer value by one Example A + B will give 30 A - B will give -10 A * B will give 200 B / A will give 2 B % A will give 0 A++ will give 11 A-- will give 9

~ Logical Operators
OPERATORS > >= < <= == != MEANING Greater than Greater than or equal Less than Less than or equal Equal Not equal SAMPLE 5>3 5>=5 7<10 7<=9 5==7 5!=6

OPERATORS && || !

MEANING AND OR NOT

~ Bitwise Operators
Operator & Description Binary AND Operator copies a bit to the result if it exists in both operands. Binary OR Operator copies a bit if it exists in eather operand. Binary XOR Operator copies the bit if it is set in one operand but not both. Example (A & B) will give 12 which is 0000 1100 (A | B) will give 61 which is 0011 1101 (A ^ B) will give 49 which is 0011 0001

| ^

Binary Ones Complement Operator is unary and has the efect of 'flipping' bits.
Binary Left Shift Operator. The left operands value is moved left by the number of bits specified by the right operand.
Binary Right Shift Operator. The left operands value is moved right by the number of bits specified by the right operand.

(~A ) will give -60 which is 1100 0011


A << 2 will give 240 which is 1111 0000

<<

>>

A >> 2 will give 15 which is 0000 1111

Truth Table (Boolean Logic)


True 1; False - 0

OR (||) at least one of the conditions is true


AND (&&) two conditions must be true
A
1 1

B
1 0

A&&B
1 0

A||B
1 1

!A
0 0

!B
0 1

0
0

1
0

0
0

1
0

1
1

0
1

Format Specifier
All format specifier start with a percent sign (%) and are followed by a single letter indicating the type of data and how data are be formatted. %c used for single char in C scanf(%c,&ch); printf(%c,ch); %d decimal number (whole number) scanf(%d,&num); printf(%d,num); %f number with floating or decimal point

scanf(%f,&pesos);
printf(%f,pesos);

%s

string of characters

scanf(%s,&str); printf(%s,str);

Expressions
Most fundamental element of C language. They are made up of two atomic elements: data and operator. A statement made up of data and operators operating upon that data which is evaluated by compiler.

any valid combination of operators, constants, functions and variables.


To solve mathematical problems using the computer, the formula should be translated to the programming language to be used.

Expressions
Example: b2 4ac = b*b-4*a*c a + b = (a+b)/(c+d) c+d x3 = x*x*x

Presented By: INOC, Jan David RAPIRAP, Chesca Mae


BSPE- 3B

July 20, 2012

Das könnte Ihnen auch gefallen