Uploaded by DIRANE NGALA

Inputs and outputs in C prorgramming

advertisement
Input and Output in C Programming
Input and output (I/O) are essential parts of any
programming language, including C. C provides several
functions for input and output operations, including printf,
scanf, gets, and puts.
a. printf Function
The printf function is used to output data to the console.
The syntax of the printf function is as follows:
printf("format string", argument list);
Here is an example program that uses the printf function:
#include <stdio.h>
int main() {
int num = 5;
printf("The value of num is %d\n", num);
return 0;
}
In the above program, the printf function is used to print
the value of num to the console.
b. scanf Function
The scanf function is used to input data from the console.
The syntax of the scanf function is as follows:
scanf("format string", argument list);
Here is an example program that uses the scanf function:
#include <stdio.h>
int main() {
int num;
printf("Enter a number: ");
scanf("%d", &num);
printf("The number you entered is %d\n", num);
return 0;
}
In the above program, the scanf function is used to input a
number from the console, which is then printed to the
console using the printf function.
c. gets and puts Functions
The gets function is used to input a string from the
console, and the puts function is used to output a string to
the console. The syntax of the gets and puts functions are
as follows:
char str[100];
gets(str);
puts(str);
Here is an example program that uses the gets and puts
functions:
#include <stdio.h>
int main() {
char str[100];
printf("Enter a string: ");
gets(str);
printf("You entered: ");
puts(str);
return 0;
}
In the above program, the gets function is used to input a
string from the console, which is then printed to the
console using the puts function.
Expression Statements in C Programming
Expression statements are used in C programming to
evaluate an expression and discard the result.
Here is an example program that uses an expression
statement:
#include <stdio.h>
int main() {
int num1 = 5, num2 = 10;
(num1 > num2) ? printf("%d is greater than %d\n",
num1, num2) : printf("%d is greater than %d\n", num2,
num1);
return 0;
}
In the above program, the expression statement (num1 >
num2) ? printf("%d is greater than %d\n", num1, num2) :
printf("%d is greater than %d\n", num2, num1) is used to
evaluate which number is greater, and then print the result
to the console using the printf function.
Debugging Techniques in C Programming
Debugging is the process of identifying and fixing errors
in a program. C provides several debugging techniques,
including using a debugger, printing debug information to
the console, and using error checking functions.
a. Debugger
A debugger is a tool that allows you to step through a
program line by line and examine the values of variables
at each step. Debuggers are built into many integrated
development environments (IDEs), such as Visual Studio
and Eclipse.
b. Printing Debug Information to the Console
Printing debug information to the console is a simple and
effective debugging technique. You can use the printf
function to print the values of variables and intermediate
results to the console, which can help you identify where
errors are occurring in your program.
Here is an example program that uses printf statements
for debugging:
#include <stdio.h>
int main() {
int num1 = 5, num2 = 10;
int sum = num1 + num2;
printf("The value of num1 is %d\n", num1);
printf("The value of num2 is %d\n", num2);
printf("The sum of num1 and num2 is %d\n", sum);
return 0;
}
In the above program, printf statements are used to print
the values of num1, num2, and sum to the console, which
can help identify any errors that may be occurring.
c. Error Checking Functions
C provides several error checking functions, such as
assert and errno, which can be used to check for errors
and handle them appropriately.
The assert function checks whether an expression is true
and stops the program if it is not. Here is an example
program that uses the assert function:
#include <stdio.h>
#include <assert.h>
int main() {
int num = 10;
assert(num > 5);
printf("The value of num is %d\n", num);
return 0;
}
In the above program, the assert function is used to check
whether num is greater than 5. If it is not, the program
will stop executing and display an error message.
The errno function is used to check for errors in system
calls, such as file I/O and network operations. Here is an
example program that uses the errno function:
#include <stdio.h>
#include <errno.h>
int main() {
FILE *fp;
fp = fopen("test.txt", "r");
if (fp == NULL) {
printf("Error opening file: %s\n", strerror(errno));
} else {
printf("File opened successfully\n");
fclose(fp);
}
return 0;
}
In the above program, the fopen function is used to open a
file called test.txt. If the file cannot be opened, the
program will use the strerror and errno functions to print
an error message to the console. If the file is opened
successfully, the program will print a success message and
close the file.
Overall, these debugging techniques can help you identify
and fix errors in your C programs, making them more
reliable and robust.
Download