Uploaded by Eslam Sami

c part1

advertisement




IDE is stands for integrated development
environment
Example : code block or atmel studio
IDE is consist of editor and preprocessor and
compiler and linker .
The compilation process will be explained later
in details .

C was originally developed by Dennis
Ritchie between 1969 and 1973 at Bell Labs.
and used to implement the Unix operating system
(C is the native language of UNIX).
 C has been standardized by the American National
Standards Institute (ANSI) since 1989
And put first standard in c which called C89 or
ANSIC
 Most of developers consider C is medium level
language .
 C is structure oriented language but C++ is object
oriented language


c
is case sensitive (uppercase letters are different
from lowercase letters )

C89 (ANSIC)

C90

C99

C11 (which is standardized in 2011)
1- assembly language:
Assembly is low level language
 Advantages:





Optimized code size and execution speed.
No need to use compiler you can directly convert it to machine
code by using just assembler.
Why do not we choose to write programs with Assembly
Language?
Disadvantages:
It is difficult to write codes with assembly language. It takes more
time to develop code.
 It is not easily understood.
 The code is not portable depends on the platform you are
developing on. It depends on instruction set available by the micro
processor.

2- C language:
 Advantages:




Easier to understand than assembly.
It is simple to write codes and it is easily understood.
It is faster in development than assembly.
It is portable in case the platform used was changed.

Disadvantage:


Less performance than Assembly. Assembly is very
optimized and every instruction written gives
deterministic “execution time” and “code size”.
This drawback is solved by the use of high
quality compilers that can change the written
C code into an optimized Assembly language.
Keep the benefit of C language “Portability”.


The primary design of C is to produce portable
code while maintaining performance and
minimizing footprint (CPU time , memory ).
One powerful reason is memory allocation.
Unlike most programming languages, C allows
the programmer to write directly to memory.
\n : new line
 \t : tap
 \\ : \ character
 \' :' character
 \" :" character
 \? :? character
 \a : Alert or bell
---------------------------------------------------------------“ ” : Double Quotation Marks which used to scan
inputs and display outputs .







#include <stdio.h>
int main()
{
printf ("Hello world!\n") ;
return 0;
}

1- Integer Types
Type
Storage size
Value range
unsigned char
1 byte
0 to 255
signed char
1 byte
-128 to 127
signed int (compiler
dependent )
2 or 4 bytes
-32,768 to 32,767 or -2,147,483,648 to 2,147,483,647
unsigned int
2 or 4 bytes
(compiler dependent
)
0 to 65,535 or 0 to 4,294,967,295
signed short
2 bytes
-32,768 to 32,767
unsigned short
2 bytes
0 to 65,535
signed long
4 bytes
-2,147,483,648 to 2,147,483,647
unsigned long
4 bytes
0 to 4,294,967,295

Notes
There are also other types such as :
signed long long ----- 8 bytes
unsigned long long ----- 8 bytes

Range for unsigned n-bit variable :
0 to 2n-1
Range for signed n-bit variable :
Positive numbers : 0 to 2n-1-1
Negative numbers : -1 to -2n-1
2- Floating-Point Types
float  4 byte
double  8 byte

long double  compiler dependent but in most
cases : 12 bytes or 10 bytes .
A C identifier is a name used to identify a
variable, function, or any other user-defined
Item .
 All identifiers have the same rules for naming
them .
 So the following rules for naming the variables
will be similar to the rules for naming
functions and other user-defined items .




int x ----- this is declaration of variable x
Every variable name should start with alphabets or
underscore (_) or ($) sign (can’t start by numbers).
Except underscore (_) and ($) sign ,no other special
symbol are allowed in the middle of the variable
declaration (even space not allowed) but numbers are
allowed in the middle of variable declaration .
Don’t use any reserved keywords in c like :
(if , else , char , int , while ,….) as variable name.

You should ensure that you use meaningful (but
short names ) for your variable name
, the reason for this are to make the program easier to
read , for example compare these two lines of code :
X= y *z
Distance = speed * time


X=10 ------- initialize x with value 10
Note
 int x =10 ----------- declaration and
initialization of x in the same line
variable declaration
 extern int a, b;
 extern int c;
 extern float f;
variable definition or variable declaration
 int a, b;
 int c;
 float f;


Endianness refers to the sequential order used
to numerically interpret a range
of bytes in computer memory.
Words may be represented in big-endian
(MSB) or little-endian (LSB) format.
1- big endian
 You store the most significant byte in the
smallest address. Here's how it would look.
 A word to mean 32 bits.
 Suppose we have a 32 bit
quantity, written as 90AB12CD16
2- little endian
 In little endian, you store the least significant
byte in the smallest address. Here's how it
would look:
 Suppose we have a 32 bit
quantity, written as 90AB12CD16
important note : code block is
Little endian
Let we need to get the binary of -1 (char):
First step
1’s complement:
1  00000001
1’s complement  11111110
Second step
2’s complement:
Add 1 to the 1’s complement
2’s complement  11111111
Note there is another Shortcut method(in one step )
that will be illustrated in the session

= is the assignment operator
 The assignment is accomplished from right to
left
 Example
int x
int y=10
x=y (now x is assigned by value 10 ) (copied the
value 10 from y to x )

Sometimes programmers need to add some notes
beside their code. Those notes are very important
to describe the code and to clarify complex
operation
 Comments have two types :
1- single line comment
// comment
2- multiple lines comment
/*
comment
*/

Specifiers
%i or %d  to deal with signed int numbers
%u
 to deal with unsigned int numbers
%hd
to deal with signed short numbers
%hu
to deal with unsigned short numbers
%c
 to deal with characters
%f
 to deal with float numbers and (double in printf)
%ld
 to deal with signed long numbers
%lu
 to deal with unsigned long numbers
%lld
 to deal with signed long long numbers
%llu
 to deal with unsigned long long numbers
%lf
 to deal with double in scanf
%Lf
 to deal with long double

1- input values
Use scanf function to scan inputs from user .
Syntax
scanf(“specifier”, &variable name)
Example
int x
scanf(“%i”,&x)
2- output values
Use printf function to display variables
Syntax
printf(“specifier”, variable name)
Example
int x=10
printf(“%i”, x)
Example
Write a program to read a value from a user and then
display this value
program
#include <stdio.h>
int main()
{
int x;
printf("please enter the value of x :");
scanf("%i",&x); //ask for the value of x from the user
printf("x=%i",x); // print x
return 0;
}
Example
Write program to read 2 values from a user and then display
their values
program
#include <stdio.h>
int main()
{
int x,y;
printf("please enter the values of x and y:\n");
scanf("%i%i",&x,&y); //ask for the values of x and y from
the user
printf("x=%i\ny=%i",x,y);
return 0;
}
Program
#include <stdio.h>
int main()
{
char x='a'; // single quotation used to store one
character
printf("x=%c\n",x); // display the character
printf("x=%i",x); // display the ascii code of this
char
return 0;
}
program
#include <stdio.h>
int main()
{
char x;
printf("enter the character:");
scanf("%c",&x); //scan the character
printf("x=%c\n",x); // display the character
printf("x=%i",x); // display the ascii code of this char
return 0;
}
Download