a brief introduction to c language

40
A brief introduction to C Mohamed Elsayed Head of Technical Committee IEEE ZC

Upload: mohamed-elsayed

Post on 18-Feb-2017

300 views

Category:

Education


0 download

TRANSCRIPT

Page 1: A brief introduction to C Language

A brief introduction to C

Mohamed Elsayed Head of Technical Committee IEEE ZC

Page 2: A brief introduction to C Language

Before the programming language:

A circuit which adds two numbers

But one problem arises here. To be able to write a computer program by typing out billions of 1s and 0s would require superhuman brainpower, and even then it would probably take you a lifetime or two to write.

Page 3: A brief introduction to C Language

Using programming language:

You can say that any programming language is just like a dictionary which contains words that mean certain circuits. For example : addition, multiplication, etc.

Page 5: A brief introduction to C Language

Structure of a simple program :

Page 6: A brief introduction to C Language

Make the things simpler !• #include <stdio.h> includes the standard input output library

functions. The printf() function is defined in stdio.h . (you can think of a library as a dictionary contains words and their meanings)

• void main() The main() function is the entry point of every program in c language. The void keyword specifies that it returns no value. (It’ll be explained soon)

• printf() The printf() function is used to print data on the console.

Page 7: A brief introduction to C Language

What is #include <stdio.h>• stdio stands for standard

input/output which contains functions that help in the process of input or output.• It’s more or less like including a

dictionary which contains words and their meanings (function and their definition)• Without it you cannot write printf()

because it will be meaningless to the compiler.

Page 8: A brief introduction to C Language

Data Types:

Page 9: A brief introduction to C Language

Declaring variables :

int x;int x = 5;float x ;float x = 3.3;char x ;char x = ‘F’;

Page 10: A brief introduction to C Language

printfThe syntax :printf(“format string”,arguments_list);

Place holders can be %d(integer), %c(character), %s(string), %f(float)

• Example : int x=50;printf(“My number is integer and its value is %d”,x);

Page 11: A brief introduction to C Language

printf and ‘\n’

Hello WorldHello World Hello WorldHello World

Page 12: A brief introduction to C Language

Do you want to enter some input?

• Scanf is a function• The syntax: scanf("%d", &a);

Page 13: A brief introduction to C Language

Do you have a compiler ?

Go to • http://www.tutorialspoint.com/compile_c_online.php• http://www.learn-c.org/

Page 14: A brief introduction to C Language

Semi-colon :

in C is like in English

Page 15: A brief introduction to C Language

Comments in C :

• Ignored by the compiler• Are used to enhance the readability of your code

Page 16: A brief introduction to C Language

Making decisions ?

Page 17: A brief introduction to C Language

Example:

I go to the university ..

IF Else

Page 18: A brief introduction to C Language

If and if-else statement :used to execute the code if condition is true or false.

Page 19: A brief introduction to C Language

The from of if-else statement:if(condition1){ //code to be executed if condition1 is true } else { // code to be executed if all the conditions are false }

Page 20: A brief introduction to C Language

Examples: Try it yourself ..

The number is greater than 50 ! Here is the rest of my program

Here is the rest of my program

Can you make the user enter the number instead of writing it in the program ?

Page 21: A brief introduction to C Language

Operators to do complicated jobs :

• If (x>5&&x<10) // if x is greater than 5 and less than 10• If (x==6||x==5) // if x is equal 6 or 5• If(x!=6) // if x is not equal to 6

Page 22: A brief introduction to C Language

It might be more branched ..

Page 23: A brief introduction to C Language
Page 24: A brief introduction to C Language

Loops are all around us !

Page 25: A brief introduction to C Language

The structure of a loop:

Page 26: A brief introduction to C Language

For loop syntax:•It is good if number of iteration is known by the user.•Syntax:-

for(initialization;condition;incr/decr){

//code to be executed }

Page 27: A brief introduction to C Language

For loop :

Page 28: A brief introduction to C Language

Example: Try it yourself ..The output :The current value of x is 0The current value of x is 1The current value of x is 2The current value of x is 3The current value of x is 4The current value of x is 5The current value of x is 6The current value of x is 7The current value of x is 8The current value of x is 9

Page 29: A brief introduction to C Language

While loop :while(condition){ //code to be executed } • It is better if number of iteration is not known by the user.

Page 30: A brief introduction to C Language

Example: Try it yourself ..

Page 31: A brief introduction to C Language

Break the loop under a certain condition !

Page 32: A brief introduction to C Language

Break statement: it is used to break the execution of loop (while, do while and for) and switch case.

Syntax:-jump-statement; break;

Page 33: A brief introduction to C Language

Example:

Page 34: A brief introduction to C Language

Functions:

Page 35: A brief introduction to C Language

Function form :

• Syntax to call function:-variable=function_name(arguments...);

Page 36: A brief introduction to C Language

A simple function : Try it yourself ..

Page 37: A brief introduction to C Language

Void in functions:• It’s the function that doesn’t return

anything !• It’s the function that doesn’t

take or return anything !

Page 39: A brief introduction to C Language

Arduino Program:int main(void){ setup();

while (1) // infinite loop loop(); return 0;}

void setup(){

}

void loop (){

}

The main function which contains calling 2 functions : setup and loop

The setup function which will be called only one time

The setup function which will be called only one time

Page 40: A brief introduction to C Language

An Arduino program: