/* scanf example */
#include <stdio.h>
int main ()
{
char str [80];
int i;
printf ("Enter your family name: ");
scanf ("%79s",str);
printf ("Enter your age: ");
scanf ("%d",&i);
printf ("Mr. %s , %d years old.\n",str,i);
printf ("Enter a hexadecimal number: ");
scanf ("%x",&i);
printf ("You have entered %#x (%d).\n",i,i);
return 0;
}
Enter your family name: Soulie
Enter your age: 29
Mr. Soulie , 29 years old.
Enter a hexadecimal number: ff
You have entered 0xff (255).
/* printf example */
#include <stdio.h>
int main()
{
printf ("Characters: %c %c \n", 'a', 65);
printf ("Decimals: %d %ld\n", 1977, 650000L);
printf ("Preceding with blanks: %10d \n", 1977);
printf ("Preceding with zeros: %010d \n", 1977);
printf ("Some different radices: %d %x %o %#x %#o \n", 100, 100, 100, 100, 100);
printf ("floats: %4.2f %+.0e %E \n", 3.1416, 3.1416, 3.1416);
printf ("Width trick: %*d \n", 5, 10);
printf ("%s \n", "A string");
return 0;
}
Characters: a A
Decimals: 1977 650000
Preceding with blanks: 1977
Preceding with zeros: 0000001977
Some different radices: 100 64 144 0x64 0144
floats: 3.14 +3e+000 3.141600E+000
Width trick: 10
A string
/* strcat example */
#include <stdio.h>
#include <string.h>
int main ()
{
char str[80];
strcpy (str,"these ");
strcat (str,"strings ");
strcat (str,"are ");
strcat (str,"concatenated.");
puts (str);
return 0;
}
these strings are concatenated.
char s1[50],
s2[50];
strcpy( s1, "Tweedledee " );
strcpy( s2, "Tweedledum" );
strcat( s1, s2 );
Tweedledee Tweedledum
strlen() Get length of a string.
strcpy() Copy one string to another.
strcat() Link together (concatenate) two strings.
strcmp() Compare two strings.
strchr() Find character in string.
strstr() Find string in string.
strlwr() Convert string to lowercase.
strupr() Convert string to uppercase.
#include <stdio.h>
#include <string.h>
int main()
{
char *t = "XXX";
printf( "Length of <%s> is %d.\n", t, strlen( t ));
}
Length of <XXX> is 3.
#include <dirent.h>
#include <stdio.h>
int main(void) {
DIR *d;
struct dirent *dir;
d = opendir(".");
if (d) {
while ((dir = readdir(d)) != NULL) {
printf("%s\n", dir->d_name);
}
closedir(d);
}
return(0);
}
#include <dirent.h>
#include <stdio.h>
int main(void) {
char path[1000];
struct dirent *dp;
DIR *dir = opendir("./new folder");
// Unable to open directory stream
if (!dir)
return;
while ((dp = readdir(dir)) != NULL)
{
if (strcmp(dp->d_name, ".") != 0 && strcmp(dp->d_name, "..") != 0)
{
printf("%s\n", dp->d_name);
// Construct new path from our base path
strcpy(path, "./new folder");
strcat(path, "/");
strcat(path, dp->d_name);
printf("%s\n",path);
}
}
closedir(dir);
return(0);
}
#include <stdio.h>
#include <string.h>
int main(void) {
char *p = "hello_12345678901234567890";
char q[] = "hello_12345678901234567890"; // no need to count this
printf("%d\n", sizeof(p)); // => size of pointer to char -- 4 on x86, 8 on x86-64
printf("%d\n", sizeof(q)); // => size of char array in memory -- 6 on both
// size_t strlen(const char *s) and we don't get any warnings here:
printf("%d\n", strlen(p)); // => 5
printf("%d\n", strlen(q)); // => 5
return(0);
}
char arr[] = "Hello World"; // array version
char ptr* = "Hello World"; // pointer version
#include <stdio.h>
int main( ) {
int c;
printf( "Enter a value :");
c = getchar( );
printf( "\nYou entered: ");
putchar( c );
return 0;
}
int c;
c = getchar( );
Enter a value : this is test
You entered: t
#include <stdio.h>
#include <math.h>
int main()
{
float result,value;
printf("Input a float value: ");
scanf("%f",&value);
result = sqrt(value);
printf("The square root of %.2f is %.2fn",
value,result);
result = pow(value,3);
printf("%.2f to the 3rd power is %.2fn",
value,result);
result = floor(value);
printf("The floor of %.2f is %.2fn",
value,result);
result = ceil(value);
printf("And the ceiling of %.2f is %.2fn",
value,result);
return(0);
}
syntax : double ceil(double x)
// C code to illustrate
// the use of ceil function.
#include <stdio.h>
#include <math.h>
int main ()
{
float val1, val2, val3, val4;
val1 = 1.6;
val2 = 1.2;
val3 = -2.8;
val4 = -2.3;
printf ("value1 = %.1lf\n", ceil(val1));
printf ("value2 = %.1lf\n", ceil(val2));
printf ("value3 = %.1lf\n", ceil(val3));
printf ("value4 = %.1lf\n", ceil(val4));
return(0);
}
value1 = 2.0
value2 = 2.0
value3 = -2.0
value4 = -2.0
syntax : double floor(double x)
// C code to illustrate
// the use of floor function
#include <stdio.h>
#include <math.h>
int main ()
{
float val1, val2, val3, val4;
val1 = 1.6;
val2 = 1.2;
val3 = -2.8;
val4 = -2.3;
printf("Value1 = %.1lf\n", floor(val1));
printf("Value2 = %.1lf\n", floor(val2));
printf("Value3 = %.1lf\n", floor(val3));
printf("Value4 = %.1lf\n", floor(val4));
return(0);
}
Value1 = 1.0
Value2 = 1.0
Value3 = -3.0
Value4 = -3.0
syntax : double fabs(double x)
// C code to illustrate
// the use of fabs function
#include <stdio.h>
#include <math.h>
int main ()
{
int a, b;
a = 1234;
b = -344;
printf("The absolute value of %d is %lf\n", a, fabs(a));
printf("The absolute value of %d is %lf\n", b, fabs(b));
return(0);
}
The absolute value of 1234 is 1234.000000
The absolute value of -344 is 344.000000
syntax : double log(double x)
syntax : double log10(double x)
syntax : double fmod(double x, double y)
syntax : double sqrt(double x)
syntax : double pow(double x, double y)
syntax : double modf(double x, double *integer)
syntax : double exp(double x)
syntax : double cos(double x)
syntax : double acos(double x)
syntax : double tanh(double x)