Vous êtes sur la page 1sur 29

C Programming language Section 1

Program: is a set of statements that solves a problem All programs work on data, which is the fuel of the program. So, we will learn the types of data, how to deal with them, and the different arrangement of data The program itself is trying to solve a real world problem. So, we have: Real World This is the problem Computer World The Program

The transformation from the real world to the computer world is an industry called programming

Why Learn C?
Compact, fast, and powerful Mid-level Language Standard for program development (wide acceptance) It is everywhere! (portable) Supports modular programming style Useful for all applications C is the native language of UNIX Easy to interface with system devices/assembly routines C is terse

The following program is written in the C programming language: #include <stdio.h> int main() { printf("Hello World! \n"); }

The Preprocessor : The preprocessor are compiler directive to direct the compiler before compiling. The preprocessor statements are statements that execute before compiling This statement begin with # example: #include<filename.h>

The header files are file that contain the function prototype of the function defined in the library file. Before compilation, the compiler will compile the header file then complete the program.

The Program body It consists of many functions, at least one function with the name main() int main() {// begin of function variable or constant declaration; statement ; return 0; } //end of function

Variable or Constant declaration


The data may be one of the following:

Variable/Constant each of them must have a data type which is one of the following:

The data type:


Char (1byte) (-128 to 127) int (2byte) (-32768 to 32767) float (4byte) (3.4e-38 to 3.4e38) double (8byte) (1.7e-308 to 1.7e308) The difference between float and double is in the precision The float has 7 number after the decimal point The double has 15 number after the decimal point.

There is also short int (2byte) long int (4byte) (-2147483648 to 2147483647) long double (10byte) 23 number after the decimal point unsigned int (2bytes) (0 to 65535) unsigned short int (2bytes) (0 to 65535) unsigned long int (4bytes) (0 to 4294967295

Constant: It is a fixed value that the program may not change. It can have any of the data type above. It is declared with one of the two ways: -In the preprocessor: # define constant-name constant-value Ex: - # define x 3 this mean x is an integer with value 3 # define ch a this mean ch is a character with value a -In the program const data-type constant-name = constant-value; Ex:- const int x=3; const char ch=a;

Variable: It is a named location in the memory that is used to hold a value that may be modified by the program. The entire variable must be declared before they can be used. The general form of declaration: data-type variable-name; Ex:- int x,y;

Note
1. The variable may be declared:
Locally: It is declared in the function. So, we can use it only in this function and it end with the end of function. Global: The variables are declared out of any function. So, we can use them in any part of the program and also we can use them in any function

2. Any variable declared in the main () are locally for the main only. 3. It is not recommended to use global variable.

The program statements


The program statement consists of many things: Input function, Expression and operation, Output function Each statement must terminate with ;

The input function: These function are used to get data from users. The following function can be used: scanf() function: it has the format: scanf(format specifier, &var-name); example: scanf(%d,&x); it is in the header file stdio.h

Format specifier: %d integer %f float %c char %s string %u unsigned integer %x hexadecimal %ld long integer %lf double %Lf long double

The output function:


These functions are used to display the results on the screen. The following function can be used: printf() function:

It has the format: printf(message sequence, variable); Example: printf(hello %c \n,ch); It is in the header file stdio.h putchar() function: it display a character on the screen it has the format: putchar(int ); example: putchar(23); It is in the header file stdio.h

Escape sequence: \a alert \b backspace \n new line + carrige return \t horizontal tab (8character) \\ print backslash \? Print question mark \ print single quote \ print double quote

The Expression and Operators:

1)Operators
There are several operators in C language: It may be unary operator which has one operand or binary operator which has two operands. All operation must have a value. The operator may be classified as: The Mathematical operator: + sign add the two operands - sign subtract the second operands from the first operands * sign multiply the two operands /...sign divided the first operand by the second %.sign modulus operator (it give the reminder of the division)

Example: # include <stdio.h> void main() {int i,x,y; x=100; y=7425; i=y%x; printf(%d,i);} The value will be 25 Note: *) In the division, if we divide int by int the result will be integer. *) To divide two integer and to have the value float we must make one of the operands acts as float. This is done by using type casting: The type casting: You force an expression to be of a specific type during a certain statement only. It has the form: (new_type) expression so we can use it to have a decimal value when dividing two integer

Example: # include <stdio.h> int main() { float x,y; float i; x=3; y=7; i=y/x; printf(%f,i); return 0; } The value of i will be 2.3333333

The Relational operator It allows the programmer to compare two values and return non zero value if the comparison is right or zero if the comparison is wrong. Usually used for conditional statements.

> greater than < lower than >= greater than or equal <= lower than or equal = = equal != not equal
The Logical operator The logical operator works with logical values (zero or non zero) and returns zero and non zero && and || or ! not

The Increment/Decrement operator ++ increment by 1 -- decrement by 1 The pre-increment: ++x The post-increment: x++ The difference between pre-increment and postincrement can be seen in the following examples

#include <stdio.h> void main() { int x,y; x=10; y=++x; printf(%d \n,x); printf(%d \n,y); } the value of x=11 the value of y=11 x=x+1; y=x; increment then assigning

#include <stdio.h> void main() { int x,y; x=10; y=x++; printf(%d \n,x); printf(%d \n,y); } the value of x=11 the value of y=10 y=x; x=x+1; assign then increment

The assignment operator = equal x = 5; += x += 5; equivalent to x = x+5; -= x -= 5; equivalent to x = x-5; *= x *= 5; equivalent to x = x*5; /= x /= 5; equivalent to x = x/5; %= x %= 5; equivalent to x = x%5; Note: The assignment puts the value at the Right Hand Side (RHS) in the variable at the Left Hand Side (LHS). Then the operation of the assignment has a value which is the RHS. This helps us to perform multiple assignment which is valid in C Language. i.e. : x = y = z = 4;

2)Expression
A) Selection (conditional) statements

1) The if statements:
if (condition) { statements; } The statement between { } will be valid if the condition is true(non zero) other wise it will be escaped and continue the program. The if statement is used when some extra instruction will be done if the condition is valid then continues the program and if the condition is not valid, it continues the program:

Example: Write a program to read a number. If it is odd (multiply the number*2) then print it, if it is even print it.

Example: Write a program to have the student grade and print if it is passed or failed, knowing that passing is from 60

3)The Switch case: It is a multiple branch selection, it has the form: switch (expression) { case constant1: { statement; break; } case constant2: { statement; break; } default: {statement;} }

Example1: Write a program to enter 1, 2, or 3 then the program write one, two or three otherwise write Out of range

Vous aimerez peut-être aussi