库函数学习(1) strdup snprintf setenv atoi assert
2012-09-28 08:01
411 查看
1. strdup
2. snprintf
3. setenv unsetenv putenv unsetenv
4. atoi
5. assert
6.
#include<stdio.h> #include<string.h> #include<stdlib.h> int main(void){ char *source_str = "shanghai"; char *target_str = strdup(source_str);//malloc memory and duplicate a string target_str[0] = 'z'; puts(source_str); puts(target_str); return 0; }
2. snprintf
#include<stdio.h> #include<string.h> #include<stdlib.h> int main(){ char target[5]; char *source = "xianer"; //it's safer than sprintf, so just use it! snprintf(target, sizeof(target), "%s", source); puts(target); return 0; }
3. setenv unsetenv putenv unsetenv
#include<stdio.h> #include<stdlib.h> int main(){ char *user; if((user = getenv("USER")) != NULL){ printf("USER = %s\n", user); } setenv("USER", "test", 1); printf("USER = %s\n", getenv("USER")); unsetenv("USER"); printf("USER = %s\n", getenv("USER")); putenv("CITY=shanghai"); printf("CITY = %s\n", getenv("CITY")); return 0; }
4. atoi
#include <stdlib.h> #include <stdio.h> int main(void) { char *str = "123456"; int n = atoi(str); printf("The string %s as an integer is = %d\n",str,n); char *str2 = "314hello"; n = atoi(str2); printf("The string %s as an integer is = %d\n",str2,n); return 0; }
5. assert
#include<stdio.h> #include<assert.h> int main(){ int i = 0x80; assert(i & 128); puts("iiiiiiiiiiii"); int j = 0x20; assert(j & 128); puts("jjjjjjjjj"); return 0; }
6.
相关文章推荐
- OpenGL学习——(3)库函数
- 【stm32学习笔记之一】跑马灯实验(基于库函数)
- 国嵌视频学习第二天——库函数访问文件
- Win32 库函数学习(一)
- 2017年1月17日学习总结----系统调用和库函数
- 今天学习时突然联想到这样一个有趣但无用的问题:利用C语言(不能是其他语言)求两个整数的较大/小值-----要求不用if、三目?、switch、循环、数组、加减乘除求余、位运算符以及任何库函数
- C++学习笔记(二)——快速排序的库函数实现
- c语言学习之snprintf
- 学习在Linux创建库函数
- C常见库函数学习
- 面向对象编程之C库函数 -- strcpy , memcpy , strdup 之区别
- STM32F1学习-驱动led灯(库函数版本)
- 【小白笔记】PHP学习之路 (30) --绘制文字及相关库函数
- snprintf()和sprintf()学习
- 和luster学习在Linux创建库函数
- 晚上夜猫子一次,学习c++的一些库函数
- 和luster学习在Linux创建库函数
- linux内核学习(4)_库函数API和C代码嵌入汇运用用系统调用
- Win32ASM学习[8]: 进制转换的库函数
- 161218 Lua学习笔记-代码+笔记《库函数》