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. (It’ll 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.
• 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.
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 ..
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:
• It’s the function that doesn’t return
anything !
• It’s the function that doesn’t
take or return anything !
What’s 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:

A brief introduction to C Language

  • 1.
    A brief introductionto C Mohamed Elsayed Head of Technical Committee IEEE ZC
  • 2.
    Before the programminglanguage: 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: Youcan say that any programming language is just like a dictionary which contains words that mean certain circuits. For example : addition, multiplication, etc.
  • 4.
  • 5.
    Structure of asimple program :
  • 6.
    Make the thingssimpler ! • #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.
  • 7.
    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.
  • 8.
  • 9.
    Declaring variables : intx; int x = 5; float x ; float x = 3.3; char x ; char x = ‘F’;
  • 10.
    printf The syntax : printf(“formatstring”,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’ HelloWorld Hello World Hello WorldHello World
  • 12.
    Do you wantto enter some input? • Scanf is a function • The syntax: scanf("%d", &a);
  • 13.
    Do you havea compiler ? Go to • http://www.tutorialspoint.com/compile_c_online.php • http://www.learn-c.org/
  • 14.
    Semi-colon : in Cis like in English
  • 15.
    Comments in C: • Ignored by the compiler • Are used to enhance the readability of your code
  • 16.
  • 17.
    Example: I go tothe university .. IF Else
  • 18.
    If and if-elsestatement : used to execute the code if condition is true or false.
  • 19.
    The from ofif-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 ityourself .. 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 docomplicated 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 bemore branched ..
  • 24.
    Loops are allaround us !
  • 25.
  • 26.
    For loop syntax: •Itis good if number of iteration is known by the user. •Syntax:- for(initialization;condition;incr/decr){ //code to be executed }
  • 27.
  • 28.
    Example: Try ityourself .. 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){ //codeto be executed } • It is better if number of iteration is not known by the user.
  • 30.
    Example: Try ityourself ..
  • 31.
    Break the loopunder a certain condition !
  • 32.
    Break statement: it isused to break the execution of loop (while, do while and for) and switch case. Syntax:- jump-statement; break;
  • 33.
  • 34.
  • 35.
    Function form : •Syntax to call function:- variable=function_name(arguments...);
  • 36.
    A simple function: Try it yourself ..
  • 37.
    Void in functions: •It’s the function that doesn’t return anything ! • It’s the function that doesn’t take or return anything !
  • 38.
    What’s 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
  • 40.