SlideShare une entreprise Scribd logo
1  sur  42
Télécharger pour lire hors ligne
C_Programming
Part 4
ENG. KEROLES SHENOUDA
1
Advanced Data Types: Arrays
 Array is a single variable that able to hold several values.
Programmer can set or get specific
value in the array.
To define an array variable containing 100 (float) values:
2
Arrays
 The position of each value is called the index. The index of the first value is 0,
which means
if the array contains 10 items the index range from 0 to 9.
 Dealing with array elements is very similar to normal variables; you can assign
a value to any item or assign any item to other program variables. Following
code illustrates this idea:
3
float degrees[100];
float x=9.9;
float y;
//assign x value to the fifth item
degrees[4] = x;
//assign fifth item to y
y = degrees[4];
//scan eighth item from user
scanf("%f", &degrees[7]);
//print eighth item from user
printf("The eighth item value is %f", degrees[7]);
Lab: Store and Print 10
Students Degrees
 shows how to scan 10
students degree from user,
stores them in a single
area, and then prints them.
4
5
6
7
Arrays
 Initializing the array:
When the array is defined, there is no data assigned to their elements.
Assigning initial
values for array elements is called initializing the array. Following code section
shows how
to initialize the array:
8
Lab:Calculate Polynomial
Value for a Set of Inputs
9
the program works
correctly, however it
appears that code size
contains many
repeated sections, each
section for each value. If
the number of values
increases code size
increases.
Alternative solve it
using Arrays and
Loops
10
Lab: Calculate the Maximum of
the Array
float degrees[] = {75.5, 88.0, 89.5, 23.5,
72.0,63.5, 57.5, 62.0, 13.5, 46.5};
11
printf ("=========>>>>>> Part2 descending order n");
printf ("=========>>>>>> Part3 ascending order n");
12
13
Bubble Sort 14
Bubble Sort 15
2D Arrays 16
2D Arrays 17
Following code section shows how to
use 2D arrays:
18
LAB: Calculate and Print the Transpose
of 3x3 Matrix
19
20
Strings
 String is a set of several consecutive characters; each character can be
represented in C language by a (char) data type.
 This means that string value can be represented by an array of (char)
21
Above code shows how to store “hello” string letters in array.
The last letter is sited with zero value (null termination), this value is
important to tell the computer that the string is
terminated before this item.
Printing String Value
 “text” variable contains 100 (char) value, only
first 5 places is used to hold the word “hello”
and
 the sixth place is used to hold the null
termination.
 In printf, “%s” is used to inform the program
that it will print a string value.
 Important: printf uses the null termination to
end the printing operation. If the null
termination is not used, the program continues
printing the following memory contents until
it reach a zero.
22
23
24
25
Scanning String Value 26
 scanf is used with “%s” to scan string input
from keyboard. However there is a problem?
 scanf takes only the first word in the input
text and leave the rest.
C language provides a solution for above
problem using gets function
27
Array of Strings 28
Printing Array of Strings 29
Copy String to String 30
There is another
solution to above
problem using strcpy
function. strcpy takes
both the
destination and the
source strings and
performs the coping
operation internally.
strcpy 31
Adding String to String 32
Changing String Case
 strlwr function changes all string letters to the lower
case.
Ex: “AhMed” “ahmed”
 strupr function change all string letters to the upper
case.
Ex: “aHmeD” “AHMED
33
Finding the String Length 34
Comparing Two Strings
 strcmp function compares two strings and produces one of three results:
 if the two string are identical it gives 0
 if the first string is lower in the alphabetic order it gives -1
 if the second string is higher in the alphabetic order it gives 1
“ahmed” and “ahmed” 0
“ahmed” and “amgad” -1 because the second letter „h‟ is less than „m‟
“maged” and “aya” 1 because the first letter „m‟ is greater than „a‟
strcmp differentiate between capital and small letters which means “MAGED” is
less than “ahmed”, because the ASCII code of the capital letters is lower than the ASCII
code of the small letters. To solve this problem you can change the case of both strings
to the same case then use strcmp function. Alternatively you can use stricmp
function which performs the
comparison independent on the string case
35
36
Converting String to Integer Value 37
The variable text contains 4 ASCII letters „1‟, „0‟, „2‟, „5‟. The statement (x + text) is
completely wrong, because text is string and not a number, computer cannot understand
string contents directly.
atoi function helps computer to convert string to a number of type (int) if it is applicable
otherwise it gives zero, for example:
“1025”  1025
“120KG”  120
“The Cost is 30”  0
Converting String to Integer Value 38
Converting String to Real Value
 atof function helps computer to convert string to a number of type (float) if it
is applicable
otherwise it gives zero, for example:
“10.25” > 10.25
“1.2KG” > 1.2
“The Cost is 3.5” > 0
39
40
Follow Chapter 4/5:
Controlling Program
Flow
C PROGRAMMING FOR ENGINEERS, DR. MOHAMED SOBH
41The next Part we will talk about Variables Scope
References 42
 The Case for Learning C as Your First Programming Language
 A Tutorial on Data Representation
 std::printf, std::fprintf, std::sprintf, std::snprintf…..
 C Programming for Engineers, Dr. Mohamed Sobh
 C programming expert.
 fresh2refresh.com/c-programming

Contenu connexe

Tendances

Tendances (20)

C programming session3
C programming  session3C programming  session3
C programming session3
 
Constants Variables Datatypes by Mrs. Sowmya Jyothi
Constants Variables Datatypes by Mrs. Sowmya JyothiConstants Variables Datatypes by Mrs. Sowmya Jyothi
Constants Variables Datatypes by Mrs. Sowmya Jyothi
 
C programming session6
C programming  session6C programming  session6
C programming session6
 
[ITP - Lecture 04] Variables and Constants in C/C++
[ITP - Lecture 04] Variables and Constants in C/C++[ITP - Lecture 04] Variables and Constants in C/C++
[ITP - Lecture 04] Variables and Constants in C/C++
 
CLASS VIII COMPUTERS FLOW CHART AND ALGORITHM
CLASS VIII COMPUTERS FLOW CHART AND ALGORITHMCLASS VIII COMPUTERS FLOW CHART AND ALGORITHM
CLASS VIII COMPUTERS FLOW CHART AND ALGORITHM
 
Ch7 Basic Types
Ch7 Basic TypesCh7 Basic Types
Ch7 Basic Types
 
MANAGING INPUT AND OUTPUT OPERATIONS IN C MRS.SOWMYA JYOTHI.pdf
MANAGING INPUT AND OUTPUT OPERATIONS IN C    MRS.SOWMYA JYOTHI.pdfMANAGING INPUT AND OUTPUT OPERATIONS IN C    MRS.SOWMYA JYOTHI.pdf
MANAGING INPUT AND OUTPUT OPERATIONS IN C MRS.SOWMYA JYOTHI.pdf
 
Ch3 Formatted Input/Output
Ch3 Formatted Input/OutputCh3 Formatted Input/Output
Ch3 Formatted Input/Output
 
STRINGS IN C MRS.SOWMYA JYOTHI.pdf
STRINGS IN C MRS.SOWMYA JYOTHI.pdfSTRINGS IN C MRS.SOWMYA JYOTHI.pdf
STRINGS IN C MRS.SOWMYA JYOTHI.pdf
 
Assignment12
Assignment12Assignment12
Assignment12
 
C language basics
C language basicsC language basics
C language basics
 
CP Handout#8
CP Handout#8CP Handout#8
CP Handout#8
 
Ocs752 unit 1
Ocs752   unit 1Ocs752   unit 1
Ocs752 unit 1
 
C programming part2
C programming part2C programming part2
C programming part2
 
Type Conversion in C++ and C# Arithmetic Expressions
Type Conversion in C++ and C# Arithmetic ExpressionsType Conversion in C++ and C# Arithmetic Expressions
Type Conversion in C++ and C# Arithmetic Expressions
 
Assignment8
Assignment8Assignment8
Assignment8
 
MATLAB programming tips 2 - Input and Output Commands
MATLAB programming tips 2 - Input and Output CommandsMATLAB programming tips 2 - Input and Output Commands
MATLAB programming tips 2 - Input and Output Commands
 
Ocs752 unit 3
Ocs752   unit 3Ocs752   unit 3
Ocs752 unit 3
 
Ch8 Arrays
Ch8 ArraysCh8 Arrays
Ch8 Arrays
 
CP Handout#2
CP Handout#2CP Handout#2
CP Handout#2
 

En vedette

En vedette (20)

Notes part3
Notes part3Notes part3
Notes part3
 
Homework 3
Homework 3Homework 3
Homework 3
 
Homework 2 solution
Homework 2 solutionHomework 2 solution
Homework 2 solution
 
Microcontroller part 1
Microcontroller part 1Microcontroller part 1
Microcontroller part 1
 
Microcontroller part 2
Microcontroller part 2Microcontroller part 2
Microcontroller part 2
 
Microcontroller part 4
Microcontroller part 4Microcontroller part 4
Microcontroller part 4
 
Microcontroller part 3
Microcontroller part 3Microcontroller part 3
Microcontroller part 3
 
K vector embedded_linux_workshop
K vector embedded_linux_workshopK vector embedded_linux_workshop
K vector embedded_linux_workshop
 
Embedded C programming session10
Embedded C programming  session10Embedded C programming  session10
Embedded C programming session10
 
Automative basics v3
Automative basics v3Automative basics v3
Automative basics v3
 
C programming first_session
C programming first_sessionC programming first_session
C programming first_session
 
Microcontroller part 5
Microcontroller part 5Microcontroller part 5
Microcontroller part 5
 
C programming part2
C programming part2C programming part2
C programming part2
 
Microcontroller part 4
Microcontroller part 4Microcontroller part 4
Microcontroller part 4
 
Microcontroller part 2
Microcontroller part 2Microcontroller part 2
Microcontroller part 2
 
Microcontroller part 6_v1
Microcontroller part 6_v1Microcontroller part 6_v1
Microcontroller part 6_v1
 
Microcontroller part 3
Microcontroller part 3Microcontroller part 3
Microcontroller part 3
 
Microcontroller part 7_v1
Microcontroller part 7_v1Microcontroller part 7_v1
Microcontroller part 7_v1
 
Microcontroller part 8_v1
Microcontroller part 8_v1Microcontroller part 8_v1
Microcontroller part 8_v1
 
Microcontroller part 1
Microcontroller part 1Microcontroller part 1
Microcontroller part 1
 

Similaire à C programming part4

C programming session 04
C programming session 04C programming session 04
C programming session 04
Dushmanta Nath
 
Dsp lab _eec-652__vi_sem_18012013
Dsp lab _eec-652__vi_sem_18012013Dsp lab _eec-652__vi_sem_18012013
Dsp lab _eec-652__vi_sem_18012013
Kurmendra Singh
 
Lec 25 - arrays-strings
Lec 25 - arrays-stringsLec 25 - arrays-strings
Lec 25 - arrays-strings
Princess Sam
 
C UNIT-3 PREPARED BY M V B REDDY
C UNIT-3 PREPARED BY M V B REDDYC UNIT-3 PREPARED BY M V B REDDY
C UNIT-3 PREPARED BY M V B REDDY
Rajeshkumar Reddy
 

Similaire à C programming part4 (20)

VISUAL BASIC 6 - CONTROLS AND DECLARATIONS
VISUAL BASIC 6 - CONTROLS AND DECLARATIONSVISUAL BASIC 6 - CONTROLS AND DECLARATIONS
VISUAL BASIC 6 - CONTROLS AND DECLARATIONS
 
Mcai pic u 4 function, storage class and array and strings
Mcai pic u 4 function, storage class and array and stringsMcai pic u 4 function, storage class and array and strings
Mcai pic u 4 function, storage class and array and strings
 
Bsc cs i pic u-4 function, storage class and array and strings
Bsc cs i pic u-4 function, storage class and array and stringsBsc cs i pic u-4 function, storage class and array and strings
Bsc cs i pic u-4 function, storage class and array and strings
 
Strings
StringsStrings
Strings
 
Btech i pic u-4 function, storage class and array and strings
Btech i pic u-4 function, storage class and array and stringsBtech i pic u-4 function, storage class and array and strings
Btech i pic u-4 function, storage class and array and strings
 
Functions torage class and array and strings-
Functions torage class and array and strings-Functions torage class and array and strings-
Functions torage class and array and strings-
 
C programming session 04
C programming session 04C programming session 04
C programming session 04
 
Dsp lab _eec-652__vi_sem_18012013
Dsp lab _eec-652__vi_sem_18012013Dsp lab _eec-652__vi_sem_18012013
Dsp lab _eec-652__vi_sem_18012013
 
Dsp lab _eec-652__vi_sem_18012013
Dsp lab _eec-652__vi_sem_18012013Dsp lab _eec-652__vi_sem_18012013
Dsp lab _eec-652__vi_sem_18012013
 
function, storage class and array and strings
 function, storage class and array and strings function, storage class and array and strings
function, storage class and array and strings
 
Lec 25 - arrays-strings
Lec 25 - arrays-stringsLec 25 - arrays-strings
Lec 25 - arrays-strings
 
Diploma ii cfpc u-4 function, storage class and array and strings
Diploma ii  cfpc u-4 function, storage class and array and stringsDiploma ii  cfpc u-4 function, storage class and array and strings
Diploma ii cfpc u-4 function, storage class and array and strings
 
Array and string
Array and stringArray and string
Array and string
 
Matlab strings
Matlab stringsMatlab strings
Matlab strings
 
MATLAB
MATLABMATLAB
MATLAB
 
[ITP - Lecture 17] Strings in C/C++
[ITP - Lecture 17] Strings in C/C++[ITP - Lecture 17] Strings in C/C++
[ITP - Lecture 17] Strings in C/C++
 
C UNIT-3 PREPARED BY M V B REDDY
C UNIT-3 PREPARED BY M V B REDDYC UNIT-3 PREPARED BY M V B REDDY
C UNIT-3 PREPARED BY M V B REDDY
 
Lecture 05 2017
Lecture 05 2017Lecture 05 2017
Lecture 05 2017
 
Pythonintro
PythonintroPythonintro
Pythonintro
 
Array &strings
Array &stringsArray &strings
Array &strings
 

Plus de Keroles karam khalil

Plus de Keroles karam khalil (20)

C basics quiz part 1_solution
C basics quiz part 1_solutionC basics quiz part 1_solution
C basics quiz part 1_solution
 
Autosar Basics hand book_v1
Autosar Basics  hand book_v1Autosar Basics  hand book_v1
Autosar Basics hand book_v1
 
Automotive embedded systems part6 v2
Automotive embedded systems part6 v2Automotive embedded systems part6 v2
Automotive embedded systems part6 v2
 
Automotive embedded systems part5 v2
Automotive embedded systems part5 v2Automotive embedded systems part5 v2
Automotive embedded systems part5 v2
 
EMBEDDED C
EMBEDDED CEMBEDDED C
EMBEDDED C
 
Automotive embedded systems part7 v1
Automotive embedded systems part7 v1Automotive embedded systems part7 v1
Automotive embedded systems part7 v1
 
Automotive embedded systems part6 v1
Automotive embedded systems part6 v1Automotive embedded systems part6 v1
Automotive embedded systems part6 v1
 
Automotive embedded systems part5 v1
Automotive embedded systems part5 v1Automotive embedded systems part5 v1
Automotive embedded systems part5 v1
 
Automotive embedded systems part4 v1
Automotive embedded systems part4 v1Automotive embedded systems part4 v1
Automotive embedded systems part4 v1
 
Automotive embedded systems part3 v1
Automotive embedded systems part3 v1Automotive embedded systems part3 v1
Automotive embedded systems part3 v1
 
Automotive embedded systems part2 v1
Automotive embedded systems part2 v1Automotive embedded systems part2 v1
Automotive embedded systems part2 v1
 
Automotive embedded systems part1 v1
Automotive embedded systems part1 v1Automotive embedded systems part1 v1
Automotive embedded systems part1 v1
 
Automotive embedded systems part8 v1
Automotive embedded systems part8 v1Automotive embedded systems part8 v1
Automotive embedded systems part8 v1
 
Quiz 9
Quiz 9Quiz 9
Quiz 9
 
C programming session10
C programming  session10C programming  session10
C programming session10
 
C programming session9 -
C programming  session9 -C programming  session9 -
C programming session9 -
 
Quiz 10
Quiz 10Quiz 10
Quiz 10
 
Homework 6
Homework 6Homework 6
Homework 6
 
Homework 5 solution
Homework 5 solutionHomework 5 solution
Homework 5 solution
 
C programming session8
C programming  session8C programming  session8
C programming session8
 

Dernier

1029 - Danh muc Sach Giao Khoa 10 . pdf
1029 -  Danh muc Sach Giao Khoa 10 . pdf1029 -  Danh muc Sach Giao Khoa 10 . pdf
1029 - Danh muc Sach Giao Khoa 10 . pdf
QucHHunhnh
 

Dernier (20)

Unit-V; Pricing (Pharma Marketing Management).pptx
Unit-V; Pricing (Pharma Marketing Management).pptxUnit-V; Pricing (Pharma Marketing Management).pptx
Unit-V; Pricing (Pharma Marketing Management).pptx
 
Dyslexia AI Workshop for Slideshare.pptx
Dyslexia AI Workshop for Slideshare.pptxDyslexia AI Workshop for Slideshare.pptx
Dyslexia AI Workshop for Slideshare.pptx
 
SOC 101 Demonstration of Learning Presentation
SOC 101 Demonstration of Learning PresentationSOC 101 Demonstration of Learning Presentation
SOC 101 Demonstration of Learning Presentation
 
Application orientated numerical on hev.ppt
Application orientated numerical on hev.pptApplication orientated numerical on hev.ppt
Application orientated numerical on hev.ppt
 
1029 - Danh muc Sach Giao Khoa 10 . pdf
1029 -  Danh muc Sach Giao Khoa 10 . pdf1029 -  Danh muc Sach Giao Khoa 10 . pdf
1029 - Danh muc Sach Giao Khoa 10 . pdf
 
ICT role in 21st century education and it's challenges.
ICT role in 21st century education and it's challenges.ICT role in 21st century education and it's challenges.
ICT role in 21st century education and it's challenges.
 
Towards a code of practice for AI in AT.pptx
Towards a code of practice for AI in AT.pptxTowards a code of practice for AI in AT.pptx
Towards a code of practice for AI in AT.pptx
 
How to Create and Manage Wizard in Odoo 17
How to Create and Manage Wizard in Odoo 17How to Create and Manage Wizard in Odoo 17
How to Create and Manage Wizard in Odoo 17
 
Graduate Outcomes Presentation Slides - English
Graduate Outcomes Presentation Slides - EnglishGraduate Outcomes Presentation Slides - English
Graduate Outcomes Presentation Slides - English
 
Making communications land - Are they received and understood as intended? we...
Making communications land - Are they received and understood as intended? we...Making communications land - Are they received and understood as intended? we...
Making communications land - Are they received and understood as intended? we...
 
Python Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docxPython Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docx
 
Single or Multiple melodic lines structure
Single or Multiple melodic lines structureSingle or Multiple melodic lines structure
Single or Multiple melodic lines structure
 
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
 
Accessible Digital Futures project (20/03/2024)
Accessible Digital Futures project (20/03/2024)Accessible Digital Futures project (20/03/2024)
Accessible Digital Futures project (20/03/2024)
 
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptxHMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
 
FSB Advising Checklist - Orientation 2024
FSB Advising Checklist - Orientation 2024FSB Advising Checklist - Orientation 2024
FSB Advising Checklist - Orientation 2024
 
Food safety_Challenges food safety laboratories_.pdf
Food safety_Challenges food safety laboratories_.pdfFood safety_Challenges food safety laboratories_.pdf
Food safety_Challenges food safety laboratories_.pdf
 
Sociology 101 Demonstration of Learning Exhibit
Sociology 101 Demonstration of Learning ExhibitSociology 101 Demonstration of Learning Exhibit
Sociology 101 Demonstration of Learning Exhibit
 
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdfUGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
 
Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptxBasic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
 

C programming part4

  • 2. Advanced Data Types: Arrays  Array is a single variable that able to hold several values. Programmer can set or get specific value in the array. To define an array variable containing 100 (float) values: 2
  • 3. Arrays  The position of each value is called the index. The index of the first value is 0, which means if the array contains 10 items the index range from 0 to 9.  Dealing with array elements is very similar to normal variables; you can assign a value to any item or assign any item to other program variables. Following code illustrates this idea: 3 float degrees[100]; float x=9.9; float y; //assign x value to the fifth item degrees[4] = x; //assign fifth item to y y = degrees[4]; //scan eighth item from user scanf("%f", &degrees[7]); //print eighth item from user printf("The eighth item value is %f", degrees[7]);
  • 4. Lab: Store and Print 10 Students Degrees  shows how to scan 10 students degree from user, stores them in a single area, and then prints them. 4
  • 5. 5
  • 6. 6
  • 7. 7
  • 8. Arrays  Initializing the array: When the array is defined, there is no data assigned to their elements. Assigning initial values for array elements is called initializing the array. Following code section shows how to initialize the array: 8
  • 9. Lab:Calculate Polynomial Value for a Set of Inputs 9 the program works correctly, however it appears that code size contains many repeated sections, each section for each value. If the number of values increases code size increases. Alternative solve it using Arrays and Loops
  • 10. 10
  • 11. Lab: Calculate the Maximum of the Array float degrees[] = {75.5, 88.0, 89.5, 23.5, 72.0,63.5, 57.5, 62.0, 13.5, 46.5}; 11 printf ("=========>>>>>> Part2 descending order n"); printf ("=========>>>>>> Part3 ascending order n");
  • 12. 12
  • 13. 13
  • 18. Following code section shows how to use 2D arrays: 18
  • 19. LAB: Calculate and Print the Transpose of 3x3 Matrix 19
  • 20. 20
  • 21. Strings  String is a set of several consecutive characters; each character can be represented in C language by a (char) data type.  This means that string value can be represented by an array of (char) 21 Above code shows how to store “hello” string letters in array. The last letter is sited with zero value (null termination), this value is important to tell the computer that the string is terminated before this item.
  • 22. Printing String Value  “text” variable contains 100 (char) value, only first 5 places is used to hold the word “hello” and  the sixth place is used to hold the null termination.  In printf, “%s” is used to inform the program that it will print a string value.  Important: printf uses the null termination to end the printing operation. If the null termination is not used, the program continues printing the following memory contents until it reach a zero. 22
  • 23. 23
  • 24. 24
  • 25. 25
  • 26. Scanning String Value 26  scanf is used with “%s” to scan string input from keyboard. However there is a problem?  scanf takes only the first word in the input text and leave the rest.
  • 27. C language provides a solution for above problem using gets function 27
  • 29. Printing Array of Strings 29
  • 30. Copy String to String 30 There is another solution to above problem using strcpy function. strcpy takes both the destination and the source strings and performs the coping operation internally.
  • 32. Adding String to String 32
  • 33. Changing String Case  strlwr function changes all string letters to the lower case. Ex: “AhMed” “ahmed”  strupr function change all string letters to the upper case. Ex: “aHmeD” “AHMED 33
  • 34. Finding the String Length 34
  • 35. Comparing Two Strings  strcmp function compares two strings and produces one of three results:  if the two string are identical it gives 0  if the first string is lower in the alphabetic order it gives -1  if the second string is higher in the alphabetic order it gives 1 “ahmed” and “ahmed” 0 “ahmed” and “amgad” -1 because the second letter „h‟ is less than „m‟ “maged” and “aya” 1 because the first letter „m‟ is greater than „a‟ strcmp differentiate between capital and small letters which means “MAGED” is less than “ahmed”, because the ASCII code of the capital letters is lower than the ASCII code of the small letters. To solve this problem you can change the case of both strings to the same case then use strcmp function. Alternatively you can use stricmp function which performs the comparison independent on the string case 35
  • 36. 36
  • 37. Converting String to Integer Value 37 The variable text contains 4 ASCII letters „1‟, „0‟, „2‟, „5‟. The statement (x + text) is completely wrong, because text is string and not a number, computer cannot understand string contents directly. atoi function helps computer to convert string to a number of type (int) if it is applicable otherwise it gives zero, for example: “1025”  1025 “120KG”  120 “The Cost is 30”  0
  • 38. Converting String to Integer Value 38
  • 39. Converting String to Real Value  atof function helps computer to convert string to a number of type (float) if it is applicable otherwise it gives zero, for example: “10.25” > 10.25 “1.2KG” > 1.2 “The Cost is 3.5” > 0 39
  • 40. 40
  • 41. Follow Chapter 4/5: Controlling Program Flow C PROGRAMMING FOR ENGINEERS, DR. MOHAMED SOBH 41The next Part we will talk about Variables Scope
  • 42. References 42  The Case for Learning C as Your First Programming Language  A Tutorial on Data Representation  std::printf, std::fprintf, std::sprintf, std::snprintf…..  C Programming for Engineers, Dr. Mohamed Sobh  C programming expert.  fresh2refresh.com/c-programming