0% found this document useful (0 votes)
3 views21 pages

C Programming Course for Engineers

The document outlines a course on Programming for Engineers using the C language, covering topics such as arrays, strings, and memory allocation. It includes examples of array declaration, initialization, and common programming errors. Additionally, it provides information on string handling functions and input/output methods in C.

Uploaded by

ngocthanh2821
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views21 pages

C Programming Course for Engineers

The document outlines a course on Programming for Engineers using the C language, covering topics such as arrays, strings, and memory allocation. It includes examples of array declaration, initialization, and common programming errors. Additionally, it provides information on string handling functions and input/output methods in C.

Uploaded by

ngocthanh2821
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Course: Programming for Engineers (C language)

Dr.-Ing. Nguyen Van Binh


Email: nvbinh@[Link]
HCMC, Mar. 2025
Content

1. Introduction to Computer and C language


2. Making Decisions
3. Looping
4. Arrays
5. Functions
6. Pointers
7. Structures
8. Characters and Strings
9. File Processing
[Link] Memory Allocation
19 September 2022 Programming for Engineers (C language) NV Binh 2
3 Array - String
3.1 Array

– An array is a variable that can store multiple values.


Example: int data[5];

19 September 2022 Programming for Engineers (C language) NV Binh 3


3 Array - String
3.1 Array
• How to declare an array?
– dataType arrayName[arraySize];

– For Example: float mark[5];

• Access Array Elements: by indices.


– Arrays have 0 as the first index, not 1. In this example, mark[0] is the first
element.

– If the size of an array is n, to access the last element, the n-1 index is
used. In this example, mark[4]

19 September 2022 Programming for Engineers (C language) NV Binh 4


3 Array - String
3.1 Array
• Initialize an array:
– int mark[5] = {19, 10, 8, 17, 9};
– int mark[] = {19, 10, 8, 17, 9};

mark[0] is equal to 19
mark[1] is equal to 10
mark[2] is equal to 8
mark[3] is equal to 17
mark[4] is equal to 9

19 September 2022 Programming for Engineers (C language) NV Binh 5


3 Array - String
3.1 Array
• Change Value of Array elements
int mark[5] = {19, 10, 8, 17, 9} // make the value of the third element to -1
mark[2] = -1; // make the value of the fifth element to 0
mark[4] = 0;
 Input and Output Array Elements

// take input and store it in the 3rd element


scanf("%d", &mark[2]);
// take input and store it in the ith element
scanf("%d", &mark[i-1]);

// print the third element of the array


printf("%d", mark[2]); /
/ print ith element of the array
printf("%d", mark[i-1]);
19 September 2022 Programming for Engineers (C language) NV Binh 6
3 Array - String
3.1 Array
Example 1

19 September 2022 Programming for Engineers (C language) NV Binh 7


3 Array - String
3.1 Array

Enter 5 integers: 1
-3
3
4
0
3
Displaying integers: 1
-3
3
4
0
3

19 September 2022 Programming for Engineers (C language) NV Binh 8


3 Array - String
3.1 Array
Example 2

19 September 2022 Programming for Engineers (C language) NV Binh 9


3 Array - String
3.1 Array

Enter n: 5
Enter number1: 45
Enter number2: 35
Enter number3: 38
Enter number4: 31
Enter number5: 49
Average = 39

19 September 2022 Programming for Engineers (C language) NV Binh 10


3 Array - String
3.2 Multidimensional Arrays

• 2D array: float x[3][4 ]; (12 elements)

• 3D array: y[2][4][3]; (24 elements)

19 September 2022 Programming for Engineers (C language) NV Binh 11


3 Array - String
3.2 Multidimensional Arrays

• Initialization of a 2D array

// Different ways to initialize two-dimensional array


int c[2][3] = {{1, 3, 0}, {-1, 5, 9}};
int c[][3] = {{1, 3, 0}, {-1, 5, 9}};
int c[2][3] = {1, 3, 0, -1, 5, 9};

• Initialization of a 3D array

int test[2][3][4] = { {{3, 4, 2, 3}, {0, -3, 9, 11}, {23, 12, 23, 2}}, {{13, 4, 56, 3}, {5,
9, 3, 5}, {3, 1, 4, 9}}};

19 September 2022 Programming for Engineers (C language) NV Binh 12


3 Array - String
3.2 Multidimensional Arrays

19 September 2022 Programming for Engineers (C language) NV Binh 13


3 Array - String
3.2 Multidimensional Arrays
Size of Arrays
• Size of a 1D array : int x[i];

i=sizeof(myArray)/sizeof(myArray[0]);
• Size of a 2D array : int x[i][j];

i=sizeof(myArray)/sizeof(myArray[0]);
j=sizeof(myArray[0])/sizeof(myArray[0][0]);
• Size of a 3D array : int x[i][j][k];

i=sizeof(myArray)/sizeof(myArray[0]);
j=sizeof(myArray[0])/sizeof(myArray[0][0]);
k= sizeof(myArray[0][0])/sizeof(myArray[0][0][0]);
19 September 2022 Programming for Engineers (C language) NV Binh 14
3 Array - String
3.3 String
• String Arrays:
char label[] = ”Hello world"; // 12 characters element

H e l l o w o r l d \0
char label[15] = ”Hello world"; // 15 characters element

H e l l o w o r l d \0

19 September 2022 Programming for Engineers (C language) NV Binh 15


3 Array - String
3.3 String
String Input & Output
#include<stdio.h>
void main()
{
char str[50];
printf("Enter a string: ");
//scanning the whole string, including the white spaces
scanf("%[^\n]s", &str);
printf("%s", str);
}

Or:
char text[20];
gets(text);
printf("%s", text);
19 September 2022 Programming for Engineers (C language) NV Binh 16
3 Array - String
3.3 String
String Handling Functions

#include<string.h>

Method Description
strcat() It is used to concatenate(combine) two
strings
strlen() It is used to show length of a string
strrev() It is used to show reverse of a string
strcpy() Copies one string into another
strcmp() It is used to compare two string
19 September 2022 Programming for Engineers (C language) NV Binh 17
3 Array - String
3.3 String
String Handling Functions
• strcat() function
Strcat("hello", "world");
• strlen() function
int j;
j = strlen("studytonight");
printf("%d",j);
Output: 12
• strcmp() function
int j;
j = strcmp("study", "tonight");
printf("%d",j);

Output:-1 // A>B  1 ; A=B  0; A<B  -1


19 September 2022 Programming for Engineers (C language) NV Binh 18
3 Array - String
3.3 String
Common Programming Error

• A source of “off-by-one” errors.

–int value[7]; value[7]=5

• Forgetting to initialize the elements of an array whose elements should be


initialized.

• Providing more initializers in an array initializer list than there are elements
in the array is a syntax error.

–int value[3]={1,2,3,4};

19 September 2022 Programming for Engineers (C language) NV Binh 19


3 Array - String
3.3 String

Common Programming Error

• Referencing a double-subscripted array element as a[x,y] instead of


a[x][y]. C interprets a[x,y] as a[y], and as such it does not cause a syntax
error

• Not providing scanf with a character array large enough to store a string
typed at the keyboard

19 September 2022 Programming for Engineers (C language) NV Binh 20


3 Array - String
3.3 String
Common Programming Error

• Ending a #define or #include preprocessor directive with a semicolon.


Remember that preprocessor directives are not C statements.
– #include <stdio.h>
– #define PI 3.141592
• Assigning a value to a symbolic constant in an executable statement is a
syntax error.
A symbolic constant is not a variable. No space is reserved for it by the
compiler as with variables that hold values at execution time.
– #define MAX 500 ~ int MAX=500;
–#define SIZE 50
–int value[SIZE]
19 September 2022 Programming for Engineers (C language) NV Binh 21

You might also like