cplusplus:c_examples
Differences
This shows you the differences between two versions of the page.
Next revision | Previous revisionNext revisionBoth sides next revision | ||
cplusplus:c_examples [2016/04/21 23:31] – created mithat | cplusplus:c_examples [2019/05/06 15:35] – mithat | ||
---|---|---|---|
Line 1: | Line 1: | ||
====== C Examples ====== | ====== C Examples ====== | ||
- | from \\ | + | adapted |
**Differences Between C and C++** \\ | **Differences Between C and C++** \\ | ||
by Robert Niemann (Century College) \\ | by Robert Niemann (Century College) \\ | ||
11/22/2015 | 11/22/2015 | ||
+ | |||
+ | ===== Input/ | ||
+ | |||
+ | * [[http:// | ||
+ | * [[http:// | ||
+ | |||
+ | |||
+ | <file c example00.c> | ||
+ | /* Basic printf use. */ | ||
+ | #include < | ||
+ | |||
+ | int main() | ||
+ | { | ||
+ | int numSpoons = 5; | ||
+ | |||
+ | printf( "I have %d spoons.", | ||
+ | | ||
+ | return 0; | ||
+ | } | ||
+ | </ | ||
<file c example01.c> | <file c example01.c> | ||
+ | /* Basic scanf use. */ | ||
#include < | #include < | ||
int main() | int main() | ||
{ | { | ||
- | int this_is_a_number; | + | int num; |
printf( " | printf( " | ||
- | scanf( " | + | scanf( " |
- | printf( "You entered %d", | + | printf( "You entered %d", |
- | | + | |
return 0; | return 0; | ||
} | } | ||
Line 21: | Line 42: | ||
<file c example02.c> | <file c example02.c> | ||
+ | /* Common I/O format specifiers. */ | ||
#include < | #include < | ||
- | main() | + | int main() |
{ | { | ||
int dec = 5; | int dec = 5; | ||
Line 31: | Line 53: | ||
printf(" | printf(" | ||
+ | | ||
+ | return 0; | ||
} | } | ||
</ | </ | ||
<file c example03.c> | <file c example03.c> | ||
+ | /* More I/O format specifiers. */ | ||
#include < | #include < | ||
- | main() | + | int main() |
{ | { | ||
char ch; | char ch; | ||
Line 43: | Line 68: | ||
double y; | double y; | ||
- | printf(" | + | printf(" |
scanf(" | scanf(" | ||
printf(" | printf(" | ||
+ | | ||
+ | return 0; | ||
} | } | ||
</ | </ | ||
+ | |||
+ | ===== File access ===== | ||
+ | |||
+ | * [[http:// | ||
<file c example04.c> | <file c example04.c> | ||
Line 58: | Line 89: | ||
int x; | int x; | ||
- | ptr_file =fopen(" | + | ptr_file = fopen(" |
if (!ptr_file) | if (!ptr_file) | ||
return 1; | return 1; | ||
- | for (x=1; x<=10; x++) | + | for (x = 1; x <= 10; x++) |
+ | { | ||
fprintf(ptr_file," | fprintf(ptr_file," | ||
+ | } | ||
fclose(ptr_file); | fclose(ptr_file); | ||
Line 81: | Line 114: | ||
char buf[1000]; | char buf[1000]; | ||
- | ptr_file =fopen(" | + | ptr_file = fopen(" |
if (!ptr_file) | if (!ptr_file) | ||
- | | + | { |
+ | | ||
+ | } | ||
- | while (fgets(buf, | + | while (fgets(buf, |
- | printf(" | + | { |
+ | | ||
+ | } | ||
fclose(ptr_file); | fclose(ptr_file); | ||
+ | | ||
return 0; | return 0; | ||
} | } | ||
</ | </ |
cplusplus/c_examples.txt · Last modified: 2020/12/07 02:40 by mithat