Sic Parvis Magna

1 Ağustos 2024 Perşembe

C/CPP - Fonksiyonlar ve Kullanım Yöntemleri #1

Ağustos 01, 2024 Posted by unexpected , , No comments

printf

Formatlanmış bir metni standart çıktıya (stdout) yazar. Başarı durumunda yazılan karakter uzunluğunu döndürür. Her hangi bir hata durumda ise ferror ve negatif bir sayı döner.

Syntax
int printf(const char* _Format, ...);

Params

  • format: String değeri
  • varargs: Array değeri
example.cpp
#include <stdio.h>

int main(int argc, char** argv)
{
	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;
}

fprintf

Formatlanmış bir metni dosya veya standart input output'a yazar. Başarı durumunda yazılan karakter uzunluğunu döndürür. Her hangi bir hata durumda ise ferror ve negatif bir sayı döner.

Syntax
int fprintf(FILE* file, const char* format, ...);

FILE* __acrt_iob_func(unsigned index);

Params

  • file: Stream akışı
  • format: String değeri
  • varargs: Array değeri
example.cpp
#include <stdio.h>

int main(int argc, char** argv)
{
	/// 0 - stdin  - Standart Input
    /// 1 - stdout - Standart Output
    /// 2 - stderr - Standart Error

	fprintf(stdout, "Hello World!");
    fprintf(__acrt_iob_func(1), "Hello World!");
    
	return 0;
}

sprintf

Formatlanmış bir metni stream tarafından işaret edilen tampon bir değikende tutulur. İçerikten sonra otomatik olarak sonlandırıcı bir null karakteri eklenir. Başarı durumunda yazılan toplam karakter sayısını döndürür. Başarısızlık durumunda negatif bir sayı döndürülür.

Syntax
int sprintf(char* stream, const char* format, ...);

Params

  • stream: Tampon değeri
  • format: String değeri
  • varargs: Array değeri
example.cpp
#include <stdio.h>

int main(int argc, char** argv)
{
	wchar_t buf[50];
	char buffer[50];
	int cx;
	
	cx = sprintf(buffer, "The half of %d is %d", 60, (60 / 2));
	printf("cx: %d - Buffer: %s", cx, buffer);
    
	return 0;
}

snprintf

Formatlanmış bir metni stream tarafından işaret edilen tampon bir değikende tutulur. İçerikten sonra otomatik olarak sonlandırıcı bir null karakteri eklenir. Başarı durumunda yazılan toplam karakter sayısını döndürür. Başarısızlık durumunda negatif bir sayı döndürülür.

Syntax
int snprintf(char* stream, size_t n, const char* format, ...);

Params

  • stream: Tampon değeri
  • n: Tampon boyutu
  • format: String değeri
  • varargs: Array değeri
example.cpp
#include <stdio.h>

int main(int argc, char** argv)
{
	wchar_t* buffer[150];
	char buf[150];
	int cx, cx2;
	
	cx = snprintf(buf, 150, "The half of %d is %d", 60, (60 / 2));
	cx2 = snprintf((buf + cx), (150 - cx), ", and the half of that is %d.", (60 / 2 / 2));
	printf("cx: %d - cx2: %d - Buffer: %s", cx, cx2, buf);
    
	return 0;
}
Specifier Output Example
%d or %i Signed Decimal Integer 392
%u Unsigned Decimal Integer 7235
%o Unsigned Octal 610
%x Unsigned Hexadecimal Integer 7fa
%X Unsigned Hexadecimal Integer (Uppercase) 7Fa
%f Decimal Floating Point (Lowercase) 392.65
%F Decimal Floating Point (Uppercase) 392.65
%e Scientific Notation (Mantissa/Exponent) (Lowercase) 3.9265e+2
%E Scientific Notation (Mantissa/Exponent) (Uppercase) 3.9265e+2
%g Use the shortest representation: %e or %f 392.65
%G Use the shortest representation: %E or %F 392.65
%a Hexadecimal Floating Point (Lowercase) -0xc.90fep-2
%A Hexadecimal Floating Point (Uppercase) -0XC.90FEP-2
%c Character a
%s String of Characters sample
%p Pointer Address b8000000
%n Nothing Printed
%% % Karakteri %

0 comments:

Yorum Gönder