0% found this document useful (0 votes)
9 views23 pages

C Programming Tutorial - 2 - Spring2024

This document is a tutorial on C programming, covering syntax, pointers, parameter passing, and arrays. It includes exercises for writing C programs that handle command line arguments, find maximum values, and manipulate arrays. The tutorial also explains the use of pointers and the difference between passing parameters by value and by reference.

Uploaded by

samirjoa3
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)
9 views23 pages

C Programming Tutorial - 2 - Spring2024

This document is a tutorial on C programming, covering syntax, pointers, parameter passing, and arrays. It includes exercises for writing C programs that handle command line arguments, find maximum values, and manipulate arrays. The tutorial also explains the use of pointers and the difference between passing parameters by value and by reference.

Uploaded by

samirjoa3
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

C Programing Tutorial - 2

CENG460
C Syntax
Lets look at a final example. Header file needed for the usage of “atoi”
function.
Array of characters pointers listing all
arguments passed. “argv[0]” is the program
name.
Number of command line arguments passed
including the name of the program.
Print the arguments passed after converting
them to integers using the function “atoi”.
Print the sum of numbers passed as command
line arguments. Multiple format specifiers for
“printf” are used. “%s” used to print “result
is” and “%d” is used to print the sum of
numbers.
Exercise
❑Write a C program
❑that takes two integers as command line arguments,
❑print all the passed arguments
❑and then prints their sum using a function sum(int, int)
Hint: don’t forget to check if two integers are passed as command line arguments
Exercise - Answer
User Input

Scan a number from the user


Exercise 3
1) Write a C program that scans a number “n” from the user and prints all even
numbers ranging between 1 and “n”

2) Write a C program that finds and prints the maximum between three
numbers. Scan one number from the user and pass two numbers as command
line arguments. You should write and use a function find_max() to find the
maximum.
Pointers
1. What are pointers?
1. As the name implies, a pointer “points” to a location in memory. They are variables that store memory addresses.
2. For what purposes?
1. Retrieving value stored in memory location
2. Passing large objects without copying them
3. Accessing dynamically allocated memory
4. Referring to functions
3. Notations used with pointers
1. & → address of operator
2. * → dereference operator
4. Declaration of a pointer
1. <variable_type> *<name>;
C Pointers
Which are valid initialization
WHY?
Pointers stores addresses not values

3 is a value
b is equal to 2 which is a value

b is not a pointer
What is the output a=1
b=2
e = 1675541944
*e = 2
&e = 1675541936
&b = 1675541944
f = 1675541944
*f = 2
&f = 1675541928
a=1 &b = 1675541944
b=2 f = 1675541944
e = 1675541944 *f = 2
*e = 2 &f = 1675541928
&e = 1675541936

Addresses 1675541944 1675541936 1675541928

Values 1 2 1675541944 ... 1675541944

Variable name a b e f
Notice that e, f, &f, &e
*f = 5 contains memory
What will happen?! addresses to view this
address we used %p
instead of %d in printf

Addresses 1675541944 1675541936 1675541928

Values 1 ...
52 1675541944 1675541944

Variable name a b e f
C Pointer Syntax
Lets take a look at this example.

Pass the address of each variable to the


function.

Function that takes 2 pointers as


arguments.

Dereference each pointer to get the


stored value at that memory location and
compute their sum.
C Parameters Passing
1. By value:
1. A duplicate copy of the argument is created and passed to the called function.
2. Any update of the variable inside the function won’t affect the original variable.

2. By reference
1. The address of the variable is passed to the called function.
2. Any update of the variable inside the function will affect the original value since the value is directly
changed in its exact memory location.

By Value By Reference
Exercise
1. Write a C program that reads two integers from the user and
swaps them in the main.

2. Write a C program that reads 2 integers in the main and swaps


them using a function swap()
Exercise 1 - Solution
Exercise 2 -
Solution
Arrays
Arrays in C are composed of a particular type, laid out in memory in a repeating pattern. Array
elements are accessed by stepping forward in memory from the base of the array by a multiple
of the element size.
Brackets specifies the count of elements.
Elements are set in the braces.
Access element at index 0 in array

Print element at index 2 using pointer


notation. A evaluates to the address of
the first element in the array.

For loops that iterates over the elements.


Exercise

❑ Write a program that reads an integer n (1 < n <10), then fills an


array of size n. Your program should compute the average of the n
numbers.
Exercise -
Solution
Same function, different notations

You might also like