User Tools

Site Tools


cplusplus:c_examples

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Next revision
Previous revision
cplusplus:c_examples [2016/04/21 23:31] – created mithatcplusplus:c_examples [2020/12/07 02:40] (current) – [C Examples] mithat
Line 1: Line 1:
 ====== C Examples ====== ====== C Examples ======
-from \\ +Mithat Konar \\  
-**Differences Between C and C++** \\ +Derived from //**Differences Between C and C++**// by Robert Niemann \\ 
-by Robert Niemann (Century College) \\ +2020-05-03 
-11/22/2015+ 
 +===== Input/output ===== 
 + 
 +  * [[http://www.cplusplus.com/reference/cstdio/printf/ | printf() reference]] 
 +  * [[http://www.cplusplus.com/reference/cstdio/scanf/ | scanf() reference]] 
 + 
 + 
 +<file c example00.c> 
 +/* Basic printf use. */ 
 +#include <stdio.h> 
 + 
 +int main() 
 +
 +  int numSpoons = 5; 
 + 
 +  printf( "I have %d spoons.\n", numSpoons ); 
 +   
 +  return 0; 
 +
 +</file>
  
 <file c example01.c> <file c example01.c>
 +/* Basic scanf use. */
 #include <stdio.h> #include <stdio.h>
  
 int main() int main()
 { {
-    int this_is_a_number;+    int num;
  
     printf( "Please enter a number: " );     printf( "Please enter a number: " );
-    scanf( "%d", &this_is_a_number ); +    scanf( "%d", &num ); 
-    printf( "You entered %d", this_is_a_number ); +    printf( "You entered %d\n", num ); 
-    getchar();+
     return 0;     return 0;
 } }
Line 21: Line 41:
  
 <file c example02.c> <file c example02.c>
 +/* Common I/O format specifiers. */
 #include <stdio.h> #include <stdio.h>
  
-main()+int main()
 { {
   int dec = 5;   int dec = 5;
Line 31: Line 52:
  
   printf("%d %s %f %c\n", dec, str, pi,  ch);   printf("%d %s %f %c\n", dec, str, pi,  ch);
 +  
 +  return 0;
 } }
 </file> </file>
  
 <file c example03.c> <file c example03.c>
 +/* More I/O format specifiers. */
 #include <stdio.h> #include <stdio.h>
  
-main()+int main()
 { {
     char ch;     char ch;
Line 43: Line 67:
     double y;     double y;
  
-    printf("enter a character, an integer, and a double:\n");+    printf("Enter a character, an integer, and a double:\n");
     scanf("%c %d %lf", &ch, &x, &y);     scanf("%c %d %lf", &ch, &x, &y);
     printf("%c %d %lf\n", ch, x, y);     printf("%c %d %lf\n", ch, x, y);
 +    
 +    return 0;
 } }
 </file> </file>
 +
 +===== File access =====
 +
 +  * [[http://www.cplusplus.com/reference/cstdio/fopen/ | fopen() reference]]
  
 <file c example04.c> <file c example04.c>
Line 58: Line 88:
     int x;     int x;
  
-    ptr_file =fopen("output.txt", "w");+    ptr_file = fopen("output.txt", "w");
  
     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,"%d\n", x);         fprintf(ptr_file,"%d\n", x);
 +    }
  
     fclose(ptr_file);     fclose(ptr_file);
Line 79: Line 111:
 { {
     FILE *ptr_file;     FILE *ptr_file;
-    char buf[1000];+    char buf[1000];    /* can store strings up to 999 chars long */
  
-    ptr_file =fopen("input.txt","r");+    ptr_file = fopen("input.txt","r");
     if (!ptr_file)     if (!ptr_file)
-            return 1;+    { 
 +        return 1; 
 +    }
  
-    while (fgets(buf,1000, ptr_file)!=NULL) +    while (fgets(buf,1000, ptr_file) != NULL) 
-            printf("%s",buf);+    { 
 +        printf("%s",buf); 
 +    }
  
     fclose(ptr_file);     fclose(ptr_file);
 +    
     return 0;     return 0;
 } }
 </file> </file>
cplusplus/c_examples.1461281479.txt.gz · Last modified: 2016/04/21 23:31 by mithat

Donate Powered by PHP Valid HTML5 Valid CSS Driven by DokuWiki