際際滷

際際滷Share a Scribd company logo
A brief introduction to C
Mohamed Elsayed
Head of Technical Committee IEEE
ZC
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.
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.
Article to read ..
Structure of a simple program :
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. (Itll be explained soon)
 printf() The printf() function is used to print data on the console.
What is #include <stdio.h>
 stdio stands for standard
input/output which contains
functions that help in the process of
input or output.
 Its 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.
Data Types:
Declaring variables :
int x;
int x = 5;
float x ;
float x = 3.3;
char x ;
char x = F;
printf
The 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);
printf and n
Hello World
Hello World
Hello WorldHello World
Do you want to enter some input?
 Scanf is a function
 The syntax: scanf("%d", &a);
Do you have a compiler ?
Go to
 http://www.tutorialspoint.com/compile_c_online.php
 http://www.learn-c.org/
Semi-colon :
in C is like in English
Comments in C :
 Ignored by the compiler
 Are used to enhance the readability of your code
Making decisions ?
Example:
I go to the university ..
IF Else
If and if-else statement :
used to execute the code if condition is true or false.
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
}
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 ?
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
It might be more branched ..
A brief introduction to C Language
Loops are all around us !
The structure of a loop:
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
}
For loop :
Example: Try it yourself ..
The output :
The current value of x is 0
The current value of x is 1
The current value of x is 2
The current value of x is 3
The current value of x is 4
The current value of x is 5
The current value of x is 6
The current value of x is 7
The current value of x is 8
The current value of x is 9
While loop :
while(condition){
//code to be executed
}
 It is better if number of iteration is not known by the user.
Example: Try it yourself ..
Break the loop under a certain condition !
Break statement:
it is used to break the execution of loop (while, do while and for) and
switch case.
Syntax:-
jump-statement;
break;
Example:
Functions:
Function form :
 Syntax to call function:-
variable=function_name(arguments...);
A simple function : Try it yourself ..
Void in functions:
 Its the function that doesnt return
anything !
 Its the function that doesnt
take or return anything !
Whats next ?
CS50: Introduction to Computer Science Embedded Systems - Shape The World YouTube Tutorials
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
An Arduino program:

More Related Content

A brief introduction to C Language

  • 1. A brief introduction to C Mohamed Elsayed Head of Technical Committee IEEE ZC
  • 2. 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.
  • 3. 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.
  • 5. Structure of a simple program :
  • 6. 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. (Itll be explained soon) printf() The printf() function is used to print data on the console.
  • 7. What is #include <stdio.h> stdio stands for standard input/output which contains functions that help in the process of input or output. Its 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.
  • 9. Declaring variables : int x; int x = 5; float x ; float x = 3.3; char x ; char x = F;
  • 10. printf The 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);
  • 11. printf and n Hello World Hello World Hello WorldHello World
  • 12. Do you want to enter some input? Scanf is a function The syntax: scanf("%d", &a);
  • 13. Do you have a compiler ? Go to http://www.tutorialspoint.com/compile_c_online.php http://www.learn-c.org/
  • 14. Semi-colon : in C is like in English
  • 15. Comments in C : Ignored by the compiler Are used to enhance the readability of your code
  • 17. Example: I go to the university .. IF Else
  • 18. If and if-else statement : used to execute the code if condition is true or false.
  • 19. 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 }
  • 20. 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 ?
  • 21. 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
  • 22. It might be more branched ..
  • 24. Loops are all around us !
  • 25. The structure of a loop:
  • 26. 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 }
  • 28. Example: Try it yourself .. The output : The current value of x is 0 The current value of x is 1 The current value of x is 2 The current value of x is 3 The current value of x is 4 The current value of x is 5 The current value of x is 6 The current value of x is 7 The current value of x is 8 The current value of x is 9
  • 29. While loop : while(condition){ //code to be executed } It is better if number of iteration is not known by the user.
  • 30. Example: Try it yourself ..
  • 31. Break the loop under a certain condition !
  • 32. Break statement: it is used to break the execution of loop (while, do while and for) and switch case. Syntax:- jump-statement; break;
  • 35. Function form : Syntax to call function:- variable=function_name(arguments...);
  • 36. A simple function : Try it yourself ..
  • 37. Void in functions: Its the function that doesnt return anything ! Its the function that doesnt take or return anything !
  • 38. Whats next ? CS50: Introduction to Computer Science Embedded Systems - Shape The World YouTube Tutorials
  • 39. 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